在Listener中手动getBean后,无法自动装配bean

J2EE 码拜 9年前 (2015-04-22) 1093次浏览 0个评论

在web.xml中定义了一个初始化资源的listener,这个listener需要从数据库中加载系统配置表,因此有了一下代码
        <listener>  
    <listener-class>  
        cn.com.fensbook.base.system.source.InitSystemParamListener
    </listener-class>  
</listener>

public class InitSystemParamListener implements ServletContextListener {
	    private static Log  LOG = LogFactory.getLog(InitSystemParamListener.class);
	    public void contextDestroyed(ServletContextEvent arg0) {}
	    public void contextInitialized(ServletContextEvent arg0) {
	    setParams(arg0);
	}

	/**
	 * 取得所有配置项
	 * @return
	 */
	private List<SystemParam> getParams(ServletContextEvent servletContext){

	    ApplicationContext context =new FileSystemXmlApplicationContext("classpath:spring-base.xml");
	    ISystemParamService systemParamService = (ISystemParamService) context.getBean("systemParamService");
	    return systemParamService.getAllParam();
	}

	private void setParams(ServletContextEvent servletContext){

	    List<SystemParam> params = getParams(servletContext);

	    for(SystemParam  param : params){
		    LOG.info(param.getParamName()+"==============="+param.getParamValue());
		    servletContext.getServletContext().setAttribute(param.getParamName(), param.getParamValue());

		}

	}

}

到此处代码运行一切正常,但是我访问我的Action时,action通过
         

 @Autowired
	@Qualifier("userInfoService")
	private IUserInfoService userInfoService;

注入service时,却报了如下错误
Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following: 1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary 2. Shutdown the earlier cacheManager before creating new one with same name. The source of the existing CacheManager is: DefaultConfigurationSource [ ehcache.xml or ehcache-failsafe.xml ] 
Error creating bean with name “”sessionFactory”” defined in URL [file:/E:/codingTool/apache-tomcat-6.0.39/webapps/fensbook/WEB-INF/classes/spring-base.xml]: Invocation of init method failed; nested exception is net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following: 1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary 2. Shutdown the earlier cacheManager before creating new one with same name. The source of the existing CacheManager is: DefaultConfigurationSource [ ehcache.xml or ehcache-failsafe.xml ] 
Error creating bean with name “”userInfoDao””: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name “”sessionFactory”” defined in URL [file:/E:/codingTool/apache-tomcat-6.0.39/webapps/fensbook/WEB-INF/classes/spring-base.xml]: Invocation of init method failed; nested exception is net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following: 1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary 2. Shutdown the earlier cacheManager before creating new one with same name. The source of the existing CacheManager is: DefaultConfigurationSource [ ehcache.xml or ehcache-failsafe.xml ] 
Could not autowire field: private cn.com.fensbook.userinfo.dao.IUserInfoDAO cn.com.fensbook.userinfo.service.impl.UserInfoServiceImpl.userInfoDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name “”userInfoDao””: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name “”sessionFactory”” defined in URL [file:/E:/codingTool/apache-tomcat-6.0.39/webapps/fensbook/WEB-INF/classes/spring-base.xml]: Invocation of init method failed; nested exception is net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following: 1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary 2. Shutdown the earlier cacheManager before creating new one with same name. The source of the existing CacheManager is: DefaultConfigurationSource [ ehcache.xml or ehcache-failsafe.xml ] 
Error creating bean with name “”userInfoService””: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.com.fensbook.userinfo.dao.IUserInfoDAO cn.com.fensbook.userinfo.service.impl.UserInfoServiceImpl.userInfoDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name “”userInfoDao””: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name “”sessionFactory”” defined in URL [file:/E:/codingTool/apache-tomcat-6.0.39/webapps/fensbook/WEB-INF/classes/spring-base.xml]: Invocation of init method failed; nested exception is net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following: 1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary 2. Shutdown the earlier cacheManager before creating new one with same name. The source of the existing CacheManager is: DefaultConfigurationSource [ ehcache.xml or ehcache-failsafe.xml ] 

哪位大神可以解决这个问题,跪谢

在Listener中手动getBean后,无法自动装配bean
up,up ,up
在Listener中手动getBean后,无法自动装配bean
看起来是Spring自己的容器已经加载了一些bean,然后你自己又加载classpath:spring-base.xml文件,再次创建bean导致的。
建议将classpath:spring-base.xml交给Spring统一管理。
在Listener中手动getBean后,无法自动装配bean
引用 2 楼 oh_Maxy 的回复:

看起来是Spring自己的容器已经加载了一些bean,然后你自己又加载classpath:spring-base.xml文件,再次创建bean导致的。
建议将classpath:spring-base.xml交给Spring统一管理。

 将spring-base.xml交给spring统一管理的话,在listener中能自动装配service bean么?

在Listener中手动getBean后,无法自动装配bean
100分
引用 3 楼 abc282822943 的回复:
Quote: 引用 2 楼 oh_Maxy 的回复:

看起来是Spring自己的容器已经加载了一些bean,然后你自己又加载classpath:spring-base.xml文件,再次创建bean导致的。
建议将classpath:spring-base.xml交给Spring统一管理。

 将spring-base.xml交给spring统一管理的话,在listener中能自动装配service bean么?

将你的Listener也纳入Spring的管理试试呗,或者让这个Listener实现ApplicationContextAware接口,得到ApplicationContext句柄,就可以直接getBean的。

在Listener中手动getBean后,无法自动装配bean
引用 3 楼 abc282822943 的回复:
Quote: 引用 2 楼 oh_Maxy 的回复:

看起来是Spring自己的容器已经加载了一些bean,然后你自己又加载classpath:spring-base.xml文件,再次创建bean导致的。
建议将classpath:spring-base.xml交给Spring统一管理。

 将spring-base.xml交给spring统一管理的话,在listener中能自动装配service bean么?

好的,我试试,感谢大神。

在Listener中手动getBean后,无法自动装配bean
引用 5 楼 abc282822943 的回复:
Quote: 引用 3 楼 abc282822943 的回复:
Quote: 引用 2 楼 oh_Maxy 的回复:

看起来是Spring自己的容器已经加载了一些bean,然后你自己又加载classpath:spring-base.xml文件,再次创建bean导致的。
建议将classpath:spring-base.xml交给Spring统一管理。

 将spring-base.xml交给spring统一管理的话,在listener中能自动装配service bean么?

好的,我试试,感谢大神。

这种整合我没尝试过。
其实Servlet能实现的功能,SpringMVC都能实现,何不直接都交给Spring做呢?


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明在Listener中手动getBean后,无法自动装配bean
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!