springboot中有用的几个有用aware以及bean操作和数据源操作
本文参考了:
https://blog.csdn.net/derrantcm/article/details/76652951
https://blog.csdn.net/derrantcm/article/details/73456550
通过以上可以获得springboot的许多知识。
本文只是列出本人常用的两个aware.
闲话少叙,直接上代码
BeanFactoryAware 帮助获取各种bean
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.stereotype.Component; @Component
public class BeanHelper implements BeanFactoryAware { private static BeanFactory beanFactory; @Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
} public static <T>T getBean(String id,Class<T> type){
return beanFactory.getBean(id,type);
} public static <T>T getBean(Class<T> type){
return beanFactory.getBean(type); } public static <T>T getBean(String beanName){
return (T) beanFactory.getBean(beanName);
} }
ApplicationContextAware 帮助获取上线文的信息,也可以操作bean
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component; @Component
public class DsControl implements ApplicationContextAware { @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
//dataSource在springboot中是一个关键字
Object ds=applicationContext.getBean("dataSource") ;
System.out.println("当前的连接池是:"+ds.getClass().getName());
System.out.println("-----gooooo"); String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
for (String beanDefinitionName : beanDefinitionNames) {
System.out.println(beanDefinitionName);
}
} }
springboot中有用的几个有用aware以及bean操作和数据源操作的更多相关文章
- SpringBoot中service注入失败(A component required a bean of type 'XXService' that could not found)
先写了JUnit,发现启动不了,注释掉有问题的service也不可以.可能是因为spring开始时会加载所有service吧. 按照网友们的说法,一般需要检查: 1.入口类有没有写MapperScan ...
- redis(Springboot中封装整合redis,java程序如何操作redis的5种基本数据类型)
平常测试redis操作命令,可能用的是cmd窗口 操作redis,记录一下 java程序操作reids, 操作redis的方法 可以用Jedis ,在springboot 提供了两种 方法操作 Red ...
- spring扩展点之四:Spring Aware容器感知技术,BeanNameAware和BeanFactoryAware接口,springboot中的EnvironmentAware
aware:英 [əˈweə(r)] 美 [əˈwer] adj.意识到的;知道的;觉察到的 XXXAware在spring里表示对XXX感知,实现XXXAware接口,并通过实现对应的set-XXX ...
- 在springboot中使用Mybatis Generator的两种方式
介绍 Mybatis Generator(MBG)是Mybatis的一个代码生成工具.MBG解决了对数据库操作有最大影响的一些CRUD操作,很大程度上提升开发效率.如果需要联合查询仍然需要手写sql. ...
- 在springboot中使用redis缓存,将缓存序列化为json格式的数据
背景 在springboot中使用redis缓存结合spring缓存注解,当缓存成功后使用gui界面查看redis中的数据 原因 springboot缓存默认的序列化是jdk提供的 Serializa ...
- 手把手教你springboot中导出数据到excel中
手把手教你springboot中导出数据到excel中 问题来源: 前一段时间公司的项目有个导出数据的需求,要求能够实现全部导出也可以多选批量导出(虽然不是我负责的,我自己研究了研究),我们的项目是x ...
- 7 — 简单了解springboot中的thymeleaf
1.官网学习地址 https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html 2.什么是thymeleaf? 一张图看明白: 解读: ...
- SpringBoot中的日志使用:
SpringBoot中的日志使用(一) 一:日志简介: 常用的日志接口 commons-logging/slf4j 日志框架:log4j/logback/log4j2 日志接口屏蔽了日志框架的底层实现 ...
- Spring Retry 在SpringBoot 中的应用
Spring Boot中使用Spring-Retry重试框架 Spring Retry提供了自动重新调用失败的操作的功能.这在错误可能是暂时的(例如瞬时网络故障)的情况下很有用. 从2.2.0版本开始 ...
随机推荐
- 使用durid的ConfigFilter对数据库密码加密
<!-- 配置dbcp数据源 --> <bean id="remoteDS" class="org.apache.commons.dbcp.BasicD ...
- 微服务学习笔记一:Spring Cloud简介
1.Spring Cloud是一个工具集:Spring Cloud是在Spring Boot的基础上构建的,用于简化分布式系统构建的工具集:使架构师在创建和发布微服务时极为便捷和有效. Sp ...
- select @@identity用法
用select @@identity得到上一次插入记录时自动产生的ID 如果你使用存储过程的话,将非常简单,代码如下:SET @NewID=@@IDENTITY 说明: 在一条 INSERT.SELE ...
- 移动端点击a链接出现蓝色背景问题解决
a:link, a:active, a:visited, a:hover { background: none; -webkit-tap-highlight-color: rgba(0,0,0,0); ...
- Strom入门
Worker.Executor.Task详解: Storm在集群上运行一个Topology时,主要通过以下3个实体来完成Topology的执行工作:1. Worker Process(工作进程)——S ...
- 安装lombok(eclipse)
下载 lombok.jar (https://projectlombok.org/download.html) 将 lombok.jar 放在eclipse安装目录下,和 eclipse.ini 文件 ...
- winform中 让 程序 自己重启
private void button1_Click(object sender, EventArgs e) { Application.ExitThread(); ...
- 创建Spark镜像文件
创建Spark镜像文件 1.将spark容器提交到新的镜像中 $>docker commit 01a25bdf1499 myrepos:centos-spark 2.将centos-spark镜 ...
- vue 单页应用中微信支付的坑
vue 单页应用中微信支付的坑 标签(空格分隔): 微信 支付 坑 vue 场景 在微信H5页面(使用 vue-router2 控制路由的 vue2 单页应用项目)中使用微信 jssdk 进行微信支付 ...
- Unix 和 Linux 安装 Perl
Unix/Linux 系统上 Perl 安装步骤如下: 通过浏览器打开 http://www.perl.org/get.html. 下载适用于 Unix/Linux 的源码包. 下载 perl-5.x ...