@bean注解默认式以方法名作为容器id的, 可以通过@bean("person")重命名id,此时的容器id是 person。
@Configuration
public class AnnotationConfiguration {
@Bean
public Person getPseron(){
return new Person();
}
}
@Configuration
public class AnnotationConfiguration {
@Bean("person")
public Person getPseron(){
return new Person();
}
}
public class test {
@Test
public void testBean(){
//AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestController.class);
//context.getBean("registory");
AnnotationConfigApplicationContext context2 = new AnnotationConfigApplicationContext(AnnotationConfiguration.class);
String[] beanNamesForType = context2.getBeanNamesForType(Person.class);
for(String a:beanNamesForType){
System.out.println(a);//getPseron
}
}
}