springmvc中使用多线程问题求解

超级菜鸟 2012-12-27 10:55:22
我在springmvc中保存的时候要开启另一个线程发送邮件,邮件服务也是自己写的service,现在在thread里面要获取当前的applicationcontext,如ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("file:D:\\workspaceXXXXXXXXXXXXX),然后再获取相应的bean,我的想法是这样的,但是在调试的时候,报错
Caused by: 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: InputStreamConfigurationSource [stream=java.io.ByteArrayInputStream@1e0eb8]
at net.sf.ehcache.CacheManager.assertNoCacheManagerExistsWithSameName(CacheManager.java:457)
at net.sf.ehcache.CacheManager.init(CacheManager.java:354)
at net.sf.ehcache.CacheManager.<init>(CacheManager.java:322)
at org.springframework.cache.ehcache.EhCacheManagerFactoryBean.afterPropertiesSet(EhCacheManagerFactoryBean.java:104)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
... 36 more

这个错好像是说虚拟机中已经有一个相同的CacheManager ,这个应该怎么解决呢
...全文
811 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenbinqun 2013-01-24
  • 打赏
  • 举报
回复
引用 3 楼 ghchen 的回复:
这个是ehcache 配置问题 <!-- Ehcache library setup --> <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:shared="true"/>
正解啊!!刚刚我也遇到这样的问题!!用了3楼的方法问题解决了!谢谢呢!
oowangoo 2012-12-31
  • 打赏
  • 举报
回复
如果是bs程序,可以在spring初始化的时候讲容器对象先注入保留,首先配置spring中配置 <bean id="springfactory" class="SpringFactory"></bean>然后在xxxx类实现ApplicationContextAware接口,重写setApplicationContext方法如下
public class SpringFactory implements ApplicationContextAware
{
		
	 private static ApplicationContext context;  
	 @Override  
	 public void setApplicationContext(ApplicationContext applicationContext)  
	            throws BeansException {  
	     this.context=applicationContext;  
	 }  
}
context对象就为spring容器对象
超级菜鸟 2012-12-28
  • 打赏
  • 举报
回复
引用 3 楼 ghchen 的回复:
这个是ehcache 配置问题 <!-- Ehcache library setup --> <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:shared="true"/>
 	<bean id="defaultCacheManager"
   		class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" >
   		<property name="configLocation">
   			<value>classpath:conf/ehcache.xml</value>
   		</property>
   		
   	</bean>
在这个applicationContext里面我应该怎么配置呢
dracularking 2012-12-27
  • 打赏
  • 举报
回复
在thread里面要获取当前的applicationcontext是不是不需要new
dracularking 2012-12-27
  • 打赏
  • 举报
回复
引用 2 楼 xw4265 的回复:
引用 1 楼 dracularking 的回复:在thread里面要获取当前的applicationcontext是不是不需要new 那请问下,怎么获取当前的applicationcontext呢
错了,是CacheManager不唯一的问题,3楼很可能是对的
ghchen 2012-12-27
  • 打赏
  • 举报
回复
这个是ehcache 配置问题 <!-- Ehcache library setup --> <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:shared="true"/>
超级菜鸟 2012-12-27
  • 打赏
  • 举报
回复
引用 1 楼 dracularking 的回复:
在thread里面要获取当前的applicationcontext是不是不需要new
那请问下,怎么获取当前的applicationcontext呢

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧