本文参考了:

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操作和数据源操作的更多相关文章

  1. SpringBoot中service注入失败(A component required a bean of type 'XXService' that could not found)

    先写了JUnit,发现启动不了,注释掉有问题的service也不可以.可能是因为spring开始时会加载所有service吧. 按照网友们的说法,一般需要检查: 1.入口类有没有写MapperScan ...

  2. redis(Springboot中封装整合redis,java程序如何操作redis的5种基本数据类型)

    平常测试redis操作命令,可能用的是cmd窗口 操作redis,记录一下 java程序操作reids, 操作redis的方法 可以用Jedis ,在springboot 提供了两种 方法操作 Red ...

  3. spring扩展点之四:Spring Aware容器感知技术,BeanNameAware和BeanFactoryAware接口,springboot中的EnvironmentAware

    aware:英 [əˈweə(r)] 美 [əˈwer] adj.意识到的;知道的;觉察到的 XXXAware在spring里表示对XXX感知,实现XXXAware接口,并通过实现对应的set-XXX ...

  4. 在springboot中使用Mybatis Generator的两种方式

    介绍 Mybatis Generator(MBG)是Mybatis的一个代码生成工具.MBG解决了对数据库操作有最大影响的一些CRUD操作,很大程度上提升开发效率.如果需要联合查询仍然需要手写sql. ...

  5. 在springboot中使用redis缓存,将缓存序列化为json格式的数据

    背景 在springboot中使用redis缓存结合spring缓存注解,当缓存成功后使用gui界面查看redis中的数据 原因 springboot缓存默认的序列化是jdk提供的 Serializa ...

  6. 手把手教你springboot中导出数据到excel中

    手把手教你springboot中导出数据到excel中 问题来源: 前一段时间公司的项目有个导出数据的需求,要求能够实现全部导出也可以多选批量导出(虽然不是我负责的,我自己研究了研究),我们的项目是x ...

  7. 7 — 简单了解springboot中的thymeleaf

    1.官网学习地址 https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html 2.什么是thymeleaf? 一张图看明白: 解读: ...

  8. SpringBoot中的日志使用:

    SpringBoot中的日志使用(一) 一:日志简介: 常用的日志接口 commons-logging/slf4j 日志框架:log4j/logback/log4j2 日志接口屏蔽了日志框架的底层实现 ...

  9. Spring Retry 在SpringBoot 中的应用

    Spring Boot中使用Spring-Retry重试框架 Spring Retry提供了自动重新调用失败的操作的功能.这在错误可能是暂时的(例如瞬时网络故障)的情况下很有用. 从2.2.0版本开始 ...

随机推荐

  1. 深入理解读写锁—ReadWriteLock源码分析

    转载:https://blog.csdn.net/qq_19431333/article/details/70568478 ReadWriteLock管理一组锁,一个是只读的锁,一个是写锁.读锁可以在 ...

  2. HTML表单特别效果—音量调节,购物数量

    <form oninput="x.value=parseInt(a.value)+parseInt(b.value)">0<input type="ra ...

  3. ORACLE查询删除重复记录三种方法

    本文列举了3种删除重复记录的方法,分别是rowid.group by和distinct,小伙伴们可以参考一下.   比如现在有一人员表 (表名:peosons) 若想将姓名.身份证号.住址这三个字段完 ...

  4. 一对多sql

    <!-- 分页查询派货成本 --> <select id="queryCostRegionPriceBycondtion" parameterMap=" ...

  5. 关于Oracle RAC节点间免密码策略

    在RAC安装过程中无论grid或是Oracle软件都需要配置节点间免密码策略.配置方法网上有很多,oracle和可以帮你配置,此处不多说. 但是在RAC运行过程中是不需要免密码登陆的,所以在集群安装完 ...

  6. MySQL学习(四)查询

    一.group_concat()函数.把groupby的分组中字段数据组合显示出来 select s_id , GROUP_CONCAT(要显示的字段名)  from table group by 分 ...

  7. Coreseek:常见问题2

    1.failed to lock XXXXX.spl文件 这是你建索引的时候会出现的问题,是你没有把打开的的searchd服务给关闭.由于你打开searchd服务时.他会建立一个叫xxx.spl的暂时 ...

  8. Matlab Colour Theme

    [转]http://blog.csdn.net/df865017/article/details/48164429 使用MATLAB进行编码时, 长时间面对白底黑字的屏幕, 眼睛会疼! 因此, 选择一 ...

  9. luogu P3796【模板】AC自动机(加强版)

    嘟嘟嘟 这个和某谷的AC自动机模板简单版差不多. 但还是要注意几点的: 1.这个是统计出现次数,而不是是否出现,所以在查询的时候加上这个节点的val后,不能把val标记为-1.那么也就可以说查询的时间 ...

  10. POJ 3461 Oulipo 【KMP统计子串数】

    传送门:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submission ...