在spring2.5 版本,没有提供基本类型属性注入 ,但是spring3.0引入注解@Value
所以在Spring3.0中属性的注入只可以这么写。
Car car是个对象,所以与普通的注入不同,要用spEl表达式才能写。
但是Spring2.5中并没有Value这个注解的,那么它怎么解决属性注入呢?
单用@AutoWired的话是用类型去匹配注入的,但是如果用@Autowired + @Qualifier 就是用指定注入Bean的id
今天看代码老遇到@Autowired ,起初一直不懂这个玩意到底干吗用的,后来问了下度娘,大概懂了点,mark一下。
@Autowired是Spring2.5
中引入的注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。通过它的使用来消除 set
,get方法,帮我们把bean里面引用的对象的setter/getter方法省略,也就是说它会自动帮我们set/get。我们在编写spring
框架的代码时候,一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量。并且要配套写上 get 和
set方法。虽然可以通过eclipse等工具来自动生成setter/getter,但会造成代码的累赘,引入了 @Autowired
注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作,这样一来就会精简代码。
看下面一个例子:Student有name和age两个属性:
package com.lyc;
public class Student {
private Name name;
private Age age;
//此处省略 getter/setter
@Override
public String toString() {
return "name:" + name + "\n" + "age:" + age;
}
}
我们在 Spring 容器中将 Name 和 Age 声明为 Bean,并注入到 Student Bean 中:下面是使用传统 XML 完成这个工作的配置文件 beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean class="com.lyc.Student">
<property name="name" ref="name"/>
<property name="age" ref="age" />
</bean>
<bean class="com.lyc.Age">
<property name="age" value="22"/>
</bean>
<bean scope="singleton">
<property name="dudian" value=" 成都"/>
<property name="xiyou" value="2000"/>
</bean>
</beans>
现在我们引入@Autowired 注释,首先得在在applicationContext.xml中加入:
<!-- 该 BeanPostProcessor 将自动对标注 @Autowired 的 Bean 进行注入 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
使用 @Autowired 注释的 Student.javapackage com.lyc;
public class Student {
@Autowired
private Name name;
@Autowired
private Age age;
.........
}
修改applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的
Bean 进行自动注入 -->
<bean
class="org.springframework.beans.factory.annotation.
AutowiredAnnotationBeanPostProcessor"/>
<!-- 移除 student Bean 的属性注入配置的信息
-->
<bean
class="com.lyc.Student"/>
<bean
class="com.lyc.Age">
<property name="age"
value="22"/>
</bean>
<bean
scope="singleton">
<property name="dudian" value="
成都"/>
<property name="xiyou"
value="2000"/>
</bean>
</beans>
spring提供了@Autowired Annotation来指定自动装配,使用@Autowired可以标注setter方法、普通方法、Field、函数形参和构造器等。
例如下代码:
package cn.zj.qiao.spring.beans; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import cn.zj.qiao.spring.interfaces.Axe; import cn.zj.qiao.spring.interfaces.Person; @Component public class Chinese implements Person { private Axe axe; @Autowired public void setAxe(Axe axe){ this.axe = axe; } public Axe getAxe(){ return axe; } @Override public void useAxe() { System.out.println(axe.chop()); } }
上面的代码使用@Autowired 指定setAxe()方法进行自动装配,spring将会自动搜索容器中类型为Axe的Bean实例,并将该Bean实例作为setAxe()方法的参数 传入,此时spring默认的装配策略为byType。同样的@Autowired可以修饰普通的方法,Field和构造器等,且其默认的装配策略均为 byType类型的装配。
为了实现精确的自动装配,spring提供了@Qualifier Annotation,通过使用@Qualifier,允许根据Bean的标识来指定自动装配,如下代码所示:
package cn.zj.qiao.spring.beans; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; import cn.zj.qiao.spring.interfaces.Axe; import cn.zj.qiao.spring.interfaces.Person; @Component public class Chinese implements Person { @Autowired @Qualifier("steelAxe") private Axe axe; public void setAxe(Axe axe){ this.axe = axe; } public Axe getAxe(){ return axe; } @Override public void useAxe() { System.out.println(axe.chop()); } }
如上代码所示,Axe axe Field将使用自动装配,且精确的指定了被装配Bean的实例的名称是steelAxe。