【Spring】普通类获取Spring容器的bean的方法
创始人
2024-03-17 02:39:45
0

我们通常在项目中获取Spring容器里bean的方式,一般是使用注解的方式(@Autowired、@Resource)直接注入就可以直接使用了,那么如果在一个普通的类里(其他地方使用它的实例是以new的方式使用的),此时再用注解的方式注入的将会是null,那这种情况下,我们该如何使用Spring容器里的bean呢?本篇博客讲解五种方法,接下来就依次详细讲一下使用方式

一、在初始化时保存ApplicationContext对象

FileSystemXmlApplicationContext fileSystemXmlApplicationContext = new FileSystemXmlApplicationContext("applicationContext.xml");
// 1、使用类名获取
XXX xxx = SpringContextService.getBean(XXX.class);
// 2、使用bean名获取
Object yyy = SpringContextService.getBean("beanName");

二、通过Spring提供的工具类获取ApplicationContext对象

WebApplicationContext requiredWebApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
// 1、使用类名获取
XXX xxx = SpringContextService.getBean(XXX.class);
// 2、使用bean名获取
Object yyy = SpringContextService.getBean("beanName");

三、继承自抽象类ApplicationObjectSupport

public class SpringTest extends ApplicationObjectSupport {private void test(){// 1、使用类名获取XXX xxx = getApplicationContext().getBean(XXX.class);// 2、使用bean名获取Object yyy = getApplicationContext().getBean("beanName");}
}

四、继承自抽象类WebApplicationObjectSupport

```java
public class SpringTest extends WebApplicationObjectSupport{private void test(){// 1、使用类名获取XXX xxx = getApplicationContext().getBean(XXX.class);// 2、使用bean名获取Object yyy = getApplicationContext().getBean("beanName");}
}

五、实现接口ApplicationContextAware

实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。
Spring初始化时,会通过该方法将ApplicationContext对象注入
1、添加一个类,实现ApplicationContextAware

@Component
public class SpringContextService implements ApplicationContextAware {private static ApplicationContext applicationContext;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {if (SpringContextService.applicationContext == null) {SpringContextService.applicationContext = applicationContext;}}public static  T getBean(Class clazz) {return applicationContext.getBean(clazz);}public static Object getBean(String beanName) {return applicationContext.getBean(beanName);}
}

2、普通类的实际使用

// 1、使用类名获取
XXX xxx = SpringContextService.getBean(XXX.class);
// 2、使用bean名获取
Object yyy = SpringContextService.getBean("beanName");

以上就是普通类获取Spring容器的bean的几种方法,感谢您的阅读!

相关内容

热门资讯

汽车油箱结构是什么(汽车油箱结... 本篇文章极速百科给大家谈谈汽车油箱结构是什么,以及汽车油箱结构原理图解对应的知识点,希望对各位有所帮...
美国2年期国债收益率上涨15个... 原标题:美国2年期国债收益率上涨15个基点 美国2年期国债收益率上涨15个基...
嵌入式 ADC使用手册完整版 ... 嵌入式 ADC使用手册完整版 (188977万字)💜&#...
重大消息战皇大厅开挂是真的吗... 您好:战皇大厅这款游戏可以开挂,确实是有挂的,需要了解加客服微信【8435338】很多玩家在这款游戏...
盘点十款牵手跑胡子为什么一直... 您好:牵手跑胡子这款游戏可以开挂,确实是有挂的,需要了解加客服微信【8435338】很多玩家在这款游...
senator香烟多少一盒(s... 今天给各位分享senator香烟多少一盒的知识,其中也会对sevebstars香烟进行解释,如果能碰...
终于懂了新荣耀斗牛真的有挂吗... 您好:新荣耀斗牛这款游戏可以开挂,确实是有挂的,需要了解加客服微信8435338】很多玩家在这款游戏...
盘点十款明星麻将到底有没有挂... 您好:明星麻将这款游戏可以开挂,确实是有挂的,需要了解加客服微信【5848499】很多玩家在这款游戏...
总结文章“新道游棋牌有透视挂吗... 您好:新道游棋牌这款游戏可以开挂,确实是有挂的,需要了解加客服微信【7682267】很多玩家在这款游...
终于懂了手机麻将到底有没有挂... 您好:手机麻将这款游戏可以开挂,确实是有挂的,需要了解加客服微信【8435338】很多玩家在这款游戏...