java Spring 应用程序最简单的 main() 方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12607766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the simplest main() method for a Spring application?
提问by Suzan Cioc
I wrote the following, but it exits immediately:
我写了以下内容,但它立即退出:
public static void main(String[] args) {
new ClassPathXmlApplicationContext("/springtests/test01.xml");
}
test01.xml
contains multiple beans with some threads (daemons) which connects to the network etc.
test01.xml
包含多个带有一些线程(守护进程)的 bean,这些线程连接到网络等。
How to wait or something?
如何等待或什么?
回答by RNJ
All you are doing is creating an object here. You need to retrieve the beans and then use them. For example
您所做的就是在此处创建一个对象。您需要检索 bean,然后使用它们。例如
ApplicationContext ctx = new ClassPathXmlApplicationContext("/springtests/test01.xml");
MyClass myObj= MyClass.class.cast(ctx.getBean("myBeanName"));
myObj.doStuff();
If you want more help then post what is in your test01.xml
如果您需要更多帮助,请发布 test01.xml 中的内容
回答by Guido Simone
Although I agree with most of the recommendations in the other answers, I think your main method is ok as it is. The only thing you need to change is make at least one of your worker threads non-daemon.
尽管我同意其他答案中的大部分建议,但我认为您的主要方法是可以的。您唯一需要更改的是至少让您的一个工作线程成为非守护进程。
From Thread.setDaemon Java doc:
来自 Thread.setDaemon Java 文档:
The Java Virtual Machine exits when the only threads running are all daemon threads.
当唯一运行的线程都是守护线程时,Java 虚拟机退出。
Also make sure all your threads are started during the init method (or afterPropertiesSet) of the bean and does not require a that the bean be started (implementing the life cycle interface). The way you have it coded all your beans will be initialized but not started.
还要确保在 bean 的 init 方法(或 afterPropertiesSet)期间启动所有线程,并且不需要启动 bean(实现生命周期接口)。您对所有 bean 进行编码的方式将被初始化但不会启动。
回答by Gray
How to wait or something?
如何等待或什么?
I've used something like the following pattern. My Main
thread starts the context and then waits for someone else to call Main.shutdownLatch.countDown()
command to tell it to close the context and exit. I usually do this with a JMX command.
我使用了类似以下模式的东西。我的Main
线程启动上下文,然后等待其他人调用Main.shutdownLatch.countDown()
命令来告诉它关闭上下文并退出。我通常使用 JMX 命令执行此操作。
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();
}
}
回答by Greg Allen
You could have your beans implement ApplicationListener<ContextStartedEvent>
i.e:
你可以让你的 bean 实现,ApplicationListener<ContextStartedEvent>
即:
@Override
public final void onApplicationEvent(ContextStartedEvent event) {...
and maybe also ExitCodeGenerator
:
也许还有ExitCodeGenerator
:
@Override
public int getExitCode() {
Then your main method could be:
那么你的主要方法可能是:
public static void main(String[] args) {
try (ConfigurableApplicationContext context = SpringApplication.run(AppConfig.class, args)) {
context.start();
System.exit(SpringApplication.exit(context));
}
}
回答by Sankalp
Do perform the logic in separate thread after initializing spring context. As the new thread starts that can wait on a condition and main thread will finish while your logic still running in another thread. That thread can be stopped by updating variable value via JMX.
初始化 spring 上下文后,请在单独的线程中执行逻辑。当新线程开始时,可以等待一个条件,主线程将完成,而您的逻辑仍在另一个线程中运行。可以通过 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();
}
}
})
回答by matt b
You can call context.registerShutdownHook()
to ensure that the beans in the applicationContext are shut down properly, but you still need to ensure that the main thread does not exit if the applicationContext only starts daemon threads.
可以调用context.registerShutdownHook()
确保applicationContext中的bean正常关闭,但是如果applicationContext只启动守护线程,还是需要保证主线程不退出。
A better strategy would be for main()
to retrieve some bean from the context that it can call bean.startApp()
on, to kick things off - or have at least one of the threads started in the context be non-daemon.
更好的策略是main()
从它可以调用的上下文中检索一些 bean bean.startApp()
,以启动事物 - 或者让至少一个在上下文中启动的线程是非守护进程。