簡體   English   中英

Spring-MVC + Spring-websocket + @Cacheable不起作用

[英]Spring-MVC + Spring-websocket + @Cacheable don't work

我在Spring-MVC和Spring-websockets上有一個項目,我嘗試在服務層上插入緩存。 這些是我的配置:

@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攔截器不使用它,因此,如果我沒有包含 @EnableWebSocketMessageBroker 則緩存和AOP攔截器將可以正常工作。

我在websocket上的文檔中找到了以下信息:

在某些情況下,可能需要在運行時用AOP代理修飾控制器。 一個示例是,如果您選擇直接在控制器上具有@Transactional批注。 在這種情況下,特別是對於控制器,我們建議使用基於類的代理。 這通常是控制器的默認選擇。 但是,如果控制器必須實現不是Spring Context回調的接口(例如InitializingBean ,* Aware等),則可能需要顯式配置基於類的代理。 例如,使用<tx:annotation-driven /> ,更改為<tx:annotation-driven proxy-target-class="true" />

我嘗試使用@EnableCaching(proxyTargetClass = true) ,但沒有幫助。

有沒有人遇到這個問題?

我決定了這個問題:我在@EnableAsync(mode = AdviceMode.ASPECTJ)中更改了模式,並且可以正常工作。 我認為這取決於訂單初始化BeanPostProcessors

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM