关于Spring框架中的AnnotationConfigApplicationContext [英] About AnnotationConfigApplicationContext in Spring framework

查看:960
本文介绍了关于Spring框架中的AnnotationConfigApplicationContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下简单的独立spring应用程序:

I have written the following simple stand-alone spring app:

package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;

@Configuration
@ComponentScan(basePackages = { "com.example" })
@PropertySource(ignoreResourceNotFound = true, value = "classpath:/application.props")
public class MainDriver {

@Autowired
private Environment env;
@Autowired
private ApplicationContext ctx;
@Autowired
private ConfigurableEnvironment cenv;

public static void main(String[] args) {

    ApplicationContext ctx = new AnnotationConfigApplicationContext(MainDriver.class);
    MainDriver l = ctx.getBean(MainDriver.class);
    System.out.println("In main() the ctx is " + ctx);
    l.test();

}

public void test() {
    System.out.println("hello");
    System.out.println("env is " + env);
    System.out.println("cenv is " + cenv);
    System.out.println("ctx is" + ctx);
}
}

我了解到,在main()中,我们正在创建一个新的Application上下文,然后创建Bean并最终调用test()方法.

I understood that within main() we are Creating a new Application context and then creating Bean and eventually calling test() method.

我无法理解EnvironmentApplicationContextConfigurableEnvironment如何自动连接(以及连接到哪个bean?)

What I am not able to understand how come Environment , ApplicationContext and ConfigurableEnvironment get Autowired (and to which bean?)

我观察到自动连线的ctx获得的上下文是在main()中初始化的.

I observed that the autowired ctx gets the context which is initialise in main().

基本上无法理解它们是如何自动接线的(以及它们的设置是什么?)

Basically not able to understand how these gets autowired by itself (and what they are set to?)

任何有助于理解这一点的方法都会有很大帮助.

Any help in understanding this would be of great help.

推荐答案

任何用@Configuration注释的类也是Spring bean.这意味着MainDirver也是在创建AnnotationConfigApplicationContext期间将创建的spring bean.

Any class that is annotated with @Configuration is also a Spring bean. That means the MainDirver is also a spring bean that will be created during creating AnnotationConfigApplicationContext.

在创建MainDirver bean之后,如果该字段用@Autowird注释,那么Spring将把其他bean注入其字段中.因此,在这种情况下,EnvironmentApplicationContextConfigurableEnvironment都注入了这个MainDirver bean.

And after the MainDirver bean is created , Spring will then inject other beans into its field if that field is annotated with @Autowird. So in this case , Environment , ApplicationContext, and ConfigurableEnvironment are all injected this MainDirver bean.

P.S.您可以认为EnvironmentApplicationContextConfigurableEnvironment是必须创建的Spring基础结构bean,即使您未使用@Configuration@Service@Bean等定义它们也是如此.

P.S. You can think that Environment , ApplicationContext, and ConfigurableEnvironment are kind of Spring infrastructure beans that must be created even though you do not define them using @Configuration , @Service , @Bean and etc.

这篇关于关于Spring框架中的AnnotationConfigApplicationContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆