When I integrate Spring cache (with EHCache) and Spring validation, proxy order is not being correctly set.
I want the validation annotation to be processed BEFORE cache annotations (CacheEvict in this case) are processed.
My configuration class:
@EnableCaching( order = Ordered.LOWEST_PRECEDENCE )
@Configuration
public class CommonConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() throws IOException {
@Bean
public MethodValidationPostProcessor methodValidationPostProcessor( LocalValidatorFactoryBean validator ) {
MethodValidationPostProcessor methodValidationPostProcessor = new MethodValidationPostProcessor();
methodValidationPostProcessor.setValidator( validator );
methodValidationPostProcessor.setOrder( Ordered.LOWEST_PRECEDENCE- 1 );
return methodValidationPostProcessor;
}
@Bean
public EhCacheCacheManager cacheManager() {
EhCacheCacheManager cacheManager = new EhCacheCacheManager();
cacheManager.setCacheManager( ehCacheManager().getObject() );
return cacheManager;
}
@Bean
public EhCacheManagerFactoryBean ehCacheManager() {
EhCacheManagerFactoryBean ehcache = new EhCacheManagerFactoryBean();
ehcache.setConfigLocation( new ClassPathResource( "ehcache.xml" ) );
return ehcache;
}
}
And my related dependencies:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.2</version>
</dependency>
The interface and implementing class with the problem:
@Validated
public interface MyRepository {
public void update(@NotNull MyEntity entity);
}
@Repository
public class MyRepositoryImpl implements MyRepository {
@CacheEvict(cacheNames = {"entityCache"});
public void update(MyEntity entity);
}
As it can be seen I have set order to MethodValidationPostProcessor to Orderer.LOWEST_PRECEDENCE- 1 and cache order to Ordered.LOWEST_PRECEDENCE (in @EnacbleCaching).
Is that the correct way to set proxy order?
Can be the problem be related to having validation annotation in the interface and cache annotation in the implementation?