Spring中@Autowired注解⽤法
@Autowired的⽤法和作⽤
这个注解就是spring可以⾃动帮你把bean⾥⾯引⽤的对象的setter/getter⽅法省略,它会⾃动帮你set/get。
<bean id="userDao" class="..."/>
<bean id="userService" class="...">
<property name="userDao">
<ref bean="userDao"/>
</property>
</bean>
这样你在userService⾥⾯要做⼀个userDao的setter/getter⽅法。
但如果你⽤了@Autowired的话,你只需要在UserService的实现类中声明即可。
@Autowired
private IUserDao userdao;
Spring@Autowired注解与⾃动装配
1  配置⽂件的⽅法
我们编写spring框架的代码时候。⼀直遵循是这样⼀个规则:所有在spring中注⼊的bean都建议定义成私有的域变量。并且要配套写上get和set⽅法。
Boss拥有Office和Car类型的两个属性:
清单3. Boss.java
package com.baobaotao;
public class Boss {
private Car car;
private Office office;
//省略get/setter
@Override
public String toString() {
return "car:" + car + "/n" + "office:" + office;
}
}
System.out.println必须实现toString⽅法
我们在Spring容器中将Office和Car声明为Bean,并注⼊到Boss Bean中:下⾯是使⽤传统XML完成这个⼯作的配置⽂
件l:
清单4. l将以上三个类配置成Bean
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/schema/beans
spring ioc注解<bean id="boss" class="com.baobaotao.Boss">
<property name="car" ref="car"/>
<property name="office" ref="office" />
</bean>
<bean id="office" class="com.baobaotao.Office">
<property name="officeNo" value="002"/>
</bean>
<bean id="car" class="com.baobaotao.Car" scope="singleton">
<property name="brand" value="红旗CA72"/>
<property name="price" value="2000"/>
</bean>
</beans>
当我们运⾏以下代码时,控制台将正确打出boss的信息:
清单5.测试类:AnnoIoCTest.java
import t.ApplicationContext;
import t.support.ClassPathXmlApplicationContext;
public class AnnoIoCTest {
public static void main(String[] args) {
String[] locations = {"l"};
ApplicationContext ctx =
new ClassPathXmlApplicationContext(locations);
Boss boss = (Boss) Bean("boss");
System.out.println(boss);
}
}
这说明Spring容器已经正确完成了Bean创建和装配的⼯作。
2  @Autowired
Spring 2.5引⼊了@Autowired注释,它可以对类成员变量、⽅法及构造函数进⾏标注,完成⾃动装配的⼯作。通过@Autowired的使⽤来消除set,get⽅法。
要实现我们要精简程序的⽬的。需要这样来处理:
*在l中加⼊:
<!--该BeanPostProcessor将⾃动对标注@Autowired的Bean进⾏注⼊-->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
Spring通过⼀个BeanPostProcessor对@Autowired进⾏解析,所以要让@Autowired起作⽤必须事先在Spring容器中声明AutowiredAnnotationBeanPostProcessor Bean。
*修改在原来注⼊spirng容器中的bean的⽅法。
在域变量上加上标签@Autowired,并且去掉相应的get和set⽅法
清单6.使⽤@Autowired注释的Boss.java
package com.baobaotao;
import org.springframework.beans.factory.annotation.Autowired;
public class Boss {
@Autowired
private Car car;
@Autowired
private Office office;
}
*在l中把原来引⽤的<porpery >标签也去掉。
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/schema/beans
<!--该BeanPostProcessor将⾃动起作⽤,对标注@Autowired的Bean进⾏⾃动注⼊-->
<bean class="org.springframework.beans.factory.annotation.
AutowiredAnnotationBeanPostProcessor"/>
<!--移除boss Bean的属性注⼊配置的信息-->
<bean id="boss" class="com.baobaotao.Boss"/>
<bean id="office" class="com.baobaotao.Office">
<property name="officeNo" value="001"/>
</bean>
<bean id="car" class="com.baobaotao.Car" scope="singleton">
<property name="brand" value="红旗CA72"/>
<property name="price" value="2000"/>
</bean>
</beans>
这样,当Spring容器启动时,AutowiredAnnotationBeanPostProcessor将扫描Spring容器中所有Bean,当发现Bean中拥
有@Autowired注释时就到和其匹配(默认按类型匹配)的Bean,并注⼊到对应的地⽅中去。
按照上⾯的配置,Spring将直接采⽤Java反射机制对Boss中的car和office这两个私有成员变量进⾏⾃动注⼊。所以对成员变量使
⽤@Autowired后,您⼤可将它们的setter⽅法(setCar()和setOffice())从Boss中删除。
当然,您也可以通过@Autowired对⽅法或构造函数进⾏标注,如果构造函数有两个⼊参,分别是bean1和bean2,@Autowired将分别寻和它们类型匹配的Bean,将它们作为CountryService (Bean1 bean1 ,Bean2 bean2)的⼊参来创建CountryService Bean。来看下⾯的代码:对⽅法
package com.baobaotao;
public class Boss {
private Car car;
private Office office;
@Autowired
public void setCar(Car car) {
this.car = car;
}
@Autowired
public void setOffice(Office office) {
this.office = office;
}
}
这时,@Autowired将查被标注的⽅法的⼊参类型的Bean,并调⽤⽅法⾃动注⼊这些Bean。⽽下⾯的使⽤⽅法则对构造函数进⾏标注:
package com.baobaotao;
public class Boss {
private Car car;
private Office office;
@Autowired
public Boss(Car car ,Office office){
this.car = car;
this.office = office ;
}
}
由于Boss()构造函数有两个⼊参,分别是car和office,@Autowired将分别寻和它们类型匹配的Bean,将它们作为Boss(Car car
,Office office)的⼊参来创建Boss Bean。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。