通过BeanFactory获取bean实例
package org.jiankunking.utils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
/**
* Created by jiankunking on 2016/12/22.
*/
public class SpringBeanUtils implements BeanFactoryAware {
private static BeanFactory beanFactory;
/**
* 注入BeanFactory实例
*/
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
SpringBeanUtils.beanFactory = beanFactory;
}
/**
* 根据bean的名称获取相应类型的对象
*
* @param beanName bean的名称
* @return Object类型的对象
*/
public static Object getBeanByName(String beanName) {
return beanFactory.getBean(beanName);
}
/**
* 根据bean的类型获取相应类型的对象
*
* @param clazz bean的类型,没有使用泛型
* @return Object类型的对象
*/
public static Object getBeanByClass(Class clazz) {
WebApplicationContext wac = ContextLoader
.getCurrentWebApplicationContext();
Object bean = wac.getBean(clazz);
return bean;
}
/**
* 根据bean的名称获取相应类型的对象
*
* @param clazz bean的类型,使用泛型
* @return T类型的对象
*/
public static T getGenericsBeanByClass(Class clazz) {
WebApplicationContext wac = ContextLoader
.getCurrentWebApplicationContext();
T bean = wac.getBean(clazz);
return bean;
}
}
在spring.xml中添加以下部分,已获取工具类bean实例:
通过ApplicationContext获取bean实例
package org.jiankunking.utils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.DefaultResourceLoader;
import java.io.IOException;
/**
* Spring的工具类,用来获得Spring中的bean
*/
public class SpringBeanUtils implements ApplicationContextAware, DisposableBean {
private static ApplicationContext applicationContext;
/**
* 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量.
*/
public void setApplicationContext(ApplicationContext applicationContext) {
SpringBeanUtils.applicationContext = applicationContext; // NOSONAR
}
/**
* 取得存储在静态变量中的ApplicationContext.
*/
public static ApplicationContext getApplicationContext() {
checkApplicationContext();
return applicationContext;
}
/**
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
@SuppressWarnings("unchecked")
public static T getBean(String name) {
checkApplicationContext();
return (T) applicationContext.getBean(name);
}
/**
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
@SuppressWarnings("unchecked")
public static T getBean(Class clazz) {
checkApplicationContext();
return (T) applicationContext.getBean(clazz);
}
/***
* 类似于getBean(String name)只是在参数中提供了需要返回到的类型。
*
* @param name
* @param requiredType
* @return
* @throws BeansException
*/
public static T getBean(String name, Class requiredType) throws BeansException {
return applicationContext.getBean(name, requiredType);
}
/**
* 如果给定的bean名字在bean定义中有别名,则返回这些别名
*
* @param name
* @return
* @throws NoSuchBeanDefinitionException
*/
public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
return applicationContext.getAliases(name);
}
/**
* 判断以给定名字注册的bean定义是一个singleton还是一个prototype。
* 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
*
* @param name
* @return boolean
* @throws NoSuchBeanDefinitionException
*/
public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
return applicationContext.isSingleton(name);
}
/**
* 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
*
* @param name
* @return boolean
*/
public static boolean containsBean(String name) {
return applicationContext.containsBean(name);
}
/**
* 清除applicationContext静态变量.
*/
public static void cleanApplicationContext() {
applicationContext = null;
}
private static void checkApplicationContext() {
if (applicationContext == null) {
throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义UtilsSpringContext");
}
}
public static String getRootRealPath() {
String rootRealPath = "";
try {
rootRealPath = getApplicationContext().getResource("").getFile().getAbsolutePath();
} catch (IOException e) {
//获取系统根目录失败
}
return rootRealPath;
}
public static String getResourceRootRealPath() {
String rootRealPath = "";
try {
rootRealPath = new DefaultResourceLoader().getResource("").getFile().getAbsolutePath();
} catch (IOException e) {
//获取系统根目录失败
}
return rootRealPath;
}
@Override
public void destroy() throws Exception {
SpringBeanUtils.cleanApplicationContext();
}
}
在spring.xml中添加以下部分,已获取工具类bean实例:
本文参考以下两篇文章: http://penghuaiyi.iteye.com/blog/2042296 http://www.tuicool.com/articles/numyuq
作者:jiankunking 出处:http://blog.csdn.net/jiankunking