在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]");
完成!