相关:
此问题与this几乎相同,但使用BeanFactoryAware
的答案并不能解决我的问题,因为创建的@Bean
不是@Autowireable。
要求:
@ConfigurationProperties
; @Autowire
在应用程序中创建bean。实施例:
可以通过spring配置声明基于咖啡因的Cache
@Beans
的数量。以下是基于类似问题的接受答案的实施:
首先,启动自动配置模块:
@Component
@ConfigurationProperties
@Data
public class Props {
private LocalCacheConf cache = new LocalCacheConf();
@Data
public static class LocalCacheConf {
/**
* List of caches to create, with accompanying configuration.
*/
private List<CacheDef> caches = new ArrayList<>();
@Data
public static class CacheDef {
private String name;
private CaffeineCacheConf properties;
}
@Data
public static class CaffeineCacheConf {
private long maximumSize = -1;
}
}
}
@Configuration
@Slf4j
public class LocalCacheConfig implements BeanFactoryAware {
@Autowired private Props props;
@Setter private BeanFactory beanFactory;
private CaffeineCacheManager localCacheManager;
@PostConstruct
public void configure() {
setCacheManager();
createCaches( props.getCache() );
}
private void createCaches( LocalCacheConf locCacheConf ) {
ConfigurableBeanFactory configurableBeanFactory = (ConfigurableBeanFactory) beanFactory;
for ( CacheDef cacheProps : locCacheConf.getCaches() ) {
Cache cache = createAndConfigureCache(
cacheProps.getName(),
cacheProps.getProperties()
);
configurableBeanFactory.registerSingleton( cacheProps.getName(), cache );
}
}
private Cache createAndConfigureCache( String name, CaffeineCacheConf props ) {
Caffeine<Object, Object> c = Caffeine.newBuilder().recordStats();
if ( props.getMaximumSize() >= 0 ) {
c.maximumSize( props.getMaximumSize() );
}
// can extend config to include additional properties
localCacheManager.setCaffeine( c );
log.info( "building [{}] cache with config: [{}]", name, c.toString() );
return localCacheManager.getCache( name );
}
private void setCacheManager() {
log.info( "creating {}...", "localCacheManager" );
localCacheManager = new CaffeineCacheManager();
( (ConfigurableBeanFactory) beanFactory )
.registerSingleton( "localCacheManager", localCacheManager );
}
}
最后,using应用程序将通过配置定义其缓存(yaml here):
cache:
caches:
- name: c1
properties:
maximumSize: 50
- name: c2
properties:
maximumSize: 5000
此示例基于类似问题的答案,但是以这种方式创建的缓存@Beans无法在应用程序中自动装配。
在ImportBeanDefinitionRegistrar
中注册bean定义的感觉可能会起作用,但不要认为beanDefs可以与Caffeine
实例一起使用,因为这些实例是通过构建器创建的,而不是属性设置器。
如何实施?
答案 0 :(得分:0)