概述

实现Spring自动装配的注解有两个:@Autowired和@Resource,两个注解的区别如下:

  1. @Autowired

    • 出处:这是Spring的注解
    • 用法:可以放在属性上面(这时候可以省略setter方法),也可以放在setter方法上面,一般放在属性上面即可
    • 实现方式:通过byType的方式实现,需要保证所有bean的class唯一,即不可以存在相同类型的bean
    • 指定要装配的bean的id的方式:可以使用@Qualifier注解指定bean的id,这个注解使用value属性指定bean的id
  2. @Resource

    • 出处:这是JDK的注解
    • 用法:放在属性上面
    • 实现方式:默认通过byName的方式实现,如果找不到名字,则通过byType实现,需要保证所有bean的class唯一,即不可以存在相同类型的bean;如果两个都找不到的情况下,就报错
    • 指定要装配的bean的id的方式:指定bean的id的方式有3种:①根据Setter方法名指定自动装配的bean的id,例如:Setter的方法名为setDog1,所以bean的id就必须为dog1②通过@Resource注解的name属性指定bean的id③可以使用@Qualifier注解指定bean的id,这个注解使用value属性指定bean的id。特别地,②和③一起用,②的方式有效

代码实现

@Autowired

ApplicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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 https://www.springframework.org/schema/context/spring-context.xsd">

<!--开启注解的支持-->
<context:annotation-config/>

<bean id="dog1" class="com.ledao.entity.Dog">
<property name="name" value="小哈1"/>
</bean>
<bean id="dog2" class="com.ledao.entity.Dog">
<property name="name" value="小哈2"/>
</bean>
<bean id="cat1" class="com.ledao.entity.Cat">
<property name="name" value="小花1"/>
</bean>

<bean id="people" class="com.ledao.entity.People">
<property name="name" value="tom"/>
</bean>
</beans>

Dog实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.ledao.entity;

/**
* @author LeDao
* @company
* @create 2022-02-15 18:14
*/
public class Dog {

private String name;

public Dog() {
System.out.println("狗吃骨头");
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
'}';
}
}

Cat实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.ledao.entity;

/**
* @author LeDao
* @company
* @create 2022-02-15 18:14
*/
public class Cat {

private String name;

public Cat() {
System.out.println("猫吃鱼");
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
'}';
}
}

People实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.ledao.entity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

/**
* @author LeDao
* @company
* @create 2022-02-15 18:13
*/
public class People {

private String name;
@Autowired
@Qualifier(value = "dog2")
private Dog dog;
@Autowired
private Cat cat;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Dog getDog() {
return dog;
}

public void setDog(Dog dog) {
this.dog = dog;
}

public Cat getCat() {
return cat;
}

public void setCat(Cat cat) {
this.cat = cat;
}

@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
", dog=" + dog +
", cat=" + cat +
'}';
}
}

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import com.ledao.entity.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* @author LeDao
* @company
* @create 2022-02-12 15:56
*/
public class MyTest {

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
People people = context.getBean("people", People.class);
System.out.println(people);
}
}

结果

image-20220215185457615

@Resource

对比

和@Autowired的代码对比,只改动了People实体类

People实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.ledao.entity;

import javax.annotation.Resource;

/**
* @author LeDao
* @company
* @create 2022-02-15 18:13
*/
public class People {

private String name;
@Resource(name = "dog2")
private Dog dog;
@Resource
private Cat cat;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Dog getDog() {
return dog;
}

public void setDog(Dog dog) {
this.dog = dog;
}

public Cat getCat() {
return cat;
}

public void setCat(Cat cat) {
this.cat = cat;
}

@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
", dog=" + dog +
", cat=" + cat +
'}';
}
}