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);
        }
    }

}

原文地址:https://www.cnblogs.com/lzfhope/p/9821749.html

时间: 2024-07-31 11:06:40

springboot中有用的几个有用aware以及bean操作和数据源操作的相关文章

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

平常测试redis操作命令,可能用的是cmd窗口 操作redis,记录一下 java程序操作reids, 操作redis的方法 可以用Jedis ,在springboot 提供了两种 方法操作 RedisTemplate 和StringRedisTemplate 两种方法的区别 可参考:https://blog.csdn.net/yifanSJ/article/details/79513179 当然 springboot 中也可以使用Jedis ,本次记录的是 如何使用RedisTemplate

Springboot中的日志管理

本案例中可以了解,怎么配置日志的输出路径,输出格式(比如说zip格式),按日期进行划分(今天的日志输出为一个文件,明天的日志输出到另一个文件),在按日期划分的同时又按文件大小划分(比如说每天的日志记录很多,希望一个日志文件为5GB...),上面的功能,看完这篇都能实现.(参考链接超有用,建议直接下拉打开链接) Slf4j 日志管理的抽象接口 Log4j,log4j2,logback,日志管理框架,日志管理实现 Springboot中pom.xml,若引入了web的起步依赖,不需要再引入日志管理相

SpringBoot中使用Spring Data Jpa 实现简单的动态查询的两种方法

首先谢谢大佬的简书文章:http://www.jianshu.com/p/45ad65690e33# 这篇文章中讲的是spring中使用spring data jpa,使用了xml配置文件.我现在使用的是spring boot ,没有了xml文件配置就方便多了.我同样尝试了两种方式,也都是简单的查询,需要更复杂的查询,还需要我研究研究.往下看,需要先配置springboot的开发环境,需要大致了解springboot,这里可以看下面两篇文章: springboot 项目新建 springboot

Springboot中使用缓存

在开发中,如果相同的查询条件去频繁查询数据库, 是不是会给数据库带来很大的压力呢?因此,我们需要对查询出来的数据进行缓存,这样客户端只需要从数据库查询一次数据,然后会放入缓存中,以后再次查询时可以从缓存中读取.Spring3开始提供了强大的基于注解的缓存支持,可以通过注解配置方式低侵入的给原有Spring应用增加缓存功能,提高数据访问性能. 具体在Springboot中使用缓存如下: 1.在pom.xml中引入cache依赖,添加如下内容: <dependency> <groupId&g

如何在SpringBoot中使用JSP ?但强烈不推荐,果断改Themeleaf吧

做WEB项目,一定都用过JSP这个大牌.Spring MVC里面也可以很方便的将JSP与一个View关联起来,使用还是非常方便的.当你从一个传统的Spring MVC项目转入一个Spring Boot项目后,却发现JSP和view关联有些麻烦,因为官方不推荐JSP在Spring Boot中使用.在我看来,继续用这种繁杂的手续支持JSP仅仅只是为了简单兼容而已. 我们先来看看如何在SpringBoot中使用JSP ? 1. 在pom.xm中加入支持JSP的依赖 <dependency> <

Spring-Boot中Tomcat端口修改

Spring-Boot中Tomcat端口默认为8080,那么我们该如何来修改呢? 工具:eclipse ①打开eclipse,点击工具栏中的Run: ②选择Configurations(任选一个): ③进入修改页面,按下图进行设置(在VM arguments处填写-Dserver.port=**(**为你要启动的端口)) ④这样就修改成功啦,启动看看吧(不行就重启一下eclipse试试) 更多方法请参考: http://stackoverflow.com/questions/21083170/s

springboot中读取自定义properties文件

一.在高版本的springboot中,@ConfigurationProperties(prefix = "wisely2",locations = "classpath:wisely.properties")这个注解不支持了,所以我们要另辟蹊径 二.使用组合式注解: 1.自定义config.properties文件: 1 config.fileServer=/root/jzyp/staticserver/webapps/ROOT/server 2 config.s

springboot中数据库配置加密

在springboot中,配置数据库等信息时,用户名和密码明文显示会大大降低安全性,在此介绍一种加密方式,简单易用. 添加依赖: <dependency>    <groupId>com.github.ulisesbocchio</groupId>    <artifactId>jasypt-spring-boot-starter</artifactId>    <version>1.8</version> </de

在SpringBoot中配置定时任务

前言 之前在spring中使用过定时任务,使用注解的方式配置很方便,在SpringBoot中的配置基本相同,只是原来在spring中的xml文件的一些配置需要改变,在SpringBoot中也非常简单. 已经加入我的github模版中:https://github.com/LinkinStars/springBootTemplate 定时任务的分类 所谓定时任务,就是在项目启动之后,定时的去执行一个任务,从而满足业务的需要. 定时任务分为下面几种,串行,并行,同步,异步 串行,并行:当配置了多个定