1.定义了个对象:Animal
public class Animal {
private String name="tom";
public Animal() {
}
public Animal(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2.在另一个配置bean里引入animal(注意这里是引用的不是容器里的animal,因为还没有注入容器对象)
@Configuration
public class ConfigPerson {
private Animal animal;
@Bean
Person person(){
return new Person(animal);
}
}
3.测试:结果为空指针
4.将其这样写,写在参数里,这样和@Resource注入是一样的,并且至少配置一个animal为名的bean,如果配置了xml和@Bean的同名bena,则会优先展示xml的配置
<bean id="animal" class="com.example.test.Animal" >
<constructor-arg name="name" value="rose"></constructor-arg>
</bean>
-----------------------------------------------------------------
@Configuration
@Bean
Person person(Animal animal){
return new Person(animal);
}
@Bean
Animal animal(){
return new Animal("xxx");
}
测试:结果为rose,将xml的animal bean注释,结果为xxx
@org.junit.Test
public void test2(){
System.out.println(animal.getName());
}
5.再次修改
<bean id="animal" class="com.example.test.Animal" >
<constructor-arg name="name" value="rose"></constructor-arg>
</bean>
@Configuration
public class ConfigPerson {
@Bean
Person person(Animal animal2){
return new Person(animal2);
}
@Bean
Animal animal2(){
return new Animal("xxx");
}
}
6.测试结果为xxx
7.再次修改
<bean id="animal" class="com.example.test.Animal" >
<constructor-arg name="name" value="rose"></constructor-arg>
</bean>
@Configuration
public class ConfigPerson {
@Bean
Person person(Animal animal){
return new Person(animal);
}
@Bean
Animal animal2(){
return new Animal("xxx");
}
}
8.测试结果为rose
结论:参数注入类似于@Rsource 根据名称选择注入bean对象
1.定义了个对象HelloServcie
public class HelloService {
private String name;
public HelloService(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
2.配置两个同一类对象的不同配置的bean
<bean id="helloService" class="com.example.entity.HelloService">
<constructor-arg name="name" value="helloService"></constructor-arg>
</bean>
<bean id="helloService2" class="com.example.entity.HelloService">
<constructor-arg name="name" value="helloService2"></constructor-arg>
</bean>
3.注入测试
将helloService2注入同一个类对象
@Resource
@Qualifier("helloService2")
private HelloService h2;
@Resource
@Qualifier("helloService2")
private HelloService h;
@org.junit.Test
public void test(){
System.out.println(h==h2);
System.out.println(h.getName());
System.out.println(h2.getName());
h.ss();
}
结果为:
true
helloService2
helloService2
将helloService注入h,helloService2注入h2
@Resource
@Qualifier("helloService2")
private HelloService h2;
@Resource
@Qualifier("helloService")
private HelloService h;
@org.junit.Test
public void test(){
System.out.println(h==h2);
System.out.println(h.getName());
System.out.println(h2.getName());
h.ss();
}
结果为:
false
helloService
helloService2
提示:
@Resource
@Resource默认按byName自动注入。
既不指定name属性,也不指定type属性,则自动按byName方式进行查找。如果没有找到符合的bean,则回退为一个原始类型进行进行查找,如果找到就注入。
只是指定了@Resource注解的name,则按name后的名字去bean元素里查找有与之相等的name属性的bean。
只指定@Resource注解的type属性,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常。
@Autowired
@Autowired默认先按byType进行匹配,如果发现找到多个bean,则又按照byName方式进行匹配,如果还有多个,则报出异常。
1.注入person对象一种是声明在类变量中,一种是在方法里声明,类变量中必须用@Resource且只读取xml的配置值,而方法体中则是不需要@Resource的
2.同名bean 默认优先读取xml,其次是@bean的