获取Spring管理的Bean方法

在Java Application运行中常常需要获取当前JVM里存在的Spring Bean资源,本帖即提供方法在Java中获取Spring管理的Bean

示例项目基于SpringMVC,可以参考我的博文创建Spring MVC项目在STS IDE


有以下三种方法获得Spring管理的Bean:(推荐方案三)

方案一

当使用Spring的DispatcherServlet方式加载时

所需配置:

【web.xml】配置

<!-- Processes application requests -->
<servlet>  
	<servlet-name>appServlet</servlet-name>  
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
	<init-param>  
		<param-name>contextConfigLocation</param-name>  
		<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>  
</servlet>

获取Spring管理的Bean实现:

Java代码

	ServletContext sc = getServletContext();  
	WebApplicationContext attr = (WebApplicationContext)sc.getAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC");  
	sc.getBean("[Bean Name]");

ServletContext的key是org.springframework.web.servlet.FrameworkServlet.CONTEXT.appServlet,

注意后面的appServlet,是servlet-name配置的值,需要根据web.xml的配置适时修改。



方案二:

当使用Spring的ContextLoaderListener方式加载时

所需配置:

【web.xml】配置

<context-param>  
	<param-name>contextConfigLocation</param-name>  
	<param-value>/WEB-INF/spring/root-context.xml</param-value>  
</context-param>  
  
<listener>  
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
</listener> 

获取Spring管理的Bean实现:

Java代码

	ServletContext sc = getServletContext();  
	WebApplicationContext applicationContext  = WebApplicationContextUtils .getWebApplicationContext(sc);   
	applicationContext.getBean("[Bean Name]");



方案三(推荐):

通用的方法,将以静态变量保存Spring ApplicationContext,通过Spring在加载时注入,可以在任何需要时提取ApplicationContext使用以获取Bean。

所需配置:

【web.xml】配置

<context-param>
	<param-name>contextConfigLocation</param-name>  
	<param-value>/WEB-INF/spring/root-context.xml</param-value>  
</context-param>


【root-context.xml】配置

	<bean class="com.tanksoft.spring.general.SpringContextHolder" lazy-init="false" />


【com.tanksoft.spring.general.SpringContextHolder.java】实现类

package com.tanksoft.spring.general;

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

/**
 * @author tanksoft
 *
 */
public class SpringContextHolder implements ApplicationContextAware {
	
	/**
	 * Static Spring ApplicationContext for get spring beans 
	 */ 
	private static ApplicationContext applicationContext;  

	/** 
	 * Initialize ApplicationContext
	 */
	@Override
	public void setApplicationContext(ApplicationContext arg0) throws BeansException {
		SpringContextHolder.applicationContext = applicationContext;
	}
	
	/** 
	 * Get ApplicationContext
	 */  
	public static ApplicationContext getApplicationContext() {  
		checkApplicationContext();  
		return applicationContext;  
	}
	  
	/** 
	 * Get Bean from ApplicationContext with bean name
	 */  
	public static Object getBean(String name) {  
		checkApplicationContext();  
		return applicationContext.getBean(name);  
	}  
	  
	/** 
	 * clean applicationContext 
	 */  
	public static void cleanApplicationContext() {  
		applicationContext = null;  
	}  
	  
	private static void checkApplicationContext() {  
	if (applicationContext == null) {  
		throw new IllegalStateException("applicaitonContext do not exist, please check root-context.xml SpringContextHolder");  
	}  
	}

}

获取Spring管理的Bean实现:

Java代码
	import com.tanksoft.spring.general.SpringContextHolder;   
	......
	SpringContextHolder.getBean("[Bean Name]");

完成!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值