spring 项目中获取bean 的方法
2012-08-01 16:09
399 查看
1、Web工程:
1)ContextLoaderListener.getCurrentWebApplicationContext ().getBean ("beanName");
2)使用servletContext来获取ApplicationContext
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
这两中方式的区别在于前者在获取失败时会抛出异常,而后者在获取失败时会返回null
3)自己写一个listener实现ServletContextListener接口
web.xml中配置好listener
<listener>
<listener-class>com.ibatis.jpetstore.util.SpringInit</listener-class>
</listener>
listener实现类
public class SpringInit implements ServletContextListener {
private static WebApplicationContext springContext;
public SpringInit() {
super();
}
public void contextInitialized(ServletContextEvent event) {
springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
}
public void contextDestroyed(ServletContextEvent event) {
}
public static ApplicationContext getApplicationContext() {
return springContext;
}
}
2、普通Java工程:
1)写一个类实现ApplicationContextAware接口
/**
*
*/
public class ApplicationContextUtil implements ApplicationContextAware
{
private static ApplicationContext context;//声明一个静态变量保存
@SuppressWarnings ("static-access")
@Override
public void setApplicationContext (ApplicationContext context) throws BeansException
{
this.context = context;
}
public static ApplicationContext getContext(){
return context;
}
}
2)在Spring的配置文件中加入:<bean id="applicationContextUtil" class="net.carefx.platform.mdm.server.core.utils.ApplicationContextUtil" />
1)ContextLoaderListener.getCurrentWebApplicationContext ().getBean ("beanName");
2)使用servletContext来获取ApplicationContext
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
这两中方式的区别在于前者在获取失败时会抛出异常,而后者在获取失败时会返回null
3)自己写一个listener实现ServletContextListener接口
web.xml中配置好listener
<listener>
<listener-class>com.ibatis.jpetstore.util.SpringInit</listener-class>
</listener>
listener实现类
public class SpringInit implements ServletContextListener {
private static WebApplicationContext springContext;
public SpringInit() {
super();
}
public void contextInitialized(ServletContextEvent event) {
springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
}
public void contextDestroyed(ServletContextEvent event) {
}
public static ApplicationContext getApplicationContext() {
return springContext;
}
}
2、普通Java工程:
1)写一个类实现ApplicationContextAware接口
/**
*
*/
public class ApplicationContextUtil implements ApplicationContextAware
{
private static ApplicationContext context;//声明一个静态变量保存
@SuppressWarnings ("static-access")
@Override
public void setApplicationContext (ApplicationContext context) throws BeansException
{
this.context = context;
}
public static ApplicationContext getContext(){
return context;
}
}
2)在Spring的配置文件中加入:<bean id="applicationContextUtil" class="net.carefx.platform.mdm.server.core.utils.ApplicationContextUtil" />
相关文章推荐
- spring项目启动后,获取bean的方法总结
- Web项目中获取SpringBean——在非Spring组件中获取SpringBean
- 获取Spring容器中Bean实例的工具类(Java泛型方法实现)
- Java中Spring获取bean方法小结
- 普通Java类获取spring 容器的bean的5种方法
- Java普通类获取Spring XML中Bean的方法总结
- spring中通过静态工厂方法获取bean实例化对象
- 7 -- Spring的基本用法 -- 10... 获取其他Bean的属性值;获取Field值;获取任意方法的返回值
- 普通Java类获取Spring的Bean的方法
- 获取Spring的Bean持有容器对象的方法
- Spring中获取bean的方法
- spring 获取 bean方法
- spring获取bean的几种方法
- java普通类获取Spring中bean方法
- Java普通类获取Spring XML中Bean的方法总结
- 如何获取spring中存放的各个bean及设置某个bean的自动启动方法
- spring整合的项目中监听器Listener如何获取注入的bean(监听器获取Spring配置文件)
- 普通Java类获取Spring的Bean的方法
- Web项目中获取SpringBean——在非Spring组件中获取SpringBean
- 普通Java类获取spring 容器的bean的5种方法