获取Spring中bean工具类

获取Spring中bean工具类

工具类

java 复制代码
package com.geekmice.springbootselfexercise.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author PMB
 */
@Component
public class SpringUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (SpringUtil.applicationContext == null) {
            SpringUtil.applicationContext = applicationContext;
        }
        // System.out.println("---------------------------------------------------------------------");
        //
        // System.out.println("---------------------------------------------------------------------");
        //
        // System.out.println("========ApplicationContext配置成功,在普通类可以通过调用SpringUtils.getAppContext()获取applicationContext对象,applicationContext=" + SpringUtil.applicationContext + "========");
        //
        // System.out.println("---------------------------------------------------------------------");
        // // 可以查询数据库做一些项目启动的初始化
        // // IDataService iDataService = getBean(IDataService.class);
        // // 可以初始化启动一些定时任务
        // // QuartzManager.addJob();
        // // 可以初始化设置一些SpEL参数
        // System.setProperty("TopicArray", "dataTopic,weatherTopic");
    }

    /**
     * 获取applicationContext,应用上下文
     *
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 通过name获取 Bean.
     *
     * @param name bean名称
     * @return bean
     */
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    /**
     * 通过class获取Bean
     */
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    /**
     * 通过name,以及Clazz返回指定的Bean
     *
     * @param name bean名称
     * @param clazz class对象
     * @param <T> 指定泛型
     * @return
     */
    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }

}

测试类

@RestController(value = "easySqlInjector")

@RequestMapping(value = "bean")

@Api(tags = "3.获取bean操作")

@Slf4j

public class BeanController {
@Bean(name = "easySqlInjector")

public EasySqlInjector easySqlInjector(){

return new EasySqlInjector();

}

java 复制代码
package com.geekmice.springbootselfexercise.controller;

import com.geekmice.springbootselfexercise.injector.EasySqlInjector;
import com.geekmice.springbootselfexercise.utils.AjaxResult;
import com.geekmice.springbootselfexercise.utils.SpringUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @BelongsProject: spring-boot-self-exercise
 * @BelongsPackage: com.geekmice.springbootselfexercise.controller
 * @Author: pingmingbo
 * @CreateTime: 2023-08-09  21:52
 * @Description: bean操作
 * @Version: 1.0
 */
@RestController(value = "easySqlInjector")
@RequestMapping(value = "bean")
@Api(tags = "3.获取bean操作")
@Slf4j
public class BeanController {

    @GetMapping(value = "getBean")
    public AjaxResult getBean() {
        // 根据class获取bean
        EasySqlInjector bean = SpringUtil.getBean(EasySqlInjector.class);

        // 根据name获取bean
        EasySqlInjector easySqlInjector = (EasySqlInjector)SpringUtil.getBean("easySqlInjector");

        // 根据name和class获取bean
        EasySqlInjector easySqlInjectorSecond = SpringUtil.getBean("easySqlInjector", EasySqlInjector.class);

        log.info("easySqlInjectorSecond : [{}]" , easySqlInjectorSecond);
        log.info("easySqlInjector : [{}]", easySqlInjector);
        log.info("bean : [{}]", bean);
        return AjaxResult.success();
    }
}

2023-08-09 22:04:20.211 INFO 25656 --- [nio-8081-exec-1] c.g.s.controller.BeanController : easySqlInjectorSecond : [com.geekmice.springbootselfexercise.injector.EasySqlInjector@935fe38]

2023-08-09 22:04:20.211 INFO 25656 --- [nio-8081-exec-1] c.g.s.controller.BeanController : easySqlInjector : [com.geekmice.springbootselfexercise.injector.EasySqlInjector@935fe38]

2023-08-09 22:04:20.212 INFO 25656 --- [nio-8081-exec-1] c.g.s.controller.BeanController : bean : [com.geekmice.springbootselfexercise.injector.EasySqlInjector@935fe38]

相关推荐
敲代码的小王!2 小时前
MD5加密算法和BCrypt密码加密算法
java·算法·安全
Smile_Gently2 小时前
前端:最简单封装nmp插件(组件)过程。
前端·javascript·vue.js·elementui·vue
web135085886356 小时前
【Spring Boot】Spring AOP动态代理,以及静态代理
spring boot·后端·spring
罗政7 小时前
冒险岛079 V8 整合版源码搭建教程+IDEA启动
java·ide·intellij-idea
架构默片7 小时前
【JAVA工程师从0开始学AI】,第五步:Python类的“七十二变“——当Java的铠甲遇见Python的液态金属
java·开发语言·python
zzyh1234568 小时前
springcloud的组件及作用
后端·spring·spring cloud
不只会拍照的程序猿8 小时前
从插入排序到希尔排序
java·开发语言·数据结构·算法·排序算法
luckycoke8 小时前
小程序立体轮播
前端·css·小程序
一 乐8 小时前
高校体育场管理系统系统|体育场管理系统小程序设计与实现(源码+数据库+文档)
前端·javascript·数据库·spring boot·高校体育馆系统
山海不说话8 小时前
从零搭建微服务项目Base(第5章——SpringBoot项目LogBack日志配置+Feign使用)
spring boot·后端·spring·spring cloud·微服务·logback