我在我的代码库中找到了这段代码。实际上是类:
package my.services.config;
@Configuration
@ImportResource("classpath:spring/*.xml")
@ComponentScan("my.services.jms.server")
public class MyServicesConfiguration {
@Bean
public ApplicationLifecycle lifecycle() {
return new MyServicesLifecycle();
}
}
我试图理解: 因此,它在启动之前/启动时使用所有 spring/*.xml 文件/bean,然后将 ApplicationLifecycle bean 注入(inject)到 spring 上下文中(以及来自 spring/*xml 的其他 bean 和来自 'my.services.jms.server 的 bean) ' 包)。所以,最终我们将拥有一个包含所有 bean 的全局上下文(?)
问题:如何启动这个应用程序(如果据我所知,这个类只是应用程序的一个入口点)?
这将是一些 main(String[] args) {} 函数,可以通过“my.services.config”路径启动它,比方说,作为参数。
最佳答案
So, in the end we gonna have one global context with all beans (?)
没错。从Spring的角度来看,@Configuration类只是定义bean的一种不同方式,相当于XML。 Java 配置和 XML 配置稍后会合并并同等对待。
这就是如何使用 main()
启动上下文:
ApplicationContext ctx =
new AnnotationConfigApplicationContext(MyServicesConfiguration.class);
及以后:
ApplicationLifecycle applicationLifecycle =
ctx.getBean(ApplicationLifecycle.class);
关于java - 通过@Configuration启动Spring应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12922723/