@Required
@Required注解适用于bean属性的setter方法
这个注解仅仅表示,受影响的bean属性必须在配置时被填充,通过在Bean定义或通过自动装配一个明确的属性值,@required在应用中并不是很常见,所以我们只需要知道这个注解即可
@Autowired
默认情况下,如果因找不到合适的bean将会导致autowiring失败抛出异常,可以通过下面的方式避免
每个类只能有一个构造器呗标记为required=true
@Autowired的必要属性,建议使用@required注解
@Autowired例子
配置文件
<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd"
>
<context:component-scan base-package="com.txr.beanannotation"/>
</beans>
StudentDAO类
package com.txr.beanannotation;
import org.springframework.stereotype.Repository;
@Repository
public class StudentDAO {
public void say(String name)
{
System.out.println(name+" : I'm a Student");
}
}
StudentServie类
package com.txr.beanannotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class StudentService {
@Autowired
private StudentDAO dao;
public void say(String name)
{
dao.say(name);
}
}
测试:
@Test
public void testStudentAnnotation()
{
StudentService service=(StudentService)context.getBean("studentService");
service.say("txr");
}
测试结果
txr : I'm a Student
接下来我们来测试在service类中为dao加上一个set方法
package com.txr.beanannotation;
import org.springframework.stereotype.Repository;
@Repository
public class StudentDAO {
public void say(String name)
{
System.out.println(name+" : I'm a Student");
}
}
我们发现测试结果一样
然后我们 再来测试一下构造器方式
package com.txr.beanannotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class StudentService {
//@Autowired
private StudentDAO dao;
//@Autowired
public void setDao(StudentDAO dao) {
this.dao = dao;
}
@Autowired
public StudentService(StudentDAO dao) {
this.dao = dao;
}
public void say(String name)
{
dao.say(name);
}
}
发现测试结果仍然一样
可以使用@Autowired注解哪些众所周知的解析依赖性接口,比如BeanFactory,ApplicationContext,Environment,ResourceLoader,ApplicationEventPublisher,and MessageSource
可以通过添加注解给需要改类型的数组字段或方法以提供ApplicationContext中的所有特定类型的bean
可以用于装配key为String的Map
如果希望数组有序,可以让bean实现org.springframework.core.Ordered接口或使用@Order注解
@Autowired是由Spring BeanPostProcessor处理的,所以不能在自己的BeanPostProcessor或BeanFactoryPostProcessor类型应用这些注解,这些类必须通过XML或者Spring的@Bean注解加载
数组及Map的自动注入例子
建立一个BeanInterface接口与俩个实现类 BeanImp1 、BeanImp2然后建一个BeanInterfaceInvok类
分别在俩个实现类与BeanInterfaceInvok上添加@Component注解
BeanInterfaceInvok类
package com.txr.beanannotation;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component("invoke")
public class BeanInterfaceInvok {
@Autowired
private List<BeanInterface> list;
public void say()
{
for(BeanInterface i:list)
{
System.out.println(i.getClass().getName());
}
}
}
测试
@Test
public void testAutowiringMulti()
{
BeanInterfaceInvok invok=(BeanInterfaceInvok) context.getBean("invoke");
invok.say();
}
输出结果
com.txr.beanannotation.multibean.BeanImp1
com.txr.beanannotation.multibean.BeanImp2
BeanInterfaceInvok类作如下修改
package com.txr.beanannotation;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component("invoke")
public class BeanInterfaceInvok {
@Autowired
private List<BeanInterface> list;
@Autowired
private Map<String,BeanInterface> map;
public void say()
{
System.out.println("this is list");
for(BeanInterface i:list)
{
System.out.println(i.getClass().getName());
}
System.out.println("======================================");
System.out.println("this is map");
for(Map.Entry<String, BeanInterface> entry :map.entrySet())
{
System.out.println(entry.getKey()+" "+entry.getValue().getClass().getName());
}
}
}
测试不变,测试结果如下
this is list
com.txr.beanannotation.multibean.BeanImp1
com.txr.beanannotation.multibean.BeanImp2
======================================
this is map
beanImp1 com.txr.beanannotation.multibean.BeanImp1
beanImp2 com.txr.beanannotation.multibean.BeanImp2
Map的key值为生成的bean标识符即首字母小写的类名
如果要用Order的话在俩个实现类中分别加上@Order()然后添加数据
如我把BeanImp1 Order值赋 我的BeanImp1赋值为2 BeanImp2赋值为1
package com.txr.beanannotation.multibean;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import com.txr.beanannotation.BeanInterface;
@Order(2)
@Component
public class BeanImp1 implements BeanInterface {
}
package com.txr.beanannotation.multibean;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import com.txr.beanannotation.BeanInterface;
@Order(1)
@Component
public class BeanImp2 implements BeanInterface {
}
测试时奇怪的现象发生了
this is list
com.txr.beanannotation.multibean.BeanImp2
com.txr.beanannotation.multibean.BeanImp1
======================================
this is map
beanImp1 com.txr.beanannotation.multibean.BeanImp1
beanImp2 com.txr.beanannotation.multibean.BeanImp2
从测试结果可以看到List进行了Order排序而map是无序的,因为Order本身是根据数组来实现的(有不同的观点欢迎提出,我也没太搞明白为什么Map不行)
@Qualifier
- 按类型自动装配可能多个bean实例的情况,可以使用Spring的@Qualifier注解缩小范围(或指定唯一),也可以用于指定单独的构造器参数或方法参数
- 可用于注解集合类型变量
如果通过名字进行注解注入,主要使用的不是@Autowired(即使在技术上能够通过@Qualifier指定bean的名字),替代方式是使用JSR-250标准中的@Resource注解,它是通过其独特的名称来定义来识别特定的目标(这是一个与所声明的类型无关的匹配过程)
因语义差异,集合或Map类型的bean无法通过@Autuwired来注入,因为没有匹配到这样的bean,为这些bean使用@Resource注解,通过唯一名称引用集合或Map的bean
@Autowired适用于fields,constructors,multi-argument methods这些允许在参数级别使用@Qualifier注解缩小范围的情况
@Resource使用于成员变量、只有一个参数的setter方法,所以在目标是构造器或一个多参数方法时,最好的方式是使用qualifiers
例子
在BeanInterfaceInvok类中添加一个BeanInterface类型的属性,由于他有俩个实现类所以我们使用@Qualifier来确定使用哪一个
package com.txr.beanannotation;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component("invoke")
public class BeanInterfaceInvok {
@Autowired
private List<BeanInterface> list;
@Autowired
private Map<String,BeanInterface> map;
@Autowired
@Qualifier("beanImp1")
private BeanInterface beanInterface;
public void testqualifier()
{
System.out.println("this is a qualifier "+beanInterface.getClass().getName());
}
}
测试
@Test
public void testAutowiringMulti()
{
BeanInterfaceInvok invok=(BeanInterfaceInvok) context.getBean("invoke");
invok.testqualifier();
}
测试结果
this is a qualifier com.txr.beanannotation.multibean.BeanImp1
这里补充一点@Autowired是按类型来注入的