ImportAware应用
@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) @Import({RedissonHttpSessionConfiguration.class}) @Configuration public @interface EnableRedissonHttpSession { int maxInactiveIntervalInSeconds() default 1800; String keyPrefix() default ""; }
@Configuration public class RedissonHttpSessionConfiguration extends SpringHttpSessionConfiguration implements ImportAware { private Integer maxInactiveIntervalInSeconds; private String keyPrefix; ... public void setImportMetadata(AnnotationMetadata importMetadata) { Map<String, Object> map = importMetadata.getAnnotationAttributes(EnableRedissonHttpSession.class.getName()); AnnotationAttributes attrs = AnnotationAttributes.fromMap(map); this.keyPrefix = attrs.getString("keyPrefix"); this.maxInactiveIntervalInSeconds = (Integer)attrs.getNumber("maxInactiveIntervalInSeconds"); } }
@Component public class MyDb implements ImportAware { private int maxConnections; @Override public void setImportMetadata(AnnotationMetadata annotationMetadata) { Map<String, Object> attributesMap = annotationMetadata.getAnnotationAttributes(EnableMyDb.class.getName()); AnnotationAttributes attrs = AnnotationAttributes.fromMap(attributesMap); this.maxConnections = attrs.getNumber("maxConnections"); System.out.println(this.maxConnections); } public void store(){ System.out.println(this.maxConnections); }
@Retention(RetentionPolicy.RUNTIME) @Import(MyDb.class) public @interface EnableMyDb { int maxConnections() default 1000; }
@Configuration @ComponentScan("com.tian") //初始化插件值 @EnableMyDb(maxConnections = 2000) public class AppConfig { }