注解@Autowired简单介绍

 

是属于spirng的注解

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

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

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

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

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

 

以下是完整代码

<?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>

  

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();
}

  

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;
    }
}

  

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;
    }
}

  

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 @ 2022-06-20 21:39  不忘初心2021  阅读(305)  评论(0编辑  收藏  举报