当前位置: 首页 > 知识库问答 >
问题:

配置spring缓存管理器时出现问题

吕琪
2023-03-14

我有一个Spring启动项目,它作为一个库(打包的jar文件)到其他一些项目。我试图哟配置咖啡因缓存,将异步刷新请求后,向服务。

pom.xml(包括):

<dependency>
    <groupId>com.github.ben-manes.caffeine</groupId>
    <artifactId>caffeine</artifactId>
</dependency>

我的配置类:

@EnableCaching
@Configuration
public class CaffeineCacheConfig {

@Bean
public CacheManager cacheManager(){
    CaffeineCacheManager cacheManager = new CaffeineCacheManager("userStories", "features");
    cacheManager.setCaffeine(caffeineCacheBuilder());
    cacheManager.setAllowNullValues(false);
    return cacheManager;
}

Caffeine<Object, Object> caffeineCacheBuilder() {
    return Caffeine.newBuilder()
            .initialCapacity(100)
            .maximumSize(500)
            .refreshAfterWrite(1, TimeUnit.MINUTES)
            .weakKeys()
            .recordStats();
}
}

DAO层(此处需要缓存):

@Component
@EnableCaching
@CacheConfig(cacheNames = {"userStories"})
public class UserStoryDaoImpl implements IUserStoryDao {

@Override
@Cacheable
public List<UserStory> getUserStoriesForProjectAndRelease(UserDto userDto, Set<Integer> reportProjectId, int releaseId) {
    return new ArrayList(); //slow and low performing method that returns a list
}

DAO层(这里也需要缓存):

@Component
@EnableCaching
@CacheConfig(cacheNames = {"features"})
public class FeatureDaoImpl implements IFeatureDao {

@Override
@Cacheable
public List<Features> geFeaturesForProjectAndRelease(UserDto userDto, Set<Integer> reportProjectId, int releaseId) {
    return new ArrayList(); //slow and low performing method that returns a list
}
}

运行此安装程序时,我遇到以下错误堆栈:

2020-03-31 16:55:46,020 ERROR web.context.ContextLoader     - Context initialization failed [localhost-startStop-1] {}
java.lang.NullPointerException
    at org.springframework.context.annotation.AutoProxyRegistrar.registerBeanDefinitions(AutoProxyRegistrar.java:62)
    at org.springframework.context.annotation.ConfigurationClassParser.processImport(ConfigurationClassParser.java:385)
    at org.springframework.context.annotation.ConfigurationClassParser.processImport(ConfigurationClassParser.java:377)
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:205)
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:164)
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:130)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:287)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:225)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:632)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:410)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
    at com.intland.codebeamer.context.CodeBeamerContextListener.lambda$contextInitialized$1(CodeBeamerContextListener.java:117)
    at com.intland.codebeamer.context.CodeBeamerContextListener.logExecutionTime(CodeBeamerContextListener.java:148)
    at com.intland.codebeamer.context.CodeBeamerContextListener.contextInitialized(CodeBeamerContextListener.java:117)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4699)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5165)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:743)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:719)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:714)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1125)
    at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1859)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
2020-03-31 16:55:46,070 INFO  codebeamer.context.CodeBeamerContextListener     - Context - Destroyed [localhost-startStop-1] {}
2020-03-31 16:55:46,132 WARN  context.support.XmlWebApplicationContext     - Exception thrown from ApplicationListener handling ContextClosedEvent [localhost-startStop-1] {}
java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: Root WebApplicationContext: startup date [Tue Mar 31 16:55:35 IST 2020]; root of context hierarchy
    at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:347)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:334)
    at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1051)
    at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:1012)
    at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:586)
    at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:143)
    at com.intland.codebeamer.context.CodeBeamerContextListener.contextDestroyed(CodeBeamerContextListener.java:128)
    at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4746)
    at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5403)
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:226)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:743)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:719)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:714)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1125)
    at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1859)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
2020-03-31 16:55:46,132 WARN  context.support.XmlWebApplicationContext     - Exception thrown from LifecycleProcessor on context close [localhost-startStop-1] {}
java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context: Root WebApplicationContext: startup date [Tue Mar 31 16:55:35 IST 2020]; root of context hierarchy
    at org.springframework.context.support.AbstractApplicationContext.getLifecycleProcessor(AbstractApplicationContext.java:360)
    at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1059)
    at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:1012)
    at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:586)
    at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:143)
    at com.intland.codebeamer.context.CodeBeamerContextListener.contextDestroyed(CodeBeamerContextListener.java:128)
    at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4746)
    at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5403)
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:226)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:743)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:719)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:714)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1125)
    at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1859)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

不确定设置中缺少什么?

共有1个答案

颜博达
2023-03-14

从删除@Component类中的@Enable缓存开始-仅保留在@Configuration上。

不确定@CacheConfig,通常可以在@Cacheable配置缓存名称。

我以前对你的想法有经验。这里的问题是刷新缓存需要一种方法来进行刷新——这种方法会根据键产生最新的值。当键基于方法签名中的多个参数时,实现这样的方法很棘手。这也意味着值加载机制应该实现两次:在注释方法和重载器中。

 类似资料:
  • 我在我的web应用程序中使用Spring启动缓存支持,并将咖啡因设置为缓存提供程序。 我在我的项目中有几个缓存,其中大多数都有公共配置,但是对于两个特定的缓存,我需要设置不同的参数。 在我的中,我有类似的内容: 这是常见的缓存。然后我想使用自定义参数扩展此配置。 这篇文章解释了如何通过配置类配置缓存,但是使用这个方法我完全覆盖了常见的配置。 我需要的是这样的东西: 但是声明一个新的bean,“原始

  • 我在我的应用程序中使用了spring缓存层,我在编写使用Mockito测试spring缓存层的单元测试时遇到了一个问题。 服务层的JUnit测试代码: 例外情况: 我得到了一个“”,因为缓存层没有工作,并且调用被传递到repository对象(两次),该对象返回了上面的'Customer2'模拟对象,即通过传递服务层,对同一个键调用了两次repository方法。

  • 我试图使用Spring Boot与咖啡因和一些注释函数。在我们的测试中,对控制器endpoint的异步调用出现了一些问题,这些问题似乎与我们使用非异步缓存的事实有关。 在做一些研究时,我看到了很多使用Caffeine手动使用的例子,但是在和Spring Boot和注释中找不到任何东西。看起来和具有非常不同的API。是否可以异步使用默认的Spring Boot? 谢谢

  • 目前,我正在使用带有Ehcache的@缓存来使用Spring cache。我将使用Spring Data Redis 2.0.3用Redis替换Ehcache。我在网上看到的所有示例都是基于旧版本的,但是新版本有不同格式的构造函数。 这是我当前的cacheManager配置: 基于旧版本使用Redis的示例如下: 新版本中的构造函数与旧版本完全不同,新版本的所有示例都像这样手动将所有内容放入缓存:

  • 我正在GitHub上的一个repo中托管一个配置。如果我公开回购协议,一切都好,但如果我将其私有化,我将面临: 我用于回购的目标资产是 我应该怎么做才能正确配置私人回购协议,谢谢

  • 我正在Java项目中使用Spring Security4。当我在http元素中指定标记access-decision-manager-ref时,我有以下异常: org.springframework.beans.factory.beanCreationException:创建名为“org.springframework.security.filterchains”的bean时出错:使用键[0]设置