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