简体   繁体   中英

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). So, in the end we gonna have one global context with all beans (?)

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.

So, in the end we gonna have one global context with all beans (?)

That's right. From Spring perspective @Configuration class is just a different way to define beans, equivalent to XML. Both Java configuration and XML configuration is merged and treated equally later.

And this is how you can start you context from withing main() :

ApplicationContext ctx = 
    new AnnotationConfigApplicationContext(MyServicesConfiguration.class);

and later:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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