Java 关于Spring框架中的AnnotationConfigApplicationContext
我编写了以下简单的独立spring应用程序:Java 关于Spring框架中的AnnotationConfigApplicationContext,java,spring,spring-ioc,Java,Spring,Spring Ioc,我编写了以下简单的独立spring应用程序: package com.example; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; impor
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()中,我们正在创建一个新的应用程序上下文,然后创建Bean并最终调用test()方法
我无法理解的是,Environment
、ApplicationContext
和ConfigurableEnvironment
是如何自动连接到哪个bean的
我观察到,自动连线的ctx
获取在main()中初始化的上下文
基本上无法理解它们是如何自动连接的(以及它们设置为什么?)
理解这一点的任何帮助都会有很大帮助。任何用
@Configuration
注释的类也是Springbean。这意味着maindirever
也是一个Springbean,它将在创建AnnotationConfigApplicationContext
期间创建
在创建maindirever
bean之后,如果该字段被@Autowird
注释,那么Spring将向其字段中注入其他bean。因此,在本例中,Environment
、ApplicationContext
和ConfigurableEnvironment
都被注入到这个主驱动程序bean中
另外,您可以认为
Environment
、ApplicationContext
和ConfigurableEnvironment
是一种必须创建的Spring基础结构bean,即使您没有使用@Configuration
、@Service
定义它们,@Bean
等。任何用@Configuration
注释的类也是Springbean。这意味着maindirever
也是一个Springbean,它将在创建AnnotationConfigApplicationContext
期间创建
在创建maindirever
bean之后,如果该字段被@Autowird
注释,那么Spring将向其字段中注入其他bean。因此,在本例中,Environment
、ApplicationContext
和ConfigurableEnvironment
都被注入到这个主驱动程序bean中
另外,您可以认为
环境
、应用上下文
和可配置环境
是一种必须创建的Spring基础设施Bean,即使您没有使用@Configuration
、@Service
、@Bean
等来定义它们。谢谢您的回答。但是,我仍然怀疑ApplicationContext自动连接到哪种类型?这部分我还不能理解。@奇怪的是,ApplicationContext
的类型是AnnotationConfigApplicationContext
,在这种情况下,您使用这种类型创建它。如果在接口上注释一个@Autowired
,它将向它注入实现相同接口的所有bean。因此,您甚至可以通过在其上添加注释的@Autowired
来插入一个列表
,然后所有实现SomeInterface
的bean都将被插入此列表中。谢谢您的回答。但是,我仍然怀疑ApplicationContext自动连接到哪种类型?这部分我还不能理解。@奇怪的是,ApplicationContext
的类型是AnnotationConfigApplicationContext
,在这种情况下,您使用这种类型创建它。如果在接口上注释一个@Autowired
,它将向它注入实现相同接口的所有bean。因此,您甚至可以通过在其上添加注释的@Autowired
来注入一个列表
,然后实现SomeInterface
的所有bean都将被注入该列表