当前位置: 首页 > news >正文

获取Spring中bean工具类

获取Spring中bean工具类

工具类

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;@Overridepublic 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();
}

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获取beanEasySqlInjector bean = SpringUtil.getBean(EasySqlInjector.class);// 根据name获取beanEasySqlInjector easySqlInjector = (EasySqlInjector)SpringUtil.getBean("easySqlInjector");// 根据name和class获取beanEasySqlInjector 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]

http://www.lryc.cn/news/121256.html

相关文章:

  • 【实战篇】亿级高并发电商项目(新建 ego_pojo、ego_mapper、ego_api、ego_provider、搭建后台项目 )十五
  • 【Plex】FRP内网穿透后 App无法使用问题
  • [管理与领导-11]:IT基层管理者 - 目标与落实 - 过程管理失控,结果总难达成的问题思考:如何把过程管控做得更好?
  • 用php语言写一个chatgpt3.5模型的例子
  • PHP实现保质期计算器
  • 【独立版】新零售社区团购电商系统生鲜水果商城兴盛优选十荟团源码
  • C++系列十:其他-1. Lua
  • 不知道打仗之害,就不知道打仗之利
  • 【leetcode】242. 有效的字母异位词(easy)
  • Windows电脑快速搭建FTP服务教程
  • Arduino驱动MQ2模拟烟雾传感器(气体传感器篇)
  • 常见分布式ID解决方案总结:数据库、算法、开源组件
  • 记录--Loading 用户体验 - 加载时避免闪烁
  • 系统架构设计专业技能 · 软件工程之软件测试与维护(六)【系统架构设计师】
  • 基于亚奈奎斯特采样和SOMP算法的平板脉冲响应空间插值matlab仿真
  • 柏睿向量数据库Rapids VectorDB赋能企业级大模型构建及智能应用
  • 装备合成(牛客)
  • C语言学习之一级指针二级指针
  • 【腾讯云 Cloud Studio 实战训练营】使用 Cloud Studio 快速构建 Vue + Vite 完成律师 H5 页面
  • Vim常用指令
  • 24届近3年青岛理工大学自动化考研院校分析
  • 进入现代云技术的世界-APIGateway、ServiceMesh、OpenStack、异步化框架、云原生框架、命令式API与声明式API
  • Macbook 终端 git 命令补全和提示
  • 2024考研408-计算机网络 第六章-应用层学习笔记
  • 使用阿里云服务器部署和使用GitLab
  • React入门学习笔记3
  • 从零开始理解Linux中断架构(25)中断运行全景实例
  • go-zero 是如何实现计数器限流的?
  • 【考研复习】24王道数据结构课后习题代码|第3章栈与队列
  • java中excel文件下载