Java類獲取Spring中bean的5種方式
獲取Spring中的bean有很多種方式,再次總結(jié)一下:
第一種:在初始化時保存ApplicationContext對象
ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
ac.getBean("beanId");
說明:這種方式適用于采用Spring框架的獨立應(yīng)用程序,需要程序通過配置文件手工初始化Spring。
第二種:通過Spring提供的工具類獲取ApplicationContext對象
import org.springframework.web.context.support.WebApplicationContextUtils;
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
ac1.getBean("beanId");
ac2.getBean("beanId");
說明:
1、這兩種方式適合于采用Spring框架的B/S系統(tǒng),通過ServletContext對象獲取ApplicationContext對象,然后在通過它獲取需要的類實例;
2、第一種方式在獲取失敗時拋出異常,第二種方式返回null。
第三種:繼承自抽象類ApplicationObjectSupport
說明:通過抽象類ApplicationObjectSupport提供的getApplicationContext()方法可以方便的獲取到ApplicationContext實例,進而獲取Spring容器中的bean。Spring初始化時,會通過該抽象類的setApplicationContext(ApplicationContext context)方法將ApplicationContext 對象注入。
第四種:繼承自抽象類WebApplicationObjectSupport
說明:和上面方法類似,通過調(diào)用getWebApplicationContext()獲取WebApplicationContext實例;
第五種:實現(xiàn)接口ApplicationContextAware
說明:實現(xiàn)該接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext對象。Spring初始化時,會通過該方法將ApplicationContext對象注入。
雖然Spring提供了后三種方法可以實現(xiàn)在普通的類中繼承或?qū)崿F(xiàn)相應(yīng)的類或接口來獲取Spring的ApplicationContext對象,但是在使用時一定要注意繼承或?qū)崿F(xiàn)這些抽象類或接口的普通java類一定要在Spring的配置文件(即application-context.xml文件)中進行配置,否則獲取的ApplicationContext對象將為null。
下面通過實現(xiàn)接口ApplicationContextAware的方式演示如何獲取Spring容器中的bean:
首先自定義一個實現(xiàn)了ApplicationContextAware接口的類,實現(xiàn)里面的方法:
package com.ghj.tool;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class SpringConfigTool implements ApplicationContextAware {// extends ApplicationObjectSupport{
private static ApplicationContext ac = null;
private static SpringConfigTool springConfigTool = null;
public synchronized static SpringConfigTool init() {
if (springConfigTool == null) {
springConfigTool = new SpringConfigTool();
}
return springConfigTool;
}
public void setApplicationContext(ApplicationContext applicationContext)throws BeansException {
ac = applicationContext;
}
public synchronized static Object getBean(String beanName) {
return ac.getBean(beanName);
}
}
其次在applicationContext.xml文件進行配置:
最后通過如下代碼就可以獲取到Spring容器中相應(yīng)的bean了:
注意一點,在服務(wù)器啟動Spring容器初始化時,不能通過以下方法獲取Spring容器:
import org.springframework.web.context.ContextLoader; import org.springframework.web.context.WebApplicationContext; WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); wac.getBean(beanID);
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助。
相關(guān)文章
如何使用Bean Validation 解決業(yè)務(wù)中參數(shù)校驗
這篇文章主要介紹了如何使用Bean Validation 解決業(yè)務(wù)中參數(shù)校驗操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
Java Apollo環(huán)境搭建以及集成SpringBoot案例詳解
這篇文章主要介紹了Java Apollo環(huán)境搭建以及集成SpringBoot案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08
SpringCloud的網(wǎng)關(guān)Zuul和Gateway詳解
SpringCloudZuul和SpringCloudGateway都是用于構(gòu)建微服務(wù)架構(gòu)中的API網(wǎng)關(guān)的組件,但SpringCloudGateway在性能、功能特性和生態(tài)支持等方面有一些優(yōu)勢,因此推薦使用SpringCloudGateway作為首選2025-02-02
Java中java.lang.ClassCastException異常原因及解決方法
大家好,本篇文章主要講的是Java中java.lang.ClassCastException異常原因及解決方法,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01

