我有一個關於Spring-MVC和Spring-websockets的項目,我嘗試在我的服務層上插入緩存。這是我的配置:Spring-MVC + Spring-websocket + @Cacheable不工作
@Configuration
@ComponentScan(basePackages = {
"com.example"
})
@PropertySource("classpath:/configuration.properties")
@EnableWebMvc
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableCaching
public class WebAppConfig extends WebMvcConfigurerAdapter {
@Bean
public EhCacheManagerFactoryBean ehcache() {
EhCacheManagerFactoryBean ehCache = new EhCacheManagerFactoryBean();
ehCache.setConfigLocation(new ClassPathResource("ehcache.xml"));
ehCache.setShared(true);
return ehCache;
}
@Bean
public CacheManager cacheManager() {
return new EhCacheCacheManager(ehcache().getObject());
}
//...different settings by mvc
}
和我的WebSocket配置:
@Configuration
@EnableAsync
@EnableWebSocket
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/queue/", "/topic/");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/locations").withSockJS();
}
@Override
public void configureClientOutboundChannel(ChannelRegistration registration) {
registration.taskExecutor().corePoolSize(4).maxPoolSize(10);
}
}
我想用@Cacheable
詮釋我的服務層上:
@Service
public class StoreServiceImpl implements StoreService {
private static final Log logger = LogFactory.getLog(StoreServiceImpl.class);
@Autowired
private StoreRepository storeRepository;
@Override
@Cacheable("stores")
public Store findById(String storeId) {
return storeRepository.findById(storeId);
}
//... others methods
}
,但如果我已經包含註釋@EnableWebSocketMessageBroker
然後緩存不起作用,因爲aop攔截器不使用它,所以如果我還沒有包含
然後緩存和AOP攔截器運作良好。@EnableWebSocketMessageBroker
上的WebSocket的文檔,我發現這樣的信息:
在某些情況下,控制器可能需要在運行時,AOP代理 裝飾。例如,如果您選擇在控制器上直接使用
@Transactional
註釋。在這種情況下,對於 控制器,我們建議使用基於類的代理。 這通常是控制器的默認選擇。然而,如果一個 控制器必須實現一個非彈簧上下文回調的接口(例如InitializingBean
,* Aware等),您可能需要 顯式配置基於類的代理。例如與<tx:annotation-driven />
,更改爲<tx:annotation-driven proxy-target-class="true" />
我嘗試使用@EnableCaching(proxyTargetClass = true)
,但它並沒有幫助。
有沒有人遇到過這個問題?
您使用的是哪個Spring版本? –
版本4.2.4 @BrianClozel –
然後這可能與[SPR-14030](https://jira.spring.io/browse/SPR-14030)鏈接。 Spring 4.1.x有相同的行爲嗎? –