2

我在 @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] must be an instance of 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.util.Assert.isInstanceOf(Assert.java:339) 处的接口 org.springframework.beans.factory.config.ConfigurableBeanFactory 的实例org.springframework.util.Assert.isInstanceOf(Assert.java:319) 在 org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.getBeanFactory(ConfigurationClassEnhancer.java:414) 在 org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor。拦截(ConfigurationClassEnhancer.java:289)在 spring.samples.config.CacheConfig$$EnhancerByCGLIB$$f6ceccea.cacheManager() 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java :57) 在太阳。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 更多

但是,如果您注释掉 @Inject'ed 字段或 @EnableCaching 注释配置将被引导而不会出错!!!

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

谢谢,

奥列格

4

1 回答 1

0

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

于 2013-11-12T14:38:14.427 回答