I want to have an annotation where it will pull in a different config class based on the value of property. It might work something like this:
(All of this is pseudo code)
The annotation:
import org.springframework.context.annotation.Import;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Import(RedisConfig.class)
@Import(HazelCastConfig.class)
public @interface EnableCaching
{
Class cacheType() default Redis.class;
}
An example of one of the configs:
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.cache.annotation.EnableCaching;
import java.util.List;
@EnableCaching
@ConditionalOnExpression("#{T(com.mypackage.CacheAnnotationMatcher).checkCacheType(com.mypackage.Redis.class)}")
public class RedisConfig
{
//...elided...
}
Where CacheAnnotationMatcher.checkProperty
is a class and method I create that finds the @EnableCaching
annotation and checks the cacheType
property and returns true/false.
I think you could use
@ConditionalOnExpression
here, I see absolutely no reason why your code wouldn't work, have your tried it?