六星教育 - Spring源码分析1909
默认单例,注入为同一个对象
- @Bean配置类注入:bean类,配置类()
@Configuration //声明为配置类
public class TestConfig {
//默认方法名
@Bean() //要注入的Bean
TestBean testBean(){
System.out.println("给容器添加bean");
TestBean testBean = new TestBean();
return testBean;
}
}
@org.junit.Test
public void test07(){
ApplicationContext app = new AnnotationConfigApplicationContext(TestConfig.class);
//
TestBean testBean = (TestBean) app.getBean("testBean");
System.out.println(testBean);
}
- 包扫描注入
// 该类为配置并注入,同时扫描@ComponentScan下的所有bean并注入
@Configuration
@ComponentScan(value = "com.stastar.fame.exp03.beans")
public class FameConfig03 {
}
- FactoryBean:
网友评论