注解实现自动配置
大约 3 分钟Spring全家桶Spring入门
jdk1.5支持的注解,spring2.5支持注解。
The introduction of annotation-based configuration raised the question of whether this approach is “better” than XML. The short answer is “it depends.
使用注解须知
- 导入context约束
- 配置注解的支持
<?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>
环境准备
Cat.java
package com.kuang.pojo;
/**
* @author Administrator
*/
public class Cat {
public void shout(){
System.out.println("miao~");
}
}
Dog.java
package com.kuang.pojo;
/**
* @author Administrator
*/
public class Dog {
public void shout(){
System.out.println("wang~");
}
}
People.java
package com.kuang.pojo;
/**
* @author Administrator
*/
public class People {
private Cat cat;
private Dog dog;
private String name;
public Cat getCat() {
return cat;
}
public Dog getDog() {
return dog;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "People{" +
"cat=" + cat +
", dog=" + dog +
", name='" + name + '\'' +
'}';
}
}
beans2.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"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启注解的支持-->
<context:annotation-config/>
<bean id="cat11" class="com.kuang.pojo.Cat"/>
<bean id="cat222" class="com.kuang.pojo.Cat"/>
<bean id="dog22" class="com.kuang.pojo.Dog"/>
<bean id="dog222" class="com.kuang.pojo.Dog"/>
<bean id="people" class="com.kuang.pojo.People"/>
<bean id="cat" class="com.kuang.pojo.Cat"/>
<bean id="dog" class="com.kuang.pojo.Dog"/>
</beans>
测试类
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans2.xml");
People people = context.getBean("people", People.class);
people.getCat().shout();
people.getDog().shout();
}
@AutoWired
直接在属性上使用即可!也可以在set方法上使用!
使用@AutoWired我们可以不编写set方法,前提是你这个自动装配的属性在IOC(Spring)容器中存在且符合名字ByName!
<!--开启注解的支持-->
<context:annotation-config/>
<bean id="cat" class="com.kuang.pojo.Cat"/>
<bean id="dog" class="com.kuang.pojo.Dog"/>
package com.kuang.pojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.lang.Nullable;
import javax.annotation.Resource;
/**
* @author Administrator
*/
public class People {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
private String name;
public Cat getCat() {
return cat;
}
public Dog getDog() {
return dog;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "People{" +
"cat=" + cat +
", dog=" + dog +
", name='" + name + '\'' +
'}';
}
}
结果:
miao~
wang~
科普
@Nullable 说明这个字段可以为null
public @interface Autowired {
boolean required() default true;
}
/**
* 如果显示的定义了Autowired的属性为false,说明这个对象可以为null。否则不能为空
*/
@Autowired(required = false)
private Cat cat;
@Autowired
private Dog dog;
private String name;
@Qualifier
如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解完成的时候【@Autowired】。我们可以使用@Qualifier(value = "xxx")去配合@Autowired的使用,指定一个唯一的bean对象注入!
<!--开启注解的支持-->
<context:annotation-config/>
<bean id="cat11" class="com.kuang.pojo.Cat"/>
<bean id="cat222" class="com.kuang.pojo.Cat"/>
<bean id="dog22" class="com.kuang.pojo.Dog"/>
<bean id="dog222" class="com.kuang.pojo.Dog"/>
<bean id="people" class="com.kuang.pojo.People"/>
<bean id="cat" class="com.kuang.pojo.Cat"/>
<bean id="dog" class="com.kuang.pojo.Dog"/>
public class People {
@Autowired
@Qualifier(value = "cat11")
private Cat cat;
@Autowired
@Qualifier(value = "dog22")
private Dog dog;
private String name;
}
@Resource
public class People {
@Resource(name="cat11")
private Cat cat;
@Resource(name="dog22")
private Dog dog;
private String name;
}
@AutoWired和@Resource的区别
- 1、都是用来自动装配的,都可以放在属性字段上
- 2、@AutoWired先通过ByType的方式实现,而且必须要求这个对象存在!【常用】
- 3、@Resource默认通过ByName的方式实现,找不到则通过ByType!如果两个都找不到,就报错!