SpringBoot获取Bean的工具类
1、beanName
- 默认是类名首字母小写
下面的类:beanName = bean1
@Component
public class Bean1 {
public String getBean1() {
return "Bean1";
}
}
- 修改beanName
下面的类:beanName = bean2New
@Component("bean2New")
public class Bean2 {
public String getBean2() {
return "Bean2";
}
}
2、工具类
package com.cc.eed.utils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
/**
* <p>获取spring的bean的工具类</p>
*
* @author CC
* @since 2023/10/12
*/
@Component
public class SpringBeanUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringBeanUtil.applicationContext = applicationContext;
}
/** <p>根据类的clazz获取<p>
* @param clazz clazz
* @return {@link T}
* @since 2023/10/12
* @author CC
**/
public static <T> T getBean(Class<T> clazz) {
T t = applicationContext != null ? applicationContext.getBean(clazz) : null;
Assert.notNull(t, "当前Bean为空,请重新选择!");
return t;
}
/** <p>根据Bean名字获取<p>
* @param clazzName bean名字
* @return {@link Object}
* @since 2023/10/12
* @author CC
**/
public static Object getBean(String clazzName) {
return applicationContext.getBean(clazzName);
}
}
3、使用
@Test
public void test01()throws Exception{
//方式一:根据类的clazz获取
Bean1 bean = SpringBeanUtil.getBean(Bean1.class);
System.out.println(bean.getBean1());
Bean2 bean2 = SpringBeanUtil.getBean(Bean2.class);
System.out.println(bean2.getBean2());
//方式二:根据bean名字获取
Bean1 bean11 = (Bean1) SpringBeanUtil.getBean("bean1");
System.out.println(bean11.getBean1());
//报错:NoSuchBeanDefinitionException
Bean2 bean22 = (Bean2) SpringBeanUtil.getBean("bean2");
//正确
Bean2 bean222 = (Bean2) SpringBeanUtil.getBean("bean2New");
System.out.println(bean222.getBean2());
}
SpringBoot获取Bean的工具类的更多相关文章
- Spring获取bean的工具类
package com.tech.jin.util; import org.springframework.context.ApplicationContext; import org.springf ...
- 可以随时拿取spring容器中Bean的工具类
前言 在Spring帮我们管理bean后,编写一些工具类的时候需要从容器中拿到一些对象来做一些操作,比如字典缓存工具类,在没有找到字典缓存时,需要dao对象从数据库load一次,再次存入缓存中.此时需 ...
- Spring MVC模式下,获取WebApplicationContext的工具类 方法
在已有的注解类型下,获取WebApplicationContext的工具类 通过 WebApplicationContextUtils.getRequiredWebApplicationContex ...
- [性能] Bean拷贝工具类性能比较
Bean拷贝工具类性能比较 引言 几年前做过一个项目,接入新的api接口.为了和api实现解耦,决定将api返回的实体类在本地也建一个.这样做有两个好处 可以在api变更字段的时候保持应用稳定性 可以 ...
- android 获取手机信息工具类
package com.yqy.yqy_listviewheadview; import android.content.Context; import android.telephony.Telep ...
- springboot封装JsonUtil,CookieUtil工具类
springboot封装JsonUtil,CookieUtil工具类 yls 2019-9-23 JsonUtil public class JsonUtil { private static Obj ...
- 工具类分享之获取Request/Response工具类《RequestContextHolderUtil》
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/aiyaya_/article/details/78975893前言在开发spring web项目时, ...
- 获取Spring容器Bean对象工具类
在开发中,总是能碰到用注解注入不了Spring容器里面bean对象的问题.为了解决这个问题,我们需要一个工具类来直接获取Spring容器中的bean.因此就写了这个工具类,在此记录一下,方便后续查阅. ...
- SpringBoot集成Mybatis-PageHelper分页工具类,实现3步完成分页
在Mybatis中,如果想实现分页是比较麻烦的,首先需要先查询出总的条数,然后再修改mapper.xml,为sql添加limit指令. 幸运的是现在已经不需要这么麻烦了,刘大牛实现了一个超牛的分页工具 ...
- 就因为加了Lombok的@Accessors(chain = true),bean拷贝工具类不干活了
前言 这次新建了一个工程,因为 Lombok 用得很习惯,但以前的话,一般只用了@Data,@AllArgsConstructor,@EqualsAndHashCode等常规注解:那这个Accesso ...
随机推荐
- R语言安装教程
R 语言官方网站:The Comprehensive R Archive Network 官方镜像站列表:CRAN - Mirrors 一.官网下载R安装包 下载地址为:Index of /bin 进 ...
- 使用SpringBatch读取csv文件
目录 1.需求 2.解决方案 3.注意事项 1.文件路径的获取 2.各个Step如果获取到ExecutionContext中的值 3.FlatFileItemReader使用注意 4.实现步骤 1.导 ...
- Zookeeper学习笔记-安装
zookeeper官网地址https://zookeeper.apache.org/ 1.卸载CentOS自带的open jdk,安装oracle jdk(1.8) 2.时间同步 #安装ntpdate ...
- spring boot @Scheduled 单线程的问题
package com.lxw.lxwDemo; import org.springframework.scheduling.annotation.EnableScheduling; import o ...
- MemfireCloud让静态托管页面动起来!
静态托管 我们最常接触到的静态托管是github pages,它的常见工作模式是在github上创建一个仓库,使用hexo类的工具初始化仓库,编写markdown文件,生成静态页面,推送到github ...
- https://codeforces.com/gym/496432
ABC:略. D 枚举分的段数,然后扫一遍判断. E F G H
- C语言跨平台时间操作计算时间差
头文件 #pragma once #if defined(_WIN32) #include<sys/timeb.h> #if defined(__UNIX__)||defined(__AP ...
- 提高生产力!这10个Lambda表达式必须掌握,开发效率嘎嘎上升!
在Java8及更高版本中,Lambda表达式的引入极大地提升了编程的简洁性和效率.本文将围绕十个关键场景,展示Lambda如何助力提升开发效率,让代码更加精炼且易于理解. 集合遍历 传统的for-ea ...
- 两个专栏帮你搞定【图像拼接(image stitching)】
[图像拼接论文精读]专栏:图像拼接论文精读 [图像拼接源码精读]专栏:图像拼接论文源码精读 前言 图像拼接(image stitching)是计算机视觉中的高级图像处理手段,是一个小众方向,研究的人很 ...
- synchronized解锁源码分析
上篇花了很大篇幅写了synchronized的加锁流程,并对比了ReentrantLock的设计,这篇我们收个尾,来聊一聊解锁流程,本来准备一章解决的,写着写着觉得内容过多,其实上一篇和Reentra ...