普通javaBean如何获取到spring托管对象
2015-03-28 12:33
645 查看
一些thread类或servlet不能通过spring注解的方式调用spring容器里面的类
尝试将thread或servlet加上@component或@controller注解变成被spring容器管理,再调用spring容器里面的其他类,失败!
最终找出下面两种解决方案:
一,通过spring配置文件applicationContext.xml初始化
[java] view plaincopy
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class SpringUtil{
private static ApplicationContext applicationContext = null;
public static ApplicationContext getApplicationContext() {
if(applicationContext == null){
applicationContext = new FileSystemXmlApplicationContext("applicationContext.xml");
}
return applicationContext;
}
}
缺点:
这里面的applicationContext.xml是全文件路径,这也是这种方式不是很灵活的原因之一,
调用SpringUtil会创建一次ApplicationContext对象.如果将static去掉改为非静态,那么每次调用该方法时都会new一个该对象,效率应该会更差.
二,通过实现ApplicationContextAware接口
在spring初始化时,会通过该接口实现的setApplicationComtext方法将ApplicationContext对象注入到该类中,具体见下面
我的解决方式选择该种:
1、定义SpringUtil
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class SpringUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public void setApplicationContext(ApplicationContext applicationContext)throws BeansException {
SpringUtil.applicationContext = applicationContext;
}
}
2、spring的配置文件加入
<bean id="applicationContext" class="com.lefu.pushCore.other.SpringUtil"/>
3、通过如下方法便能获取到所有由spring管理的对象了:
PushController pushController = (PushController) SpringUtil.getApplicationContext().getBean("pushController");
参考文章:http://blog.csdn.net/a19881029/article/details/7842070
尝试将thread或servlet加上@component或@controller注解变成被spring容器管理,再调用spring容器里面的其他类,失败!
最终找出下面两种解决方案:
一,通过spring配置文件applicationContext.xml初始化
[java] view plaincopy
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class SpringUtil{
private static ApplicationContext applicationContext = null;
public static ApplicationContext getApplicationContext() {
if(applicationContext == null){
applicationContext = new FileSystemXmlApplicationContext("applicationContext.xml");
}
return applicationContext;
}
}
缺点:
这里面的applicationContext.xml是全文件路径,这也是这种方式不是很灵活的原因之一,
调用SpringUtil会创建一次ApplicationContext对象.如果将static去掉改为非静态,那么每次调用该方法时都会new一个该对象,效率应该会更差.
二,通过实现ApplicationContextAware接口
在spring初始化时,会通过该接口实现的setApplicationComtext方法将ApplicationContext对象注入到该类中,具体见下面
我的解决方式选择该种:
1、定义SpringUtil
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class SpringUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public void setApplicationContext(ApplicationContext applicationContext)throws BeansException {
SpringUtil.applicationContext = applicationContext;
}
}
2、spring的配置文件加入
<bean id="applicationContext" class="com.lefu.pushCore.other.SpringUtil"/>
3、通过如下方法便能获取到所有由spring管理的对象了:
PushController pushController = (PushController) SpringUtil.getApplicationContext().getBean("pushController");
参考文章:http://blog.csdn.net/a19881029/article/details/7842070
相关文章推荐
- Spring如何管理Java普通类(Java类获取Spring容器的bean)
- 普通类中获取spring容器中的javabean对象
- java普通类获取Spring中bean方法
- Java普通类获取Spring XML中Bean的方法总结
- spring中普通类如何获取session,request等对象
- 非spring托管对象如何获取到spring托管对象
- SpringBoot普通类中如何获取其他bean例如Service、Dao(转)
- 使用反射创建Bean、Spring中是如何根据类名配置创建Bean实例、Java提供了Class类获取类别的字段和方法,包括构造方法
- SpringBoot普通类中如何获取其他bean例如Service、Dao
- 为Servlet或普通Java程序注入Spring托管的Bean、数据源
- 为Servlet或普通Java程序注入Spring托管的Bean、数据源
- 在普通java类里获取Spring管理的bean
- java 从spring容器中获取注入的bean对象
- 解析Java中如何获取Spring中配置的bean
- Java普通类获取Spring XML中Bean的方法总结
- java 获取spring配置文件中bean 对象
- 使用反射创建Bean、Spring中是如何根据类名配置创建Bean实例、Java提供了Class类获取类别的字段和方法,包括构造方法
- 如何在spring普通的bean中获取session对象
- Spring 注解中,普通类获取@Service标记的方法 或者bean对象
- 解析Java中如何获取Spring中配置的bean