我在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 ,这个应该怎么解决呢
错了,是CacheManager不唯一的问题,3楼很可能是对的这个是ehcache 配置问题
<!-- Ehcache library setup -->
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:shared="true"/>如果是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容器对象