简体   繁体   English

通过@Configuration启动Spring应用程序

[英]Startup the Spring application by @Configuration

I found this piece of code in my codebase. 我在我的代码库中找到了这段代码。 Actually the class: 实际上是该类:

package my.services.config;

@Configuration
@ImportResource("classpath:spring/*.xml")
@ComponentScan("my.services.jms.server")
public class MyServicesConfiguration {
    @Bean
    public ApplicationLifecycle lifecycle() {
        return new MyServicesLifecycle();
    }   
}

I'm trying to understand: So, it uses all spring/*.xml files/beans before/while staring up, then it injects ApplicationLifecycle bean into the spring context (along with other beans from spring/*xml and from beans from 'my.services.jms.server' packages). 我试图理解:因此,它在启动之前/期间使用了所有spring / *。xml文件/ bean,然后将ApplicationLifecycle bean注入spring上下文(以及spring / * xml中的其他bean和' my.services.jms.server'软件包)。 So, in the end we gonna have one global context with all beans (?) 因此,最后,我们将对所有bean具有一个全局上下文(?)

The question: How does it possible to launch this application (if, as I understand this class is only one entry point to the app) ? 问题:如何启动此应用程序(如果据我了解,此类仅是该应用程序的一个入口点)?

It's gonna be some main(String[] args) {} function that would able to launch it by 'my.services.config' path, let's say, as an argument. 它将是一些main(String [] args){}函数,它可以通过'my.services.config'路径作为参数启动。

So, in the end we gonna have one global context with all beans (?) 因此,最后,我们将对所有bean具有一个全局上下文(?)

That's right. 那就对了。 From Spring perspective @Configuration class is just a different way to define beans, equivalent to XML. 从Spring的角度来看, @Configuration类只是定义bean的另一种方式,等效于XML。 Both Java configuration and XML configuration is merged and treated equally later. Java配置和XML配置将合并并在以后同等对待。

And this is how you can start you context from withing main() : 这就是从main()开始上下文的方式:

ApplicationContext ctx = 
    new AnnotationConfigApplicationContext(MyServicesConfiguration.class);

and later: 然后:

ApplicationLifecycle applicationLifecycle = 
    ctx.getBean(ApplicationLifecycle.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM