Springboot注解实现自动装配
jdk1.5支持的注解,Spring2.5就支持注解了。
The introduction of annotation-based configuration raised the question of whether this approach is "better"than XML.
要使用注解须知:
-
1.导入约束: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></context:annotation-config> <bean id="cat" class="com.example.springannotation.dao.Cat"/> <bean id="cat1" class="com.example.springannotation.dao.Cat"/> <bean id="people" class="com.example.springannotation.dao.People"/> </beans>
@Autowired //**如果显示定义了Autowired的required属性为false,说明这个对象可以为null,否则不允许为空** @Nullable // 字段标记了这个注解,说明这个字段可以为null; //直接在属性上使用即可!也可以在set方式上使用! //使用Autowired 我们可以不用编写Set方法了,前提是你这个自动装配的属性在IOC (Spring)容器中存在,且符合名字byname!
如果显示定义了Autowired的required属性为false,说明这个对象可以为null,f否则不允许为空
如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成的时候,我们可以使用@Qualifier(value=“xxx”)去配置@Autowired的使用,指定一个唯一的bean对象注入!
@Resource(name="cat") //效果与Autowired几乎一致
小结:
@Resource和@ Autowired的区别:
-
都是用来自动装配的,都可以放在属性字段上
-
Autowired通过byType的方式实现,而且必须要求这个对象存在!【常用】
-
@Resource默认通过byname的方式实现,如果找不到名字,则通过byType实现!
如果两个都找不到的情况下,就报错!【常用】
-
执行顺序不同:@ Autowired通过byType的方式实现。@Resource默认通过byname的方式实现。
测试代码:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
// 实体类
@Component
public class People {
@Autowired(required = false)
private int id;
@Autowired
@Qualifier(value="cat1")
private Cat cat;
@Resource(name="cat")
private Cat cat2;
@Autowired(required = false)
private String name ="ming";
@Autowired(required = false)
private int age = 2;
public People() {
}
public Cat getCat2() {
return cat2;
}
public void setName(@Nullable String name) {
this.name = name;
}
public int getId() {
return id;
}
public Cat getCat() {
return cat;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "People{" +
"id=" + id +
", cat=" + cat +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
//测试类
package com.example.springannotation.service;
import com.example.springannotation.dao.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestMain {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
People people = (People) context.getBean("people");
people.getCat().Shout();
people.getCat2().Shout();
System.out.println(people.toString());
people.setName(null);
System.out.println(people.toString());
}
}