我使用帶有@一起@EnableCaching註解噴射/ @自動裝配Autowired在@Configuration上課的時候發現了一個問題:春@EnableCaching與@噴射/ @自動裝配Autowired問題
@Configuration
@EnableCaching
public class CacheConfig {
@Inject
private DataSource dataSource;
@Bean
public CacheManager cacheManager(){
SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("books")));
return cacheManager;
}
@Configuration
static class DevProfileConfig {
@Bean(destroyMethod = "shutdown")
public DataSource dataSource() {
EmbeddedDatabaseFactory factory = new EmbeddedDatabaseFactory();
factory.setDatabaseType(EmbeddedDatabaseType.HSQL);
return factory.getDatabase();
}
}
}
應用程序上下文啓動:
public class CacheConfigLauncher {
public static void main(String args[]){
ApplicationContext springAppContext = new AnnotationConfigApplicationContext(CacheConfig.class);
}
}
埃羅r:
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.cache.CacheManager spring.samples.config.CacheConfig.cacheManager()] threw exception; nested exception is java.lang.IllegalArgumentException: Object of class [null] must be an instance of interface org.springframework.beans.factory.config.ConfigurableBeanFactory at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:188) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:573) ... 76 more
Caused by: java.lang.IllegalArgumentException: Object of class [null] must be an instance of interface org.springframework.beans.factory.config.ConfigurableBeanFactory at org.springframework.util.Assert.isInstanceOf(Assert.java:339) at org.springframework.util.Assert.isInstanceOf(Assert.java:319) at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.getBeanFactory(ConfigurationClassEnhancer.java:414) at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:289) at spring.samples.config.CacheConfig$$EnhancerByCGLIB$$f6ceccea.cacheManager() at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:491) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166) ... 77 more
但是,如果您註釋掉@ Inject'ed字段或@EnableCaching註釋配置將被引導而沒有錯誤!
它接縫就像一個bug對我來說。有人遇到同樣的問題,或者我錯過了smth?
謝謝
奧列格