package com.wang.pojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import javax.annotation.Resource;
public class People {
// @Resource
@Resource(name = "cat2")
// private Cat cat;
//
// @Resource
// private Dog dog;
// @Autowired
// 直接在属性上使用即可,也可以在set方式上使用
// 使用Autowired可以不用编写Set方法,前提是自动装配的属性在IOC(spring)容器中存在,且符合名字byName
@Autowired(required = false) //如果显示定义了Autowired的required属性为false,说明这个对象可以为空,否则不允许为空。
private Cat cat;
@Autowired
@Qualifier(value = "dog2") //可以使用这种方式去配置@Autowired的使用,指定一个唯一的bean对象注入。
private Dog dog;
private String name;
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "People{" +
"cat=" + cat +
", dog=" + dog +
", name='" + name + '\'' +
'}';
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启注解的支持-->
<context:annotation-config/>
<bean id="cat" class="com.wang.pojo.Cat"/>
<bean id="cat2" class="com.wang.pojo.Cat"/>
<bean id="dog" class="com.wang.pojo.Dog"/>
<bean id="dog2" class="com.wang.pojo.Dog"/>
<bean id="people" class="com.wang.pojo.People"/>
<!-- <bean id="cat111" class="com.wang.pojo.Cat"/>-->
<!--<!– <bean id="dog" class="com.wang.pojo.Dog"/>–>-->
<!-- <bean id="dog" class="com.wang.pojo.Dog"/>-->
<!-- <bean id="people" class="com.wang.pojo.People">-->
<!-- <property name="name" value="小青蛙"/>-->
<!-- <property name="cat" ref="cat"/>-->
<!-- <property name="dog" ref="dog"/>-->
<!-- </bean>-->
<!--1、byName自动装配 会自动在容器的上下文中查找,和自己对象set方法后面对应的beanid-->
<!-- <bean id="people" class="com.wang.pojo.People" autowire="byName">-->
<!-- <property name="name" value="小青蛙"/>-->
<!-- </bean>-->
<!--2、byType自动装配 会自动在容器的上下文中查找,和自己对象属性类型相同的beanid
id可省略 class要唯一
-->
<!-- <bean id="people" class="com.wang.pojo.People" autowire="byType">-->
<!-- <property name="name" value="小青蛙"/>-->
<!-- </bean>-->
<!--小结:
byName的时候,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致。
byType的时候,需要保证所有的bean的class唯一,并且这个bean需要和自动注入的属性的类型一致。
-->
</beans>
import com.wang.pojo.People;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
People people = context.getBean("people", People.class);
people.getDog().shout(); // wang--
people.getCat().shout(); // miao--
}
}
package com.wang.pojo;
public class Cat {
public void shout(){
System.out.println("miao--");
}
}
import com.wang.pojo.People;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
People people = context.getBean("people", People.class);
people.getDog().shout(); // wang--
people.getCat().shout(); // miao--
}
}