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

相关推荐
吕彬-前端7 分钟前
使用vite+react+ts+Ant Design开发后台管理项目(五)
前端·javascript·react.js
学前端的小朱10 分钟前
Redux的简介及其在React中的应用
前端·javascript·react.js·redux·store
guai_guai_guai19 分钟前
uniapp
前端·javascript·vue.js·uni-app
duration~32 分钟前
Maven随笔
java·maven
zmgst35 分钟前
canal1.1.7使用canal-adapter进行mysql同步数据
java·数据库·mysql
跃ZHD1 小时前
前后端分离,Jackson,Long精度丢失
java
blammmp1 小时前
Java:数据结构-枚举
java·开发语言·数据结构
bysking1 小时前
【前端-组件】定义行分组的表格表单实现-bysking
前端·react.js
暗黑起源喵1 小时前
设计模式-工厂设计模式
java·开发语言·设计模式
WaaTong1 小时前
Java反射
java·开发语言·反射