获取Spring管理的Bean方法
2015-10-10 08:55
471 查看
在Java Application运行中常常需要获取当前JVM里存在的Spring Bean资源,本帖即提供方法在Java中获取Spring管理的Bean
示例项目基于SpringMVC,可以参考我的博文创建Spring MVC项目在STS IDE。
有以下三种方法获得Spring管理的Bean:(推荐方案三)
方案一
当使用Spring的DispatcherServlet方式加载时
所需配置:
【web.xml】配置
获取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】配置
获取Spring管理的Bean实现:
Java代码
方案三(推荐):
通用的方法,将以静态变量保存Spring ApplicationContext,通过Spring在加载时注入,可以在任何需要时提取ApplicationContext使用以获取Bean。
所需配置:
【web.xml】配置
【root-context.xml】配置
【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代码
完成!
示例项目基于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]");
完成!
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- 一个jar包里的网站