首页 > 其他分享 >SpringBoot Bean工具类 普通类环境中获取Bean

SpringBoot Bean工具类 普通类环境中获取Bean

时间:2024-08-20 16:55:22浏览次数:10  
标签:return SpringBoot throws 获取 Bean String static public name

通过实现BeanFactoryPostProcessor和ApplicationContextAware接口,
可以在Spring容器启动时注入BeanFactory和ApplicationContext。


import org.springframework.aop.framework.AopContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @Author Hang
 * @Description Bean工具类
 */
@Component
public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware {
    /**
     * Spring应用上下文环境
     */
    private static ConfigurableListableBeanFactory beanFactory;

    private static ApplicationContext applicationContext;

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        SpringUtils.beanFactory = beanFactory;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringUtils.applicationContext = applicationContext;
    }

    /**
     * 获取对象
     *
     * @param name
     * @return Object 一个以所给名字注册的bean的实例
     * @throws BeansException
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) throws BeansException {
        return (T) beanFactory.getBean(name);
    }

    /**
     * 获取类型为requiredType的对象
     *
     * @param clz
     * @return
     * @throws BeansException
     */
    public static <T> T getBean(Class<T> clz) throws BeansException {
        T result = (T) beanFactory.getBean(clz);
        return result;
    }

    /**
     * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
     *
     * @param name
     * @return boolean
     */
    public static boolean containsBean(String name) {
        return beanFactory.containsBean(name);
    }

    /**
     * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
     *
     * @param name
     * @return boolean
     * @throws NoSuchBeanDefinitionException
     */
    public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
        return beanFactory.isSingleton(name);
    }

    /**
     * @param name
     * @return Class 注册对象的类型
     * @throws NoSuchBeanDefinitionException
     */
    public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
        return beanFactory.getType(name);
    }

    /**
     * 如果给定的bean名字在bean定义中有别名,则返回这些别名
     *
     * @param name
     * @return
     * @throws NoSuchBeanDefinitionException
     */
    public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
        return beanFactory.getAliases(name);
    }

    /**
     * 获取aop代理对象
     *
     * @param invoker
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T getAopProxy(T invoker) {
        return (T) AopContext.currentProxy();
    }

    /**
     * 获取当前的环境配置,无配置返回null
     *
     * @return 当前的环境配置
     */
    public static String[] getActiveProfiles() {
        return applicationContext.getEnvironment().getActiveProfiles();
    }

    /**
     * 获取当前的环境配置,当有多个环境配置时,只获取第一个
     *
     * @return 当前的环境配置
     */
    public static String getActiveProfile() {
        final String[] activeProfiles = getActiveProfiles();
        return activeProfiles.length > 0 ? activeProfiles[0] : null;
    }

    /**
     * 获取配置文件中的值
     *
     * @param key 配置文件的key
     * @return 当前的配置文件的值
     */
    public static String getRequiredProperty(String key) {
        try {
            return applicationContext.getEnvironment().getRequiredProperty(key);
        } catch (Exception e) {
            return null;
        }
    }
}

标签:return,SpringBoot,throws,获取,Bean,String,static,public,name
From: https://blog.csdn.net/guo__hang/article/details/141362971

相关文章

  • 【工具使用】【SpringBoot】【P6spy】P6spy 的使用
    1 前言今儿在看 HikariCP数据库连接池实战我主要是想看下,连接的管理、连接的获取及释放。但是看到第五章的时候,书中提到P6spy,说是能很容易监控到JDBC中执行的SQL语句。那我们平时SpringBoot微服务对数据的操作,不管是JDBCTemplate、还是Mybatis、Hibernate最后的落点......
  • arcgis js 获取距离和面积
    Definesthetypeofcalculationforthegeometry.Thetypecanbeoneofthefollowing:planar: Planarmeasurementsuse2DCartesianmathematicstocalculatelength.Usethistypeifthelengthneedstobecalculatedintheinputspatialreferenceotherw......
  • 《深入探究 @SpringBootApplication 注解的内部原理》
    《深入探究@SpringBootApplication注解的内部原理》@SpringBootApplication注解涵盖了SpringBoot的包扫描原理、自动装配原理等众多重要原理。接下来,我们将对该注解展开深入且详尽的研究。而研究上述原理的关键,在于剖析@SpringBootApplication内部的构成结构,如下图:......
  • springboot投票管理系统-计算机毕业设计源码33128
    摘 要本文介绍了基于微信小程序和SpringBoot的投票管理系统的设计与实现。该系统结合了移动互联网技术和后端开发框架,旨在为各类组织或活动提供一个高效、便捷、用户友好的在线投票平台。系统采用微信小程序作为前端展示与交互界面,用户无需下载安装即可通过微信快速访问......
  • springboot助农商城小程序-计算机毕业设计源码34035
    目 录摘要1绪论1.1研究背景1.2 研究意义1.3论文结构与章节安排2系统分析2.1可行性分析2.2系统流程分析2.2.1用户登录流程2.2.2 数据删除流程2.3 系统功能分析2.3.1功能性分析2.3.2非功能性分析2.4 系统用例分析2.5本章小结3 系......
  • SpringBoot+Vue校园兼职平台-计算机毕业设计源码26261
    摘要校园兼职平台作为连接学生和校园兼职资源的重要桥梁,具有推动校园就业服务和学生职业发展的重要作用。本项目旨在基于SpringBoot后端框架和Vue前端框架,设计和实现一个高效、便捷的校园兼职平台。通过该平台,学生可以轻松浏览、搜索和申请各类校园兼职岗位,实现校园资源的最......
  • 免费【2024】基于SpringBoot 的干洗店预约洗衣系统设计与实现
    博主介绍:✌CSDN新星计划导师、Java领域优质创作者、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和学生毕业项目实战,高校老师/讲师/同行前辈交流✌技术范围:SpringBoot、Vue、SSM、HTML、Jsp、PHP、Nodejs、Python、爬虫、数据可视化、小程序、安卓app、大数......
  • 免费【2024】基于springboot 闲置物品共享平台的设计与实现
    博主介绍:✌CSDN新星计划导师、Java领域优质创作者、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和学生毕业项目实战,高校老师/讲师/同行前辈交流✌技术范围:SpringBoot、Vue、SSM、HTML、Jsp、PHP、Nodejs、Python、爬虫、数据可视化、小程序、安卓app、大数......
  • Springboot计算机毕业设计驾校管理系统(程序+源码+数据库+调试部署+开发环境)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表学员,教练,驾校车辆,学车预约,考试信息,考试预约,考试成绩,课时充值,取消学车,练车登记,财务信息开题报告内容一、项目背景随着社会的快速发展和人们生活水平的......
  • Springboot计算机毕业设计驾校信息管理系统c6oor
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表学员,教练,车辆信息,学员信息,我的预约,调度信息,车辆类型开题报告内容一、研究背景与意义随着中国人民生活水平的日益提高,汽车已经成为现代生活不可或缺的一部......