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

Spring Bean的获取方式

参考https://juejin.cn/post/7251780545972994108?searchId=2023091105493913AF7C1E3479BB943C80#heading-12
记录并补充

1.通过BeanFactoryAware

package com.toryxu.demo1.beans;import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.stereotype.Component;@Component
public class BeanFactoryHelper implements BeanFactoryAware {private static BeanFactory beanFactory;@Overridepublic void setBeanFactory(BeanFactory beanFactory) throws BeansException {BeanFactoryHelper.beanFactory = beanFactory;}public static Object getBean(String beanName) {return beanFactory.getBean(beanName);}public static <T> T getBean(Class<T> requireType) {return beanFactory.getBean(requireType);}
}

2.获取启动时的ApplicationContext

存到静态对象里

@SpringBootApplication
public class Demo1Application {public static void main(String[] args) {var applicationContext = SpringApplication.run(Demo1Application.class, args);}}

3.继承ApplicationObjectSupport

需要声明为Bean

@Component
public class SpringContextUtil extends ApplicationObjectSupport {public <T> T getBean(Class<T> requireType) {ApplicationContext ac = getApplicationContext();if (ac == null) return null;return ac.getBean(requireType);}
}

ApplicationObjectSupport继承了ApplicationContextAware,在容器创建完成后会执行setApplicationContext方法

4.继承WebApplicationObjectSupport

@Component
public class WebSpringContextUtil extends WebApplicationObjectSupport {public <T> T getBean(Class<T> requireType) {var applicationContext = getApplicationContext();if (applicationContext == null) return null;return applicationContext.getBean(requireType);}
}

WebApplicationObjectSupport是extends ApplicationObjectSupport的

5.WebApplicationContextUtils

public class SpringContextUtil2 {public <T> T getBean(ServletContext sc, Class<T> requireType) {var webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(sc);return webApplicationContext.getBean(requireType);}
}

Web项目中通过ServletContext获取Bean。

   @RequestMapping("/test/{beanName}")public void test(HttpServletRequest request,@PathVariable("beanName") String beanName){System.out.println("test");var servletContext = request.getServletContext();var webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);var bean = webApplicationContext.getBean(beanName);}

6.通过ApplicationContextAware

public class SpringContextUtil3 implements ApplicationContextAware {

    private static ApplicationContext ac;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {ac = applicationContext;}
}

7.通过ContextLoader(废弃)

public <T> T getBean(Class<T> requireType) {var currentWebApplicationContext = ContextLoader.getCurrentWebApplicationContext();return currentWebApplicationContext.getBean(requireType);
}

ContextLoader中维护了一个Context类加载器和ac的映射关系
通过当前线程获取当前类加载器,从而获得ac。

/*** Obtain the Spring root web application context for the current thread* (i.e. for the current thread's context ClassLoader, which needs to be* the web application's ClassLoader).* @return the current root web application context, or {@code null}* if none found* @see org.springframework.web.context.support.SpringBeanAutowiringSupport*/@Nullablepublic static WebApplicationContext getCurrentWebApplicationContext() {ClassLoader ccl = Thread.currentThread().getContextClassLoader();if (ccl != null) {WebApplicationContext ccpt = currentContextPerThread.get(ccl);if (ccpt != null) {return ccpt;}}retu
 @CallerSensitivepublic ClassLoader getContextClassLoader() {ClassLoader cl = this.contextClassLoader;if (cl == null) {return null;} else {if (!isSupportedClassLoader(cl)) {cl = ClassLoader.getSystemClassLoader();}SecurityManager sm = System.getSecurityManager();if (sm != null) {Class<?> caller = Reflection.getCallerClass();ClassLoader.checkClassLoaderPermission(cl, caller);}return cl;}}

ServletContextListener:
通过ServletContextListener,监听到contextInitialized事件,执行initWebApplicationContext方法,创建context并建立映射关系

一般都是在SSM项目中,需要配置web.xml才行,springboot项目中不会执行SpringBootServletInitializer .startUp方法

现在的项目中是获取不到该值的。

8.通过BeanFactoryPostprocessor

public class SpringContextUtil4 implements BeanFactoryPostProcessor {private static ConfigurableListableBeanFactory beanFactory;@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {this.beanFactory = beanFactory;}public <T> T getBean(Class<T> requireType) {return beanFactory.getBean(requireType);}}

9.直接获取容器中的ApplicationContext

    @Autowiredprivate ApplicationContext applicationContext;@RequestMapping("/test/{beanName}")public void test(HttpServletRequest request,@PathVariable("beanName") String beanName){System.out.println("test");applicationContext.getBean(beanName);
}

总结:

从容器中获取Bean的方式主要在启动时将ApplicationContext(BeanFactory的子接口)或者BeanFactory放进static变量中
其中又主要通过:
1.BeanFactoryAware
2.ApplicationContextAware(建议)
3.BeanFactoryPostProcessor
4.构造方法或者autowired/resource注解获取

1、3都是获取BeanFactory不建议
默认就直接从容器中拿applicationContext就可以了
当我们的对象不在容器中时,要获取applicationContext的话,就只能先搞一个Utils将applicationContext拿到并通过静态资源的形式放进去,这个时候就需要用到ApplicationContextAware。

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

相关文章:

  • 4795-2023 船用舱底水处理装置 学习记录
  • [框架设计之道(二)]设备、任务设置及业务流程
  • Nuxt3+Vite批量引入图片
  • 采用nodejs + socket.io实现简易聊天室功能(群聊 + 私聊)
  • 消息队列(一):需求分析
  • ImageViewer技术实现细节
  • MFC多文档程序,从菜单关闭一个文档和直接点击右上角的x效果不同
  • 【数据结构】C++实现AVL平衡树
  • 图神经网络系列之序章
  • Unity中 UI Shader的基本功能
  • 【自学开发之旅】Flask-标准化返回-连接数据库-分表-orm-migrate-增删改查(三)
  • numpy增删改查
  • 【kafka】kafka重要的集群参数配置
  • cs224w_colab3_2023 And cs224w_colab4_2023学习笔记
  • Cannot find module ‘prop-types‘
  • LeetCode-63-不同路径Ⅱ-动态规划
  • unity 使用Photon进行网络同步
  • 大数据课程M1——ELK的概述
  • C# byte[] 如何转换成byte*
  • MySQL与Oracle的分页
  • git基本手册
  • 每日一题(两数相加)
  • 恒运资本:沪指震荡涨0.28%,医药板块强势拉升,金融等板块上扬
  • 【计算机网络】Tcp详解
  • 最简单的laravel不使用任何扩展导出csv
  • Android studio 断点调试、日志断点
  • 服务器数据恢复-热备盘同步过程中硬盘离线的RAID5数据恢复案例
  • Python 使用input获取用户输入
  • Python 可迭代对象、迭代器、生成器
  • HTML的有序列表、无序列表、自定义列表