SpringBoot进阶教程(六十九)ApplicationContextAware
在某些特殊的情况下,Bean需要实现某个功能,但该功能必须借助于Spring容器才能实现,此时就必须让该Bean先获取Spring容器,然后借助于Spring容器实现该功能。为了让Bean获取它所在的Spring容器,可以让该Bean实现ApplicationContextAware接口。ApplicationContextAware 通过它Spring容器会自动把上下文环境对象调用ApplicationContextAware接口中的setApplicationContext方法。在ApplicationContextAware的实现类中,就可以通过这个上下文环境对象得到Spring容器中的Bean。看到—Aware就知道是干什么的了,就是属性注入的,但是这个ApplicationContextAware的不同地方在于,实现了这个接口的bean,当spring容器初始化的时候,会自动的将ApplicationContext注入进来。
1. 添加实现ApplicationContextAware的工具类
package learn.utils; import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component; /**
* @author toutou
* @date by 2020/12
* @des
*/
@Component
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext; @Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
applicationContext = context;
} public static ApplicationContext getApplicationContext() {
return applicationContext;
}
//获取Bean
public static <T> T getBean(Class<T> requiredType){
return getApplicationContext().getBean(requiredType);
}
public static <T> T getBean(String name){
return (T) getApplicationContext().getBean(name);
}
}
2. 接口中直接调用
@GetMapping("home")
public Result getUser(){
UserAccountService userAccountService = SpringContextUtil.getBean(UserAccountService.class);
return Result.setSuccessResult(userAccountService.getUserAccountById(1));
}
3. service中内部调用
由于项目中配置了多个数据源,若所有mybatis mapper访问都集中在单个service方法中,@AutoDBDecision声明的数据源会串。所以需要颗粒化。这样就会在单个service中需要请求内部的方法,这时候也可以用上ApplicationContextAware工具类。
3.1 service中声明内部专用调用方法inside()
/**
* @author toutou
* @date by 2020/12
* des https://www.cnblogs.com/toutou/
*/
public interface UserAccountService {
default UserAccountService inside() {
return SpringContextUtil.getBean(UserAccountService.class);
} UserAccountVO getUserAccountById(Integer id); UserAccountVO getUserAccountById2(Integer id);
}
3.2 impl中调用方法
/**
* @author toutou
* @date by 2020/12
* des https://www.cnblogs.com/toutou/
*/
@Service
public class UserAccountServiceImpl implements UserAccountService{
@Autowired
UserAccountMapper userMapper; public UserAccountVO getUserAccountById(Integer id){
UserAccountVO accountVO = null;
UserAccount account = userMapper.selectByPrimaryKey(id);
if (account != null) {
accountVO = new UserAccountVO();
accountVO.setId(account.getId());
accountVO.setAccount(account.getAccount());
accountVO.setAge(account.getAge());
accountVO.setEmail(account.getEmail());
accountVO.setUsername(account.getUsername());
accountVO.setPhone(account.getPhone());
} return accountVO;
} public UserAccountVO getUserAccountById2(Integer id){
return inside().getUserAccountById(id);
}
}
其他参考/学习资料:
- ApplicationContextAware (Spring Framework 5.3.2 API)
- How does ApplicationContextAware work in Spring?
- How to Get ApplicationContext in Spring Boot
v源码地址
https://github.com/toutouge/javademosecond/tree/master/hellolearn
作 者:请叫我头头哥
出 处:http://www.cnblogs.com/toutou/
关于作者:专注于基础平台的项目开发。如有问题或建议,请多多赐教!
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
特此声明:所有评论和私信都会在第一时间回复。也欢迎园子的大大们指正错误,共同进步。或者直接私信我
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是作者坚持原创和持续写作的最大动力!
#comment_body_3242240 { display: none }
SpringBoot进阶教程(六十九)ApplicationContextAware的更多相关文章
- SpringBoot进阶教程(六十四)注解大全
在Spring1.x时代,还没出现注解,需要大量xml配置文件并在内部编写大量bean标签.Java5推出新特性annotation,为spring的更新奠定了基础.从Spring 2.X开始spri ...
- SpringBoot进阶教程(六十五)自定义注解
在上一篇文章<SpringBoot进阶教程(六十四)注解大全>中介绍了springboot的常用注解,springboot提供的注解非常的多,这些注解简化了我们的很多操作.今天主要介绍介绍 ...
- SpringBoot进阶教程(二十九)整合Redis 发布订阅
SUBSCRIBE, UNSUBSCRIBE 和 PUBLISH 实现了 发布/订阅消息范例,发送者 (publishers) 不用编程就可以向特定的接受者发送消息 (subscribers). Ra ...
- SpringBoot进阶教程(五十九)整合Codis
上一篇博文<详解Codis安装与部署>中,详细介绍了codis的安装与部署,这篇文章主要介绍介绍springboot整合codis.如果之前看过<SpringBoot进阶教程(五十二 ...
- SpringBoot进阶教程(六十八)Sentinel实现限流降级
前面两篇文章nginx限流配置和SpringBoot进阶教程(六十七)RateLimiter限流,我们介绍了如何使用nginx和RateLimiter限流,这篇文章介绍另外一种限流方式---Senti ...
- SpringBoot进阶教程(六十)intellij idea project下建多个module搭建架构(上)
在 IntelliJ IDEA 中,没有类似于 Eclipse 工作空间(Workspace)的概念,而是提出了Project和Module这两个概念.多module有一个父maven工程,多个子工程 ...
- SpringBoot进阶教程(六十二)整合Kafka
在上一篇文章<Linux安装Kafka>中,已经介绍了如何在Linux安装Kafka,以及Kafka的启动/关闭和创建发话题并产生消息和消费消息.这篇文章就介绍介绍SpringBoot整合 ...
- SpringBoot进阶教程(六十一)intellij idea project下建多个module搭建架构(下)
在上一篇文章<SpringBoot进阶教程(六十)intellij idea project下建多个module(上)>中,我们已经介绍了在intellij idea中创建project之 ...
- SpringBoot进阶教程(七十四)整合ELK
在上一篇文章<SpringBoot进阶教程(七十三)整合elasticsearch >,已经详细介绍了关于elasticsearch的安装与使用,现在主要来看看关于ELK的定义.安装及使用 ...
随机推荐
- .Net编码规范整理
前言 此处只是整理并记录下.Net开发规范以便加深编码规范.一个好的编程规范可以让自己程序可读性,让自己编码更规范,分为两部分:通用规范..Net开发规范. 微软通用编程规范 明确性和一致性 库的使用 ...
- linux离线安装docker + docker-compose
1 准备阶段(docker) 1.1 卸载旧版本 如果电脑上已经存在docker,需要先卸载可能存在的旧版本: 1. 删除某软件,及其安装时自动安装的所有包 sudo apt-get autoremo ...
- Python中format_map与format字符串格式化的区别
在Python3中使用format和format_map方法都可以进行字符串格式化,但format是一种所有情况都能使用的格式化方法,format_map仅使用于字符串格式中可变数据参数来源于字典等映 ...
- Jemter环境搭建
Jemter环境搭建 步骤一:安装Jmeter 1.下载Jmeter,官网地址:http://jmeter.apache.org/download_jmeter.cgi 2.解压Jmeter安装包,J ...
- [MRCTF2020]Ezpop
题目: Welcome to index.php <?php //flag is in flag.php //WTF IS THIS? //Learn From https://ctf.ieki ...
- 读github,deepfm,pytorch源码 记录
代码:https://github.com/chenxijun1029/DeepFM_with_PyTorch 2020/12/2首先是数据预处理文件:dataPreprocess.py1. 源数据集 ...
- Shell:子shell概念
Blog:博客园 个人 目录 shell环境 什么是子shell 子shell的分类 shell环境 每个shell进程有一个自己的运行环境,不同的Shell进程有不同的Shell环境.Shell解析 ...
- 题解-NOI2016 优秀的拆分
NOI2016 优秀的拆分 \(T\) 组测试数据.求字符串 \(s\) 的所有子串拆成 \(AABB\) 形式的方案总和. 数据范围:\(1\le T\le 10\),\(1\le n\le 3\c ...
- css处理文字不换行、换行截断、溢出省略号
1.使文字不换行 white-space: nowrap; 值 描述 normal 默认.空白会被浏览器忽略. pre 空白会被浏览器保留.其行为方式类似 HTML 中的 <pre> 标签 ...
- 二、利用Git将GitHub上的项目拉下项目
本地同样需要安装Git,同样在GitHub上加入ssh公共钥匙 如果忘了 去看上一篇 一.本地项目部署到GitHub上 - 中华田园猫饭饭 - 博客园 (cnblogs.com) 1-鼠标右键点击 G ...