普通Java类获取Spring的Bean的方法
2014-04-15 16:47
549 查看
普通Java类获取Spring的Bean的方法
在SSH集成的前提下。某些情况我们需要在Action以外的类中来获得Spring所管理的Service对象。之前我在网上找了好几好久都没有找到合适的方法。例如:
ApplicationContext context = new ClassPathXmlApplicationContext(); |
1.创建一个类并让其实现org.springframework.context.ApplicationContextAware接口来让Spring在启动的时候为我们注入ApplicationContext对象.
示例代码:
view plaincopy to clipboardprint? import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class MyApplicationContextUtil implements ApplicationContextAware { private static ApplicationContext context;//声明一个静态变量保存 public void setApplicationContext(ApplicationContext contex) throws BeansException { this.context=contex; } public static ApplicationContext getContext(){ return context; } |
例:
<!-- 这个bean主要是为了得到ApplicationContext 所以它不需要其它属性--> <bean class="org.ing.springutil.MyApplicationContextUtil"></bean> |
相关文章推荐
- 普通Java类获取spring 容器的bean的5种方法
- 普通Java类获取Spring的Bean的方法
- 普通java类获取 spring中的bean方法
- 普通Java类获取spring 容器的bean的5种方法
- 普通java类获取 spring中的bean方法
- 普通java类获取spring容器bean的方法
- 普通Java类获取spring 容器的bean的5种方法
- 普通Java类获取Spring的Bean的方法
- Java普通类获取Spring XML中Bean的方法总结
- java普通类获取Spring中bean方法
- Spring 普通类获取bean的方法
- 普通Java类获取Spring的bean
- 普通Java类获取Spring的bean
- Spring 注解中,普通类获取@Service标记的方法 或者bean对象
- Java普通类获取Spring XML中Bean的方法总结
- 普通Java类获取Spring的bean
- 普通Java类获取Spring的bean - traceofsun的专栏 - 博客频道 - CSDN.NET
- spring中通过静态工厂方法获取bean实例化对象
- 普通Java类获取Spring的ApplicationContext
- 普通静态类方法获取Spring注入的Been实体