當前位置:首頁 » 網頁前端 » web獲取springbean
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

web獲取springbean

發布時間: 2022-07-24 13:49:20

『壹』 spring 怎麼獲取bean

Spring在代碼中獲取bean的幾種方式
方法一:在初始化時保存ApplicationContext對象
方法二:通過Spring提供的utils類獲取ApplicationContext對象
方法三:繼承自抽象類ApplicationObjectSupport
方法四:繼承自抽象類WebApplicationObjectSupport
方法五:實現介面ApplicationContextAware
方法六:通過Spring提供的ContextLoader
獲取spring中bean的方式總結:

方法一:在初始化時保存ApplicationContext對象
ApplicationContext ac = new ("applicationContext.xml"); ac.getBean("userService");//比如:<bean id="userService" class="com.cloud.service.impl.UserServiceImpl"></bean>

說明:這樣的方式適用於採用Spring框架的獨立應用程序,須要程序通過配置文件手工初始化Spring的情況。
方法二:通過Spring提供的工具類獲取ApplicationContext對象

ApplicationContext ac1 = WebApplicationContextUtils.(ServletContext sc); ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc); ac1.getBean("beanId"); ac2.getBean("beanId");

說明:這樣的方式適合於採用Spring框架的B/S系統,通過ServletContext對象獲取ApplicationContext對象。然後在通過它獲取須要的類實例。上面兩個工具方式的差別是,前者在獲取失敗時拋出異常。後者返回null。
方法三:繼承自抽象類ApplicationObjectSupport

說明:抽象類ApplicationObjectSupport提供getApplicationContext()方法。能夠方便的獲取ApplicationContext。
Spring初始化時。會通過該抽象類的setApplicationContext(ApplicationContext context)方法將ApplicationContext 對象注入。
方法四:繼承自抽象類WebApplicationObjectSupport

說明:類似上面方法。調用getWebApplicationContext()獲取WebApplicationContext
方法五:實現介面ApplicationContextAware

說明:實現該介面的setApplicationContext(ApplicationContext context)方法,並保存ApplicationContext 對象。Spring初始化時,會通過該方法將ApplicationContext對象注入。

下面是實現ApplicationContextAware介面方式的代碼,前面兩種方法類似:
public class SpringContextUtil implements ApplicationContextAware { // Spring應用上下文環境 private static ApplicationContext applicationContext; /** * 實現ApplicationContextAware介面的回調方法。設置上下文環境 * * @param applicationContext */ public void setApplicationContext(ApplicationContext applicationContext) { SpringContextUtil.applicationContext = applicationContext; } /** * @return ApplicationContext */ public static ApplicationContext getApplicationContext() { return applicationContext; } /** * 獲取對象 * * @param name * @return Object * @throws BeansException */ public static Object getBean(String name) throws BeansException { return applicationContext.getBean(name); } }

盡管,spring提供的後三種方法能夠實如今普通的類中繼承或實現對應的類或介面來獲取spring 的ApplicationContext對象,可是在使用是一定要注意實現了這些類或介面的普通java類一定要在Spring 的配置文件applicationContext.xml文件里進行配置。否則獲取的ApplicationContext對象將為null。
方法六:通過Spring提供的ContextLoader
WebApplicationContext wac = ContextLoader.();wac.getBean(beanID);

最後提供一種不依賴於servlet,不須要注入的方式。可是須要注意一點,在server啟動時。Spring容器初始化時,不能通過下面方法獲取Spring 容器,細節能夠查看spring源代碼org.springframework.web.context.ContextLoader。

『貳』 如何從當前spring容器中獲得bean

1.第一種方式:

在用spring做一個web項目,有一個需求是在沒有servletContext的情況下,根據beanId獲得當前容器中的具體bean對象。請問如何實現?注意,這個beanId可能是外部系統傳過來的,所以無法用@autowire提前注入。
發現

Java代碼

WebApplicationContext wac = ContextLoader.getCurrentWebApplicationCon;

但這個方法只能獲得web容器,我想做的是獲得當前的spring root container即無論當前是通過web還是非web載入的bean都能讓我獲得到。

2.第二種方式(費web工程)


context = new ();

3.第三種方式

寫一個類,實現BeanFactoryAware介面,把該介面配置到spring中,然後把getbean方法寫成靜態的,就可以動態獲取了。下面是示例:

Java代碼

public class Springfactory implements BeanFactoryAware {

private static BeanFactory beanFactory;

// private static ApplicationContext context;

public void setBeanFactory(BeanFactory factory) throws BeansException {
this.beanFactory = factory;
}

/**
* 根據beanName名字取得bean
*
* @param beanName
* @return
*/
public static <T> T getBean(String beanName) {
if (null != beanFactory) {
return (T) beanFactory.getBean(beanName);
}
return null;
}

}

<bean id="springFactoryUtil"
class="com.hundsun.jresplus.mvc.demo.util.SpringFactoryUtil">
</bean>
使用的時候,通過Springfactory.getBean("beanName"),就可以獲取到bean了。注意:這個是靜態方法,直接通過類名去調用。

『叄』 如何通過代碼直接獲得Spring容器中的Bean

[引文]通常在struts2+spring的例子上,要想使用spring的Bean,可以在applicationContext.xml加上如下配置
<bean id="springBean" scope="prototype" class=""
<property name="name" value="chen"/</bean<bean id="myAction" scope="prototype" class=""
<property name="springBean" ref="springBean"/</bean如果是j2ee應用,啟動web應用時將會自動載入ApplicationContext實例(Spring容器負責創建Bean實例)
一旦struts2的myAction實例化,其中的SpringBean也會被自動注入進來,從而達到使用SpringBean的目的。
[問題]但是仍有需要通過代碼來調用SpringBean的情況:
1)對於不是由spring創建管理的類,如在java 代碼中直接使用new去創建一個對象,並且想在這個對象中使用
SpringBean;因為這個對象並不是由Spring容器創建管理的類,所以即使它有setter方法,容器的springBean也不會被注入。
2)動態更改springBean中的屬性值,如在代碼運行時,name值需要發生變動;
3)對於一個獨立的應用程序[解決]定義一個非Spring容器創建管理的類
public class NonSpringClass implements ServletContextAware {
private SpringBean springBean;
//如果 testGetBean不是被Spring容器創建管理,即使它有setter方法,容器的springBean也不會被注入。
public void setSpringBean(SpringBean springBean){this.springBean=springBean;}//利用ApplicationContext 從spring容器中獲得springBean;
//Spring有兩個核心介面BeanFactory和ApplicationContext,其中ApplicationContext是BeanFactory的子介面,
//它們代表了Spring容器,Spring容器是產生Bean的工廠,用於管理容器中的Bean。
public NonSpringClass (){//ApplicationContext acx = new ("applicationContext.xml");
ApplicationContext acx = new ("src/WEB-INF/applicationContext.xml");
springBean=(SpringBean)acx.getBean("springBean");}}調用這個類的代碼:
import com.NonSpringClass;
public class TestCode {
private NonSpringClass nonSpringClass;
//這樣nonSpringClass里將包含Spring容器創建的springBean}}

『肆』 spring 中怎麼取得bean

總共有6種方法可以實現:

方法一:在初始化時保存ApplicationContext對象
方法二:通過Spring提供的utils類獲取ApplicationContext對象
方法三:繼承自抽象類ApplicationObjectSupport
方法四:繼承自抽象類WebApplicationObjectSupport
方法五:通過Spring提供的ContextLoader

獲取spring中bean的方式總結:

方法一:在初始化時保存ApplicationContext對象

ApplicationContext ac = new ("applicationContext.xml"); ac.getBean("userService");//比如:<bean id="userService" class="com.cloud.service.impl.UserServiceImpl"></bean>

說明:這樣的方式適用於採用Spring框架的獨立應用程序,須要程序通過配置文件手工初始化Spring的情況。

方法二:通過Spring提供的工具類獲取ApplicationContext對象

ApplicationContext ac1 = WebApplicationContextUtils.(ServletContext sc); ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc); ac1.getBean("beanId"); ac2.getBean("beanId");

說明:這樣的方式適合於採用Spring框架的B/S系統,通過ServletContext對象獲取ApplicationContext對象。然後在通過它獲取須要的類實例。上面兩個工具方式的差別是,前者在獲取失敗時拋出異常。後者返回null。

方法三:繼承自抽象類ApplicationObjectSupport

說明:抽象類ApplicationObjectSupport提供getApplicationContext()方法。能夠方便的獲取ApplicationContext。

Spring初始化時。會通過該抽象類的setApplicationContext(ApplicationContext context)方法將ApplicationContext 對象注入。

方法四:繼承自抽象類WebApplicationObjectSupport

說明:類似上面方法。調用getWebApplicationContext()獲取WebApplicationContext

方法五:通過Spring提供的ContextLoader

WebApplicationContext wac = ContextLoader.();wac.getBean(beanID);

『伍』 項目中獲取Spring的Bean的幾種方式

但是,對於系統中非Spring框架管理的類,如果需要獲取Spring管理的類,或者,程序中需要動態的根據Bean的id來獲取Bean實例,不可能事先為該類提供所有需要的Bean屬性的setter方法,在類似這樣的情況下,獲取Spring框架管理的類實例的方法有多種,現在簡單總結如下:方法一:在初始化時保存ApplicationContext對象 代碼: ApplicationContext ac = new ("applicationContext.xml"); ac.getBean("beanId");說明:這種方式適用於採用Spring框架的獨立應用程序,需要程序通過配置文件手工初始化Spring的情況。 方法二:通過Spring提供的工具類獲取ApplicationContext對象代碼:import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; ServletContext sc=loc_session.getServletContext(); ApplicationContext ac1 = WebApplicationContextUtils.(sc) ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(sc) ac1.getBean("beanId"); ac2.getBean("beanId");說明:這種方式適合於採用Spring框架的B/S系統,通過ServletContext對象獲取ApplicationContext對象,然後在通過它獲取需要的類實例。 上面兩個工具方式的區別是,前者在獲取失敗時拋出異常,後者返回null。 方法三:繼承自抽象類ApplicationObjectSupport說明:抽象類ApplicationObjectSupport提供getApplicationContext()方法,可以方便的獲取到ApplicationContext。Spring初始化時,會通過該抽象類的setApplicationContext(ApplicationContext context)方法將ApplicationContext 對象注入。方法四:繼承自抽象類WebApplicationObjectSupport說明:類似上面方法,調用getWebApplicationContext()獲取WebApplicationContext 方法五:實現介面ApplicationContextAware說明:實現該介面的setApplicationContext(ApplicationContext context)方法,並保存ApplicationContext 對象。Spring初始化時,會通過該方法將ApplicationContext 對象注入。 以上方法適合不同的情況,請根據具體情況選用相應的方法。

『陸』 如何獲取spring的bean

方法一:在初始化時保存ApplicationContext對象 方法二:通過Spring提供的utils類獲取ApplicationContext對象 方法三:繼承自抽象類ApplicationObjectSupport 方法四:繼承自抽象類WebApplicationObjectSupport 方法五:實現介面ApplicationContextAware 方法六:通過Spring提供的ContextLoader
獲取spring中bean的方式總結:
方法一:在初始化時保存ApplicationContext對象

[html] view plain
ApplicationContext ac = new ("applicationContext.xml");
ac.getBean("beanId");

說明:這種方式適用於採用Spring框架的獨立應用程序,需要程序通過配置文件手工初始化Spring的情況。
方法二:通過Spring提供的工具類獲取ApplicationContext對象
[java] view plain
ApplicationContext ac1 = WebApplicationContextUtils.(ServletContext sc);
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
ac1.getBean("beanId");
ac2.getBean("beanId");

說明:這種方式適合於採用Spring框架的B/S系統,通過ServletContext對象獲取ApplicationContext對象,然後在通過它獲取需要的類實例。上面兩個工具方式的區別是,前者在獲取失敗時拋出異常,後者返回null。
方法三:繼承自抽象類ApplicationObjectSupport
說明:抽象類ApplicationObjectSupport提供getApplicationContext()方法,可以方便的獲取ApplicationContext。
Spring初始化時,會通過該抽象類的setApplicationContext(ApplicationContext context)方法將ApplicationContext 對象注入。
方法四:繼承自抽象類WebApplicationObjectSupport
說明:類似上面方法,調用getWebApplicationContext()獲取WebApplicationContext
方法五:實現介面ApplicationContextAware
說明:實現該介面的setApplicationContext(ApplicationContext context)方法,並保存ApplicationContext 對象。Spring初始化時,會通過該方法將ApplicationContext對象注入。
以下是實現ApplicationContextAware介面方式的代碼,前面兩種方法類似:
[java] view plain
package com.ifaboo.MiomOmmg.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;

/**
* 實現ApplicationContextAware介面的context注入函數, 將其存入靜態變數.
*/
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextUtil.applicationContext = applicationContext; // NOSONAR
}

/**
* 取得存儲在靜態變數中的ApplicationContext.
*/
public static ApplicationContext getApplicationContext() {
checkApplicationContext();
return applicationContext;
}

/**
* 從靜態變數ApplicationContext中取得Bean, 自動轉型為所賦值對象的類型.
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
checkApplicationContext();
return (T) applicationContext.getBean(name);
}

/**
* 從靜態變數ApplicationContext中取得Bean, 自動轉型為所賦值對象的類型.
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(Class<T> clazz) {
checkApplicationContext();
return (T) applicationContext.getBeansOfType(clazz);
}

/**
* 清除applicationContext靜態變數.
*/
public static void cleanApplicationContext() {
applicationContext = null;
}

private static void checkApplicationContext() {
if (applicationContext == null) {
throw new IllegalStateException("applicaitonContext未注入,請在applicationContext.xml中定義SpringContextHolder");
}
}

}

雖然,spring提供的後三種方法可以實現在普通的類中繼承或實現相應的類或介面來獲取spring 的ApplicationContext對象,但是在使用是一定要注意實現了這些類或介面的普通java類一定要在Spring 的配置文件applicationContext.xml文件中進行配置。否則獲取的ApplicationContext對象將為null。
方法六:通過Spring提供的ContextLoader
[java] view plain
WebApplicationContext wac = ContextLoader.();
wac.getBean(beanID);

最後提供一種不依賴於servlet,不需要注入的方式。但是需要注意一點,在伺服器啟動時,Spring容器初始化時,不能通過以下方法獲取Spring 容器,細節可以查看spring源碼org.springframework.web.context.ContextLoader。

『柒』 spring怎麼獲取全部的bean

通過xml配置文件 bean配置在xml裡面,spring提供多種方式讀取配置文件得到ApplicationContext. 第一種方式: 通過程序在初始化的時候,導入Bean配置文件,然後得到Bean實例: ApplicationContext ac = new (applicationContext.xml) ac.getBean(beanName); 第二種方式:WebApplicationContextUtil 在B/S系統中,通常在web.xml初始化bean的配置文件,然後由WebAppliCationContextUtil得到ApplicationContext.例如: ApplicationContext ctx = WebApplicationContextUtils.(ServletContext sc); ApplicationContext ctx =

『捌』 Spring如何獲取Bean

通過xml配置文件
bean配置在xml裡面,spring提供多種方式讀取配置文件得到ApplicationContext.
第一種方式:
通過程序在初始化的時候,導入Bean配置文件,然後得到Bean實例:
ApplicationContext ac = new (applicationContext.xml)
ac.getBean(beanName);
第二種方式:WebApplicationContextUtil
在B/S系統中,通常在web.xml初始化bean的配置文件,然後由WebAppliCationContextUtil得到ApplicationContext.例如:
ApplicationContext ctx = WebApplicationContextUtils.(ServletContext sc);
ApplicationContext ctx =�0�2�0�2 WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
其中 servletContext sc 可以具體 換成 servlet.getServletContext()或者 this.getServletContext() 或者 request.getSession().getServletContext();
另外,由於spring是注入的對象放在ServletContext中的,所以可以直接在ServletContext取出WebApplicationContext 對象:

『玖』 Spring獲取bean的幾種方式

但是,對於系統中非Spring框架管理的類,如果需要獲取Spring管理的類,或者,程序中需要動態的根據Bean的id來獲取Bean實例,不可能事先為該類提供所有需要的Bean屬性的setter方法,在類似這樣的情況下,獲取Spring框架管理的類實例的方法有多種,現在簡單總結如下:方法一:在初始化時保存ApplicationContext對象代碼:ApplicationContext ac = new ("applicationContext.xml"); ac.getBean("beanId");說明: 這種方式適用於採用Spring框架的獨立應用程序,需要程序通過配置文件手工初始化Spring的情況。 方法二:通過Spring提供的工具類獲取ApplicationContext對象代碼:import org.springframework.web.context.support.WebApplicationContextUtils; ApplicationContext ac1 = WebApplicationContextUtils.(ServletContext sc) ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc) ac1.getBean("beanId"); ac2.getBean("beanId");說明: 這種方式適合於採用Spring框架的B/S系統,通過ServletContext對象獲取ApplicationContext對象,然後 在通過它獲取需要的類實例。 上面兩個工具方式的區別是,前者在獲取失敗時拋出異常,後者返回null。 方法三:繼承自抽象類ApplicationObjectSupport說明: 抽象類ApplicationObjectSupport提供getApplicationContext()方法,可以方便的獲取到ApplicationCont ext。Spring初始化時,會通過該抽象類的setApplicationContext(ApplicationContext context)方法將ApplicationContext 對象注入。 方法四:繼承自抽象類WebApplicationObjectSupport說明:類似上面方法,調用getWebApplicationContext()獲取WebApplicationContext 方法五:實現介面ApplicationContextAware說明:實現該介面的setApplicationContext(ApplicationContext context)方法,並保存ApplicationContext 對 象。Spring初始化時,會通過該方法將ApplicationContext 對象注入。 以上方法適合不同的情況,請根據具體情況選用相應的方法。 這里值得提一點的是,系統中用到上述方法的類實際上就於Spring框架緊密耦合在一起了,因為這些類是知 道它們是運行在Spring框架上的,因此,系統中,應該盡量的減少這類應用,使系統盡可能的獨立於當前運 行環境,盡量通過DI的方式獲取需要的服務提供者。 本人認為,方法五比較可行,可以設計一個工具類,專門來獲取Spring中的類。減少對業務代碼的侵入性。