注解@Autowired简单介绍

 

是属于spirng的注解

可以标记在成员变量上,也可以标记在方法上

除了使用@Autowired,有的时候标记也会使用到@Resource

@Resource来自jdk中,@Autowired是先根据类型找,在根据name找,@Resource是先根据name找,找不到再根据type找

@Autowired在Bean的生命周期的实例化后之后,初始化前之前调用

当CacheService类被多个类实现了,就会运行错误,这里需要指定默认,需要使用注解@Qualifier

 

以下是完整代码

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
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.java</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
 
</project>

  

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
package com.java;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class DemoApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
 
}
 
 
package com.java.service;
 
import java.util.List;
 
public interface CacheService {
 
    public List<String> getList();
 
    public String query();
}

  

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
53
54
55
56
57
58
59
package com.java.service.impl;
 
import com.java.service.CacheService;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * @author yourheart
 * @Description
 * @create 2022-06-20 20:57
 */
@Service("test001")
public class CacheServiceImpl implements CacheService {
 
    @Override
    public List<String> getList() {
        List<String> list=new ArrayList<>();
        list.add("123");
        list.add("567");
        list.add("qwe");
        list.add("asd");
        list.add("zxc");
        list.add("jkl");
        return list;
    }
 
    @Override
    public String query() {
        return "测试Autowired注解,通过标注成员属性";
    }
}
 
 
package com.java.service.impl;
 
import com.java.service.CacheService;
import org.springframework.stereotype.Service;
 
import java.util.List;
 
/**
 * @author yourheart
 * @Description
 * @create 2022-06-20 21:30
 */
@Service("test002")
public class TempCacheServiceImpl  implements CacheService {
    @Override
    public List<String> getList() {
        return null;
    }
 
    @Override
    public String query() {
        return null;
    }
}

  

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.java.util;
 
import com.java.service.CacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
 
import java.util.List;
 
/**
 * @author yourheart
 * @Description
 * @create 2022-06-20 20:58
 */
@Component
public class SystemUtil {
 
    private static CacheService cacheService;
 
    @Qualifier("test001")
    @Autowired
    private  void setCacheService(CacheService cacheService) {
        SystemUtil.cacheService = cacheService;
    }
 
    public static List<String> getList(){
        List<String> list = cacheService.getList();
        return list;
    }
}

  

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
package com.java;
 
/**
 * @author yourheart
 * @Description
 * @create 2022-06-20 21:14
 */
import com.java.service.CacheService;
import com.java.util.SystemUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
 
import java.util.List;
 
@SpringBootTest
public class DemoApplicationTests {
 
    @Qualifier("test001")
    @Autowired
    private CacheService cacheService;
 
 
    @Test
    public void test(){
 
        String query = cacheService.query();
        System.out.println("query="+query);
 
 
        List<String> list = SystemUtil.getList();
        list.forEach(a->{
            System.out.println(a);
        });
 
    }
}

  

 

posted @   不忘初心2021  阅读(333)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
点击右上角即可分享
微信分享提示