@Autowired与之前的autowire="byType/byName"相同
如:<bean name="t" class="com.briup.bean.Teacher" autowire="byType">
新建3个类Boss,Car,Office
///Boss类///
public class Boss {
private String name;
private Car car;//用了Car类
private Office office;//用了Office类
public Boss(){
}
public Boss(String name, Car car, Office office) {
this.name = name;
this.car = car;
this.office = office;
}
public Boss( Car car, Office office) {
this.car = car;
this.office = office;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public Office getOffice() {
return office;
}
public void setOffice(Office office) {
this.office = office;
}
}
///Car类///
public class Car {
private double price;
private String name;
public Car(){
}
public Car(double price, String name) {
this.price = price;
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
///Office类///
public class Office {
private String num = "001";
public Office(){
}
public Office(String num) {
this.num = num;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
}
之前的annotation.xml文件
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<bean name="car" class="com.briup.ioc.annotation.Car">
<property name="name" value="兰博基尼"></property>
<property name="price" value="100000"></property>
</bean>
<bean name="office" class="com.briup.ioc.annotation.Office">
<property name="num" value="001"></property>
</bean>
<bean name="boss" class="com.briup.ioc.annotation.Boss">
<property name="name" value="tom"></property>
<property name="car" ref="car"></property>
<property name="office" ref="office"></property>
</bean>
</beans>
@Autowired知识点
特点:`@Autowired先按照byType,再按照byName`
1) @Autowired使用后需要在xml文件加入以下配置才能生效:`<context:annotation-config/>`
2) @Autowired注解可以写在`成员变量、setter方法、构造器函数上面(一般写在变量名上)`
3) @Autowired默认按照`byType`匹配的方式进行注入,如果没有一个bean的类型是匹配的则会抛异常,
如果有`多个bean的`类型都匹配成功了,那么再按`byName`方式进行选择
4) @Autowired如果最终匹配不成功(注意一定是一个都没有找到的情况,即没有找到)则会抛出异常,但是如果
设置为`@Autowired(required=false)`,则最终即使是匹配不成功,也不会抛出异常。
(required=false是在连一个都找不到的情况下使用),当找到一个就会报错
5) @Autowired可以结合@Qualifier("beanName")来使用,则可以达到byName的效果
byType注入
现在的annotation.xml文件,没有手动注入Car,但是在Boss.java里面用了注解
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<bean name="car" class="com.briup.ioc.annotation.Car">
<property name="name" value="兰博基尼"></property>
<property name="price" value="100000"></property>
</bean>
<bean name="office" class="com.briup.ioc.annotation.Office">
<property name="num" value="001"></property>
</bean>
<bean name="boss" class="com.briup.ioc.annotation.Boss">
<property name="name" value="tom"></property>
<!-- 没有注入Car,但是在Boss.java里面用了注解-->
<!-- 只是注入了office -->
<property name="office" ref="office"></property>
</bean>
<!-- 要用@Autowired必须要加 context:annotation-config-->
<context:annotation-config/>
</beans>
在Boss.java里面用了注解@Autowired
import org.springframework.beans.factory.annotation.Autowired;
public class Boss {
private String name;
//将@Autowired或者放到底下的setCar()方法上面
@Autowired
private Car car;//用了Car类
private Office office;//用了Office类
.......
//@Autowired //一般放在变量名上
public void setCar(Car car) {
this.car = car;
}
.......
}
测试类:
@Test
//知识点: ioc中的注解
public void ioc_annotation() {
try {
String path = "com/briup/ioc/annotation/annotation.xml";
ClassPathXmlApplicationContext container = new ClassPathXmlApplicationContext(path);
Boss boss = (Boss) container.getBean("boss");
System.out.println(boss);
System.out.println(boss.getOffice());
System.out.println(boss.getCar());
} catch (Exception e) {
e.printStackTrace();
}
}
结果:是能够拿到Car的
byName注入
当同时出现多个相同类型的,就会按照byName注入
测试:
@Test
//知识点: ioc中的注解
public void ioc_annotation() {
try {
String path = "com/briup/ioc/annotation/annotation.xml";
ClassPathXmlApplicationContext container = new ClassPathXmlApplicationContext(path);
Boss boss = (Boss) container.getBean("boss");
System.out.println(boss);
System.out.println(boss.getOffice());
System.out.println(boss.getCar().getName());//获取名字
} catch (Exception e) {
e.printStackTrace();
}
}
结果:
@Autowired(required=false)
在xml里面根本就没有Car的相关配置,但是要在Boss.java类里面注入
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- 没有Car的任何人配置,但是要在Boss.java类里面注入 -->
<bean name="office" class="com.briup.ioc.annotation.Office">
<property name="num" value="001"></property>
</bean>
<bean name="boss" class="com.briup.ioc.annotation.Boss">
<property name="name" value="tom"></property>
<property name="office" ref="office"></property>
</bean>
<context:annotation-config/>
</beans>
用@Autowired(required=false),就不会报错
public class Boss {
private String name;
//required=false是连一个对应的对象都没有找到,注入不成功不报错,若找到多个依旧会报错
//@Autowired(required=false)
private Car car;//在xml里面没有配置car
private Office office;
}
测试类:
@Test
//知识点: ioc中的注解
public void ioc_annotation() {
try {
String path = "com/briup/ioc/annotation/annotation.xml";
ClassPathXmlApplicationContext container = new ClassPathXmlApplicationContext(path);
Boss boss = (Boss) container.getBean("boss");
System.out.println(boss);
System.out.println(boss.getOffice());
System.out.println(boss.getCar());//获取Car
} catch (Exception e) {
e.printStackTrace();
}
}
结果:不报错
@Autowired结合@Qualifier(“beanName”)
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- 有两个Car类型的 -->
<bean name="car1" class="com.briup.ioc.annotation.Car">
<property name="name" value="兰博基尼"></property>
<property name="price" value="2100000"></property>
</bean>
<bean name="car2" class="com.briup.ioc.annotation.Car">
<property name="name" value="奥迪"></property>
<property name="price" value="100000"></property>
</bean>
<bean name="office" class="com.briup.ioc.annotation.Office">
<property name="num" value="001"></property>
</bean>
<bean name="boss" class="com.briup.ioc.annotation.Boss">
<property name="name" value="tom"></property>
<property name="office" ref="office"></property>
</bean>
<context:annotation-config/>
</beans>
@Autowired可以结合@Qualifier(“beanName”)来使用,则可以达到byName的效果
public class Boss {
private String name;
@Autowired
@Qualifier("car2")//注入的时name为car2的Car类型
private Car car;
private Office office;
......
......
}
测试类:
@Test
//知识点: ioc中的注解
public void ioc_annotation() {
try {
String path = "com/briup/ioc/annotation/annotation.xml";
ClassPathXmlApplicationContext container = new ClassPathXmlApplicationContext(path);
Boss boss = (Boss) container.getBean("boss");
System.out.println(boss);
System.out.println(boss.getOffice());
System.out.println(boss.getCar().getName());//获取Car名字
} catch (Exception e) {
e.printStackTrace();
}
}
结果: