@Configuration
@ComponentScan("com")
@ImportResource({"applicationContext2.xml","applicationContext.xml"})
public class SpringConfig2 {
@Bean
public Cat cat(){
return new Cat();
}
}
@Configuration(proxyBeanMethods = false)
@ComponentScan("com")
@ImportResource({"applicationContext2.xml","applicationContext.xml"})
public class SpringConfig2 {
@Bean
public Cat cat(){
return new Cat();
}
}
主方法中
public class app3 {
public static void main(String[] args) {
ApplicationContext app=new AnnotationConfigApplicationContext(SpringConfig2.class);
String[] names = app.getBeanDefinitionNames();
for (String name : names) {
System.out.println(name);
}
System.out.println("--------");
System.out.println(app.getBean("springConfig2"));
SpringConfig2 springConfig2 = app.getBean("springConfig2", SpringConfig2.class);
System.out.println(springConfig2.cat());
System.out.println(springConfig2.cat());
System.out.println(springConfig2.cat());
}
}
运行结果
产生的是普通对象,每一次调用方法都会new一个新的对象前提是这个方法是被bean管理的对象。
值为true时
不写或写true时
@Configuration(proxyBeanMethods = true)
@ComponentScan("com")
@ImportResource({"applicationContext2.xml","applicationContext.xml"})
public class SpringConfig2 {
@Bean
public Cat cat(){
return new Cat();
}
}
import com.service.TestBean1;
import com.service.TestBean2;
import org.springframework.context.annotation.Import;
@Import({TestBean1.class, TestBean2.class})
public class springConfig4 {
}
创建测试的类
public class TestBean1 {
}
public class testBean2 {
}
主类上
public class app4 {
public static void main(String[] args) {
ApplicationContext app=new AnnotationConfigApplicationContext(springConfig4.class);
String[] names = app.getBeanDefinitionNames();
for (String name : names) {
System.out.println(name);
}
}
}
运行结果
全路径名,加入的类即使没有被spring管理也可以产生bean。
第五种加载bean方式-registerBean
使用上下文对象在容器初始化后注入bean
在创建完上下文对象的时候可以加载bean
只能使用AnnotationConfigApplicationContext获取上下文对象
public class app5 {
public static void main(String[] args) {
AnnotationConfigApplicationContext app=new AnnotationConfigApplicationContext(springConfig5.class);
//加载完成后
app.registerBean("CaiDog", Dog.class,1 );
app.registerBean("CaiDog", Dog.class,2);
app.registerBean("CaiDog", Dog.class,3 );
String[] names = app.getBeanDefinitionNames();
for (String name : names) {
System.out.println(name);
}
System.out.println("------");
System.out.println(app.getBean(Dog.class));
}
}
Dog类下
@Component
public class Dog {
int age;
public Dog(){}
public Dog(int age){
this.age=age;
}
@Override
public String toString() {
return "Dog{" +
"age=" + age +
'}';
}
}
运行结果 ,当有多个的时候,且bean的名字一致时,以最后的为准。
或者直接使用
app.register(Cat.class);
也可以快速的加载一个bean
第六种加载bean方式-实现ImportSelector接口
这个接口有许多方法用来判定
导入实现了ImportSelector接口的类,实现对导入源的编程式处理
public class MyImportSelector implements ImportSelector {
public String[] selectImports(AnnotationMetadata annotationMetadata) {//AnnotationMetadata 注解的源数据
// 做判定条件,是否有这个注解
boolean flag = annotationMetadata.hasAnnotation("org.springframework.context.annotation.Configuration");
if (flag){
return new String[]{"com.service.Dog"};
}
return new String[]{"com.service.Cat"};
}
}
@Import({MyImportSelector.class})
public class SpringConfig6 {
}
public class app6 {
public static void main(String[] args) {
ApplicationContext app=new AnnotationConfigApplicationContext(SpringConfig6.class);
String[] names = app.getBeanDefinitionNames();
for (String name : names) {
System.out.println(name);
}
}
}
@Configuration
@ComponentScan("com")
@ImportResource({"applicationContext2.xml","applicationContext.xml"})
public class SpringConfig2 {
@Bean
public Cat cat(){
return new Cat();
}
}
@Configuration(proxyBeanMethods = false)
@ComponentScan("com")
@ImportResource({"applicationContext2.xml","applicationContext.xml"})
public class SpringConfig2 {
@Bean
public Cat cat(){
return new Cat();
}
}
主方法中
public class app3 {
public static void main(String[] args) {
ApplicationContext app=new AnnotationConfigApplicationContext(SpringConfig2.class);
String[] names = app.getBeanDefinitionNames();
for (String name : names) {
System.out.println(name);
}
System.out.println("--------");
System.out.println(app.getBean("springConfig2"));
SpringConfig2 springConfig2 = app.getBean("springConfig2", SpringConfig2.class);
System.out.println(springConfig2.cat());
System.out.println(springConfig2.cat());
System.out.println(springConfig2.cat());
}
}
运行结果
产生的是普通对象,每一次调用方法都会new一个新的对象前提是这个方法是被bean管理的对象。
值为true时
不写或写true时
@Configuration(proxyBeanMethods = true)
@ComponentScan("com")
@ImportResource({"applicationContext2.xml","applicationContext.xml"})
public class SpringConfig2 {
@Bean
public Cat cat(){
return new Cat();
}
}
import com.service.TestBean1;
import com.service.TestBean2;
import org.springframework.context.annotation.Import;
@Import({TestBean1.class, TestBean2.class})
public class springConfig4 {
}
创建测试的类
public class TestBean1 {
}
public class testBean2 {
}
主类上
public class app4 {
public static void main(String[] args) {
ApplicationContext app=new AnnotationConfigApplicationContext(springConfig4.class);
String[] names = app.getBeanDefinitionNames();
for (String name : names) {
System.out.println(name);
}
}
}
运行结果
全路径名,加入的类即使没有被spring管理也可以产生bean。
第五种加载bean方式-registerBean
使用上下文对象在容器初始化后注入bean
在创建完上下文对象的时候可以加载bean
只能使用AnnotationConfigApplicationContext获取上下文对象
public class app5 {
public static void main(String[] args) {
AnnotationConfigApplicationContext app=new AnnotationConfigApplicationContext(springConfig5.class);
//加载完成后
app.registerBean("CaiDog", Dog.class,1 );
app.registerBean("CaiDog", Dog.class,2);
app.registerBean("CaiDog", Dog.class,3 );
String[] names = app.getBeanDefinitionNames();
for (String name : names) {
System.out.println(name);
}
System.out.println("------");
System.out.println(app.getBean(Dog.class));
}
}
Dog类下
@Component
public class Dog {
int age;
public Dog(){}
public Dog(int age){
this.age=age;
}
@Override
public String toString() {
return "Dog{" +
"age=" + age +
'}';
}
}
运行结果 ,当有多个的时候,且bean的名字一致时,以最后的为准。
或者直接使用
app.register(Cat.class);
也可以快速的加载一个bean
第六种加载bean方式-实现ImportSelector接口
这个接口有许多方法用来判定
导入实现了ImportSelector接口的类,实现对导入源的编程式处理
public class MyImportSelector implements ImportSelector {
public String[] selectImports(AnnotationMetadata annotationMetadata) {//AnnotationMetadata 注解的源数据
// 做判定条件,是否有这个注解
boolean flag = annotationMetadata.hasAnnotation("org.springframework.context.annotation.Configuration");
if (flag){
return new String[]{"com.service.Dog"};
}
return new String[]{"com.service.Cat"};
}
}
@Import({MyImportSelector.class})
public class SpringConfig6 {
}
public class app6 {
public static void main(String[] args) {
ApplicationContext app=new AnnotationConfigApplicationContext(SpringConfig6.class);
String[] names = app.getBeanDefinitionNames();
for (String name : names) {
System.out.println(name);
}
}
}