本文参考了:

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. 对json缓存进行操作

    var data={ id:1, name:"张三" } //存储缓存 var arrdata=[]; arrdata.push({id:data.id,name:data.nam ...

  2. SharePoint 2013 - App Domain Configuration

    1. 首先,在DNS服务器上创建app domain,建议使用一个新domain,而不是当前domain的 sub domain,参考此文章的 Option A: Create a new domai ...

  3. java面试题之----spring与struts2的比较

    我们从以下几个维度来区分两者的概念: 1.  机制:spring mvc的入口是servlet,而struts2是filter.     补充几点知识:     < Filter 实现javax ...

  4. Java 创建 ARM 虚拟机磁盘类型选择的问题

    问题描述 在Azure 门户创建 ARM 虚拟机时,我们直接可以选择虚拟机的磁盘类型,但是在 Azure Management Libraries for Java 的 API 中我们无法找到直接设置 ...

  5. O/S-Error: (OS 23) Data error (cyclic redundancy check)问题处理

    RMAN-03002: backup plus archivelog 命令 (在 08/24/2015 03:31:00 上) 失败ORA-19501: 文件 "XXXXXX.DBF&quo ...

  6. March 24 2017 Week 12 Friday

    Our lives are brief, that is why it's important to search for meaning. 人生短暂,所以才要寻找它的意义. What can we ...

  7. SQA和测试规程

    SQA *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; ...

  8. Educational Codeforces Round 10 D. Nested Segments 【树状数组区间更新 + 离散化 + stl】

    任意门:http://codeforces.com/contest/652/problem/D D. Nested Segments time limit per test 2 seconds mem ...

  9. 2018.12.16 struts.xml 结果集方式分析 && 源码查看

    1.结果集 转发 重定向 转发Action 重定向Action <?xml version="1.0" encoding="UTF-8"?> < ...

  10. 2018.11.8 Error contacting service. It is probably not running.

    安装zookeeper-3.4.6的时候,启动正常没报错,但zkServer.sh status查看状态的时候却出现错误,如下: JMX enabled by default Using config ...