내가 쓴 다음, 그러나 즉시 종료 : Spring 애플리케이션에서 가장 간단한 main() 메소드는 무엇입니까?
public static void main(String[] args) {
new ClassPathXmlApplicationContext("/springtests/test01.xml");
}
test01.xml
등 네트워크
또는 무언가에 연결하는 일부 스레드 (데몬) 여러 콩을 포함
?내가 쓴 다음, 그러나 즉시 종료 : Spring 애플리케이션에서 가장 간단한 main() 메소드는 무엇입니까?
public static void main(String[] args) {
new ClassPathXmlApplicationContext("/springtests/test01.xml");
}
test01.xml
등 네트워크
또는 무언가에 연결하는 일부 스레드 (데몬) 여러 콩을 포함
?여기에 개체를 만드는 것뿐입니다. 빈을 검색하여 사용해야합니다. 예를 들어
ApplicationContext ctx = new ClassPathXmlApplicationContext("/springtests/test01.xml");
MyClass myObj= MyClass.class.cast(ctx.getBean("myBeanName"));
myObj.doStuff();
도움이 더는 다음 test01.xml에 무엇을 게시하려면
당신은 that the beans in the applicationContext are shut down properly을 보장하기 위해 context.registerShutdownHook()
를 호출 할 수 있습니다,하지만 당신은 여전히 메인 스레드는 경우 종료하지 않도록하십시오 applicationContext는 데몬 스레드 만 시작합니다.
을 호출 할 수있는 컨텍스트에서 일부 빈을 검색하는 것이 더 좋은 전략 일 수 있습니다. 즉, 상황에서 시작된 스레드 중 하나 이상을 비 데몬으로 만들 수 있습니다.
다른 답변의 권장 사항 대부분에 동의하지만 귀하의 주된 방법은 그대로입니다. 변경할 필요가있는 것은 적어도 하나의 작업자 스레드를 비 데몬으로 만드는 것입니다. Thread.setDaemon 자바 문서에서
:Java 가상 머신이 종료 실행하는 thread가 demon thread 때.
또한 모든 스레드가 bean의 init 메소드 (또는 afterPropertiesSet) 중에 시작되고 bean이 시작될 필요가 없음 (라이프 사이클 인터페이스 구현). 모든 빈을 코딩 한 방법은 초기화되지만 시작되지 않습니다.
기다리는 방법?
다음 패턴을 사용했습니다. 내 Main
스레드가 컨텍스트를 시작한 다음 다른 사람이 Main.shutdownLatch.countDown()
명령을 호출하여 컨텍스트를 닫고 종료하도록 명령 할 때까지 기다립니다. 보통 JMX 명령으로이 작업을 수행합니다.
@Override
public final void onApplicationEvent(ContextStartedEvent event) {...
어쩌면 또한 ExitCodeGenerator
:
public static CountDownLatch shutdownLatch = new CountDownLatch(1);
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("/springtests/test01.xml");
try {
shutdownLatch.await();
} finally {
context.close();
}
}
당신은 당신의 콩 ApplicationListener<ContextStartedEvent>
을 즉 구현 할 수
@Override
public int getExitCode() {
그런 다음 주요 방법이 될 수있다 :
public static void main(String[] args) {
try (ConfigurableApplicationContext context = SpringApplication.run(AppConfig.class, args)) {
context.start();
System.exit(SpringApplication.exit(context));
}
}
스프링 컨텍스트를 초기화 한 후 별도의 스레드에서 논리를 수행하십시오. 새로운 스레드가 시작될 때 조건을 기다릴 수 있고 주 스레드는 논리가 여전히 다른 스레드에서 실행되는 동안 완료됩니다. 이 스레드는 JMX를 통해 변수 값을 업데이트하여 중지 할 수 있습니다.
ApplicationContext ctx = new ClassPathXmlApplicationContext("/springtests/test01.xml");
MyClass myObj= MyClass.class.cast(ctx.getBean("myBeanName"));
ExecutorService executor= Executors.newSingleThreadExecutor();
executor.submit(new Runnable() {
@Override
public void run() {
//Update the value of this variable via jmx
while(<SomeAtomicBooleanValueCheck>)
{
myObj.doStuff();
}
}
})
누구나 -1을 설명하고 싶습니까? – Gray