获取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]

相关推荐
V+zmm101342 分钟前
基于微信小程序的乡村政务服务系统springboot+论文源码调试讲解
java·微信小程序·小程序·毕业设计·ssm
吃杠碰小鸡18 分钟前
lodash常用函数
前端·javascript
Oneforlove_twoforjob27 分钟前
【Java基础面试题025】什么是Java的Integer缓存池?
java·开发语言·缓存
emoji11111128 分钟前
前端对页面数据进行缓存
开发语言·前端·javascript
xmh-sxh-131429 分钟前
常用的缓存技术都有哪些
java
泰伦闲鱼30 分钟前
nestjs:GET REQUEST 缓存问题
服务器·前端·缓存·node.js·nestjs
m0_7482500335 分钟前
Web 第一次作业 初探html 使用VSCode工具开发
前端·html
一个处女座的程序猿O(∩_∩)O40 分钟前
vue3 如何使用 mounted
前端·javascript·vue.js
m0_7482359542 分钟前
web复习(三)
前端
AiFlutter1 小时前
Flutter-底部分享弹窗(showModalBottomSheet)
java·前端·flutter