笔记:Spring基于注解方式实现属性注入,详解@Autowired、@Qulifier、@Resource、@Value四个注解
定义 Student、Teacher 两个类用于测试
- Student
public class Student {
private String name;
private Integer age;
public Student() {
}
public Student(Integer age, String name) {
this.age = age;
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void printStu() {
System.out.println("this is Student's method!");
}
}
- Teacher
public class Teacher {
private Student student;
public void print() {
System.out.println(student.getName() + " " + student.getAge());
student.printStu();
}
}
一、 @Autowired
- XML配置文件为:
<bean id="teacher" class="spring5.Annotation.Teacher">
</bean>
<bean id="student" class="spring5.Annotation.Student">
<property name="name" value="nash"></property>
<property name="age" value="18"></property>
</bean>
1.1 @Autowired的自动装配方式
Autowired
按byType
的方式进行匹配,在Ioc容器
中查找Bean
,当有且仅有一个Bean时,spring将进行属性注入,否则抛出异常- Autowired不是通过set函数进行装配,故Teacher类中
不需要setter方法
@Autowired
private Student student;
- Autowired注解不仅可以用在字段上,也可以用在
构造器、setter方法
如用在构造器上为:
@Autowired
public Teacher(Student student) {
this.student = student;
}
1.2 运行结果
二、@Qulifier
2.1 @Qulifier 的使用方法
@Qulifier
配合@Autowired
在有多个相同类型Bean的时候使用
如xml文件中配置了两个StudentBean:
<bean id="student1" class="spring5.Annotation.Student">
<property name="name" value="nash"></property>
<property name="age" value="18"></property>
</bean>
<bean id="student2" class="spring5.Annotation.Student">
<property name="name" value="dana"></property>
<property name="age" value="19"></property>
</bean>
则此时需要使用@Autowired
自动装配的话,则要配合@Qulifier
@Autowired
@Qualifier("student2")
private Student student;
2.2 运行结果
三、@Resource
3.1 @Resource的自动装配方式
- 如果
@Resource
后面没有任何内容,则spring默认按byName
的方式进行匹配。若按byName
找不到,则再按byType匹配,同样,此时若有多个同类型bean,会抛出异常
<bean id="student" class="spring5.Annotation.Student">
<property name="name" value="nash"></property>
<property name="age" value="18"></property>
</bean>
@Resource
private Student student;
运行结果:
- 如果@Resource后面指定了
name
或type
,则按指定的name或type去匹配
<bean id="stu" class="spring5.Annotation.Student">
<property name="name" value="nash"></property>
<property name="age" value="18"></property>
</bean>
@Resource(name = "stu")
private Student student;
运行结果:
如果指定的是type,类似:
@Resource(type = Student.class)
private Student student;
3.2 @Autowired 和 @Resource 的区别
@Autowired
默认按byType
,而@Resource
默认按byName
匹配@Autowired
是Spring
的注解,而@Resource
是J2EE
的
四、@Value
- @Value 不常用,一般用于注入普通类型(如
String、int
等) - @Value 一般有三种数值填充方式
4.1 @Value(" ")
- 直接注入普通类型
@Value("nash")
private String name;
@Value("1")
private int age;
@Value("https://www.csdn.net")
private Resource path;
4.2 @Value(#{ })
- 表示
SpEl表达式
通常用来获取bean的属性,或者调用bean的某个方法,也可以表示常量 - 当bean通过@Value(“#{}”) 获取其他bean的属性,或者调用其他bean的方法时,只要该bean (Beab_A)能够访问到被调用的bean(Beab_B),即要么Beab_A 和Beab_B在同一个容器中,或者Beab_B所在容器是Beab_A所在容器的父容器。
@Value("#{T(java.lang.Math).random() * 100}")
private Integer age;
@Value("#{systemProperties['os.name']}")
private String osName;
@Value("#{1}")
private int number; //获取数字 1
SpEl表达式 详见
4.3 @Value(${ })
- 一般用于获取外部配置文件的属性值
如config.properties
中
name = nash
@Value("${name}")
private String name;