【Spring】Spring的@Autowire注入Bean的规则测试
背景
在项目中使用Spring的Bean,一般都使用默认的Bean的单例,并且结合@Autowire
使用。
实在有同一个类型多个实例的情况,也使用@Qualifier
或@Resource
实现注入。
所以,对@Autowire
的注入规则并不是特别的清楚。
今天突然想起这个疑惑,就用简单的实验确认一下。
实验的基础类
AppleBean,里面有个字符串的属性,和覆盖toString方法,用于在打印日志里更明显地分辨不同的bean:
public class AppleBean {
private String content;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "AppleBean{" +
"content='" + content + '\'' +
'}';
}
}
当容器中该类型只有一个Bean实例时,按类型注入,与Bean名字无关
注册一个Bean,叫appleBean:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BeanConfig {
@Bean
public AppleBean appleBean() {
AppleBean appleBean = new AppleBean();
appleBean.setContent("1");
return appleBean;
}
}
将AppleBean注入到一个Controller中,并在此Controller的方法中打印AppleBean:
import com.example.demo.bean.AppleBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HeartBeatController {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private AppleBean appleBean;
@GetMapping("/test")
public String test() {
this.logger.info("appleBean -> {}", appleBean);
return "SUCCESS";
}
}
运行程序,并访问此测试接口,可见日志,appleBean成功注入:
com.example.demo.HeartBeatController : appleBean -> AppleBean{content='1'}
修改注入的属性名为appleBean2,与注册的bean名字appleBean是不一致的哦:
import com.example.demo.bean.AppleBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HeartBeatController {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private AppleBean appleBean2;
@GetMapping("/test")
public String test() {
this.logger.info("appleBean -> {}", appleBean2);
return "SUCCESS";
}
}
再运行,日志如下,appleBean也正常注入进来了:
com.example.demo.HeartBeatController : appleBean -> AppleBean{content='1'}
此节结论:
当容器中该类型只有一个Bean实例时,按类型注入,与Bean名字无关
当容器中该类型只有多个Bean实例时,按类型注入,与Bean名字有关
我们实例化两个AppleBean,名字分别为appleBean、appleBean2:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BeanConfig {
@Bean
public AppleBean appleBean() {
AppleBean appleBean = new AppleBean();
appleBean.setContent("1");
return appleBean;
}
@Bean
public AppleBean appleBean2() {
AppleBean appleBean = new AppleBean();
appleBean.setContent("2");
return appleBean;
}
}
我们注入appleBean2试试:
import com.example.demo.bean.AppleBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HeartBeatController {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private AppleBean appleBean2;
@GetMapping("/test")
public String test() {
this.logger.info("appleBean -> {}", appleBean2);
return "SUCCESS";
}
}
运行,可见日志,与名字精准匹配的Bean被注入进来:
com.example.demo.HeartBeatController : appleBean -> AppleBean{content='2'}
我们注入appleBean3试试,appleBean3是一个不存在的Bean名字:
import com.example.demo.bean.AppleBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HeartBeatController {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private AppleBean appleBean3;
@GetMapping("/test")
public String test() {
this.logger.info("appleBean -> {}", appleBean3);
return "SUCCESS";
}
}
日志如下,启动时就报异常了:
Field appleBean3 in com.example.demo.HeartBeatController required a single bean, but 2 were found:
- appleBean: defined by method 'appleBean' in class path resource [com/example/demo/bean/BeanConfig.class]
- appleBean2: defined by method 'appleBean2' in class path resource [com/example/demo/bean/BeanConfig.class]
我们再实例化另一种类(BoyBean,不是刚才的AppleBean哦),但将它注册的Bean名叫appleBean3:
@Bean
public BoyBean appleBean3() {
BoyBean boyBean = new BoyBean();
return boyBean;
}
再运行,仍然报错,与上面的异常是一致的:
Field appleBean3 in com.example.demo.HeartBeatController required a single bean, but 2 were found:
- appleBean: defined by method 'appleBean' in class path resource [com/example/demo/bean/BeanConfig.class]
- appleBean2: defined by method 'appleBean2' in class path resource [com/example/demo/bean/BeanConfig.class]
此节结论:
当容器中该类型只有多个Bean实例时,按类型注入,与Bean名字有关
作者:Nick Huang 博客:http://www.cnblogs.com/nick-huang/
本博客为学习、笔记之用,以笔记形式记录学习的知识与感悟。学习过程中可能参考各种资料,如觉文中表述过分引用,请务必告知,以便迅速处理。如有错漏,不吝赐教。
如果本文对您有用,点赞或评论哦;如果您喜欢我的文章,请点击关注我哦~
本博客为学习、笔记之用,以笔记形式记录学习的知识与感悟。学习过程中可能参考各种资料,如觉文中表述过分引用,请务必告知,以便迅速处理。如有错漏,不吝赐教。
如果本文对您有用,点赞或评论哦;如果您喜欢我的文章,请点击关注我哦~