Spring @EnableCaching @ Inject / @ Autowired问题

时间:2013-05-22 12:40:59

标签: spring caching autowired inject

我在@Configuration类中使用@EnableCaching注释和@Inject / @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);
    }
}

错误:

  

引起:   org.springframework.beans.factory.BeanDefinitionStoreException:   工厂方法[public org.springframework.cache.CacheManager   spring.samples.config.CacheConfig.cacheManager()]抛出异常;   嵌套异常是java.lang.IllegalArgumentException:Object of   class [null]必须是interface的实例   org.springframework.beans.factory.config.ConfigurableBeanFactory at   org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:188)     在   org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:573)     ......还有76个

     

引起:java.lang.IllegalArgumentException:类的对象[null]   必须是接口的实例   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)     在   org.springframework.context.annotation.ConfigurationClassEnhancer $ BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:289)     在   spring.samples.config.CacheConfig $$ EnhancerByCGLIB $$ f6ceccea.cacheManager()     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:491)at   org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166)     ......还有77个

但是如果您注释掉@Inject'ed字段或@EnableCaching注释配置将被引导而没有错误!!!

它对我来说就像一个小虫。有人遇到同样的问题,或者我可能错过了什么?

谢谢,

奥列格

1 个答案:

答案 0 :(得分:0)

问题已在Spring v4.0.0.M2修复!