注解@Autowired简单介绍
是属于spirng的注解
可以标记在成员变量上,也可以标记在方法上
除了使用@Autowired,有的时候标记也会使用到@Resource
@Resource来自jdk中,@Autowired是先根据类型找,在根据name找,@Resource是先根据name找,找不到再根据type找
@Autowired在Bean的生命周期的实例化后之后,初始化前之前调用
当CacheService类被多个类实现了,就会运行错误,这里需要指定默认,需要使用注解@Qualifier
以下是完整代码
<?xml version="1.0" encoding="UTF-8"?>4.0.0 org.springframework.boot spring-boot-starter-parent 2.7.0 com.java demo 0.0.1-SNAPSHOT demo Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
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 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 getList() {
List 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 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 getList(){
List 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 list = SystemUtil.getList();
list.forEach(a->{
System.out.println(a);
});
}
}