Config.class 中的 CacheManager bean 定义导致 NoSuchBeanDefinitionException

CacheManager bean definition in Config.class leads to NoSuchBeanDefinitionException

我有一个 Spring 服务正在检查数据库条目。为了尽量减少我的存储库调用,两个查找方法都是“@Cacheable”。但是当我尝试初始化我的服务 bean 而我的配置 class 有一个 CacheManager bean 定义时,我得到了 NoSuchBeanDefinitionException:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'foo.mediacode.directory.MediaCodeDirectoryService' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1093)
at foo.mediacode.directory.MediaCodeDirectoryService.implementation(MediaCodeDirectoryService.java:63)
at foo.campaigntree.directory.CampaignTreeDirectoryService.<init>(CampaignTreeDirectoryService.java:18)
... 15 more

如果我取出 CacheManager bean 定义,我可以初始化我的服务 bean 并且它 运行s 没有任何问题和缓存!

这是我的代码: 配置

...
    @Configuration
    @EnableCaching
    @EnableJpaRepositories(...)
    @PropertySource({...})
    public class MediaCodeDirectoryServiceConfig {

        private static Logger   configLogger    = Logger.getLogger(MediaCodeDirectoryServiceConfig.class.getName());

        @Value("${jpa.loggingLevel:FINE}")
        private String          loggingLevel;

        @Value("${mysql.databaseDriver}")
        private String          dataBaseDriver;

        @Value("${mysql.username}")
        private String          username;

        @Value("${mysql.password}")
        private String          password;

        @Value("${mysql.databaseUrl}")
        private String          databaseUrl;

        @Bean
        public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
            ...
        }

        @Bean
        public MediaCodeDirectoryService mediaCodeDirectoryService() {
            return new MediaCodeDirectoryService();
        }

        @Bean
        public CacheManager mediaCodeCacheManager() {
            SimpleCacheManager cacheManager = new SimpleCacheManager();
            cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("mediaCodeMappingRegexCache"),
                    new ConcurrentMapCache("mediaCodeMappingsCache")));

            return cacheManager;
        }

        @Bean
        public JpaTransactionManager transactionManager() {
            ...
        }

        @Bean
        public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
            ...
        }

        public DataSource getDataSource() {
            ...
        }

        public JpaDialect getJpaDialect() {
            ...
        }

        public Properties getEclipseLinkProperty() {
            ...
        }

        public JpaVendorAdapter getJpaVendorAdapter() {
            ...
        }
    }

服务

....
    public class MediaCodeDirectoryService implements MediaCodeDirectoryServiceApi {

        ...

        @Autowired
        private MediaCodeDirectoryRepository repo;

        @SuppressWarnings("resource")
        public static MediaCodeDirectoryServiceApi implementation() {
        if (INSTANCE == null) {
                ApplicationContext ctx = new AnnotationConfigApplicationContext(MediaCodeDirectoryServiceConfig.class);
                INSTANCE = ctx.getBean(MediaCodeDirectoryService.class);
            }

            return INSTANCE;
        }
...

存储库

...
@Repository
public interface MediaCodeDirectoryRepository extends CrudRepository<MediaCodeDao, Integer> {

    @Cacheable("mediaCodeMappingRegexes")
    @Query("SELECT m FROM  #{#entityName} m WHERE (m.fooId = :fooId) AND (m.isRegex = :isRegex) ORDER BY (m.orderId DESC, m.id ASC)")
    List<MediaCodeDao> findByfooIdAndIsRegexOrderByOrderIdDescAndIdAsc(@Param("fooId") int fooId, @Param("isRegex") boolean isRegex);

    @Cacheable("mediaCodeMappings")
    List<MediaCodeDao> findByMediaCode(String MediaCode, Pageable pageable);
}

当我调试 DefaultListableBeanFactory 时,我可以在 beanDefinitionMap 中找到我的 mediaCodeDirectoryService 以及 beanDefinitionNames mediaCodeDirectoryService[=40] =]出现。但是 DefaultListableBeanFactory.getBean(...) 无法解析名称并且第 364 行中的 namedBean 为空。

当我尝试通过字符串获取上下文时:

INSTANCE = (MediaCodeDirectoryService) ctx.getBean("mediaCodeDirecotryService")

我避免了 NoSuchBeanDefinitionException 但我 运行 进入了另一个异常。

这里有人知道这可能是什么原因吗?我是否遗漏了配置中的某些内容?谢谢!

通过 AOP 应用缓存。对于 AOP Spring 使用基于代理的方法,默认是创建基于接口的代理。

public class MediaCodeDirectoryService implements MediaCodeDirectoryServiceApi {... }

在运行时使用此 class 定义,您将获得一个动态创建的 class(Proxy 或类似的东西),它实现了所有接口,但它不是 MediaCodeDirectoryService。然而它是 MediaCodeDirectoryServiceApi.

你有两种方法来解决这个问题,要么编程到接口(你应该一直这样做,因为你已经定义了接口)而不是具体的 classes 或使用基于 class 的代理。

第一个选项涉及您在直接 @Autowire 的地方更改代码或获取 MediaCodeDirectoryService 的实例以使用 MediaCodeDirectoryServiceApi 代替(恕我直言,您应该已经这样做了,为什么否则定义一个接口)。现在您将注入代理,一切都会正常工作。

第二个选项涉及在 @EnableCaching 注释上设置 proxyTargetClass=true。然后,您将获得基于 class 的代理,而不是基于接口的代理。

@EnableCaching(proxyTargetClass=true)