我是春季靴子的新手。尝试实现ehcache,但收到以下错误:
Handler dispatch failed; nested exception is NoClassDefFoundError: org/springframework/transaction/support/TransactionSynchronizationManager
没有记录任何内容,只有一个Debug语句
2018-05-30 14:09:47.291 DEBUG 5520 --- [nio-9091-exec-2] .m.m.a.ExceptionHandlerExceptionResolver : Resolving exception from handler [public org.springframework.http.ResponseEntity<?> com.jetblue.api.controller.AirportLocationController.getAirport(com.jetblue.api.domain.Location,org.springframework.validation.BindingResult)]: org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/transaction/support/TransactionSynchronizationManager
2018-05-30 14:09:47.293 DEBUG 5520 --- [nio-9091-exec-2] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'customizedResponseEntityExceptionHandler'
缓存配置
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
@Configuration
@EnableCaching
public class CacheConfiguration {
@Bean
public EhCacheManagerFactoryBean ehCacheManagerFactory() {
EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean();
cacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
cacheManagerFactoryBean.setShared(true);
return cacheManagerFactoryBean;
}
@Bean
public EhCacheCacheManager ehCacheCacheManager() {
EhCacheCacheManager cacheManager = new EhCacheCacheManager();
cacheManager.setCacheManager(ehCacheManagerFactory().getObject());
cacheManager.setTransactionAware(true);
return cacheManager;
}
}
服务层:
@Override
public NearByAirport getAirport(Location location) {
............
}
接口:
public interface IAirportLocatorService {
/**
* Gets the airport.
*
* @param airport the airport
* @return the airport
*/
@Cacheable(value="nearestAirport", key="#location.latAndLong")
public NearByAirport getAirport(Location location);
}
ehcache.xml中
<cache name="nearestAirport" eternal="false"
maxElementsInMemory="250" overflowToDisk="false" diskPersistent="false"
timeToIdleSeconds="21600" timeToLiveSeconds="21600"
memoryStoreEvictionPolicy="LRU" />
我尝试将@Cachable注释放在服务层而不是服务上,但同样的问题。 任何指针都将非常感激。感谢。
答案 0 :(得分:0)
我认为您的问题是cacheManager.setTransactionAware(true);
。这将导致Spring将缓存链接到事务(使用XA或其虚假版本,我不确定)。因此,如果事务回滚,那么对缓存的修改也是如此。
但是你现在没有使用Spring事务管理器,因为它不在你的类路径中。所以春天不开心。做cacheManager.setTransactionAware(false);
应该可以解决问题。