JDK1.5支持注解,Spring2.5支持注解的!
The introduction of annotation-based configuration raised the question of whether this approach is “better” than XML.
要使用注解须知:
1.导入约束
xmlns:context="http://www.springframework.org/schema/context"
2.配置注解的支持<context:annotation-config/>
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解支持-->
<context:annotation-config/>
</beans>
1、注解自动装配
@Autowired注解
直接在属性上使用即可,也可以在set方法上使用!
使用@Autowired我们可以不用编写set方法了,前提是你这个自动装配的属性在IOC(Spring)容器中存在且符合名字byName
如果显示定义了Autowired的required属性为false,说明这个对象可以为null,否则不允许为空
@Autowired(required=false)
//如果显示定义了Autowired的required属性为false,说明这个对象可以为null,否则不允许为空
public class People{
@Autowired(required=false)
private Cat cat;
@Autowired
private Dog dog;
private String 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"
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">
<!--开启注解支持-->
<context:annotation-config/>
<bean id="cat1" class="com.jialidun.pojo.Cat"/>
<bean id="dog1" class="com.jialidun.pojo.Dog"/>
<bean id="people" class="com.jialidun.pojo.People"/>
</beans>
如果@Autowired自动配置的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成的时候,我们可以使用@Qualifier(value=“xxx”)去配置@Autowired的使用,指定一个唯一的bean对象注入!
@Qualifier注解
@Qualifier(value="")
科普:
@Nullable 字段标记了这个注解,说明这个字段可以为null;
public @interface Autowired{
boolean required() default true;
}
public class People{
@Autowired
@Qualifier(value="cat2")
private Cat cat;
@Autowired
@Qualifier(value="dog1")
private Dog dog;
private String name;
}
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"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!--开启注解支持-->
<context:annotation-config/>
<bean id="cat1" class="com.jialidun.pojo.Cat"/>
<bean id="cat2" class="com.jialidun.pojo.Cat"/>
<bean id="dog1" class="com.jialidun.pojo.Dog"/>
<bean id="dog2" class="com.jialidun.pojo.Dog"/>
<bean id="people" class="com.jialidun.pojo.People"/>
</beans>
@Resource注解
此注解是java元注解,它兼容了会先从bean的id名字先找,找不到情况下,会去从类型去查找!
public class People{
@Resource
private Cat cat;
private Dog dog;
private String name;
}
xml文件
<bean id="cat1" class="com.jialidun.pojo.Cat"/>
<bean id="dog1" class="com.jialidun.pojo.Dog"/>
<bean id="people" class="com.jialidun.pojo.People"/>
此方式不会报错,因为它会根据类型查找!
如果情况比较复杂,可以使用@Resource(name=“xxx”)来查找
public class People{
@Resource(name="cat1")
private Cat cat;
@Resource(name="dog2")
private Dog dog;
private String name;
}
xml文件
<bean id="cat1" class="com.jialidun.pojo.Cat"/>
<bean id="cat2" class="com.jialidun.pojo.Cat"/>
<bean id="dog1" class="com.jialidun.pojo.Dog"/>
<bean id="dog2" class="com.jialidun.pojo.Dog"/>
测试
@Test
public void test01(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
People people = context.getBean("people",People.class);
people.getDog().shout();
people.getCat().shout();
}
小结
@Autowired和**@Resource**的区别
- 都是用来装配的,都可以放在属性字段上
- @Autowired是通过byType方式实现的,而且必须要求这个对象存在!
- @Resource默认通过byName方式实现,如果找不到名字,则通过byType实现!如果两个方式都找不到,就报错!
- 执行顺序不同:@Autowired是通过byType方式实现的,@Resource默认通过byName方式实现