- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我需要更改 Redis 代码,以便它可以与新的 SpringBoot 2.0.3 一起使用,目前在启动 Tomcat 9.0.12 时(不是以 Jar 形式启动 SpringBoot - 业务需要的原因)我收到以下错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method vehicleHistoryCacheManager in somePath.config.UfCacheConfig
required a bean of type 'org.springframework.data.redis.connection.RedisConnectionFactory' that could not be found.
- Bean method 'redisConnectionFactory' in 'JedisConnectionConfiguration' not loaded because @ConditionalOnMissingBean
(types: org.springframework.data.redis.connection.RedisConnectionFactory; SearchStrategy: all)
beans of type 'org.springframework.data.redis.connection.RedisConnectionFactory' redisConnectionFactory
- Bean method 'redisConnectionFactory' in 'LettuceConnectionConfiguration' not loaded because @ConditionalOnMissingBean
(types: org.springframework.data.redis.connection.RedisConnectionFactory; SearchStrategy: all)
found beans of type 'org.springframework.data.redis.connection.RedisConnectionFactory' redisConnectionFactory
Action:
Consider revisiting the conditions above or defining a bean of type 'org.springframework.data.redis.connection.RedisConnectionFactory' in your configuration.
19-Oct-2018 13:43:22.142 SEVERE [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start:
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/motoPolicy]]
(...)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'technicalController'
defined in file [somePath\TecController.class]:
Unsatisfied dependency expressed through constructor parameter 0;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'ufCacheServiceImpl'
defined in URL [jar:file:/someName.jar!/somePath/UfCacheServiceImpl.class]:
Unsatisfied dependency expressed through constructor parameter 0;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'vehicleHistoryCacheManager' defined in class path resource
[somePath/config/UfCacheConfig.class]:
Unsatisfied dependency expressed through method 'vehicleHistoryCacheManager' parameter 0;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'org.springframework.data.redis.connection.RedisConnectionFactory' available:
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=vehicleTemplate)}
(...)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ufgCacheServiceImpl'
defined in URL [jar:file:/uf.jar!/somePath/UfgCacheServiceImpl.class]:
Unsatisfied dependency expressed through constructor parameter 0;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'vehicleHistoryCacheManager' defined in class path resource
[somePath/config/UfCacheConfig.class]:
Unsatisfied dependency expressed through method 'vehicleHistoryCacheManager' parameter 0;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'org.springframework.data.redis.connection.RedisConnectionFactory' available:
expected at least 1 bean which qualifies as autowire candidate.
Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=vehicleTemplate)}
(...)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'vehicleHistoryCacheManager' defined in class path resource
[somePath/config/UfCacheConfig.class]:
Unsatisfied dependency expressed through method 'vehicleHistoryCacheManager' parameter 0;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'org.springframework.data.redis.connection.RedisConnectionFactory' available:
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=vehicleTemplate)}
(...)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'org.springframework.data.redis.connection.RedisConnectionFactory' available:
expected at least 1 bean which qualifies as autowire candidate.
Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=vehicleTemplate)}
(...)
19-Oct-2018 13:43:22.162 SEVERE [RMI TCP Connection(3)-127.0.0.1] org.apache.tomcat.util.modeler.BaseModelMBean.invoke Exception invoking method manageApp
java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException:
Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/motoPolicy]]
(...)
[2018-10-19 01:43:22,176] Artifact motoPolicy-web:war exploded: Error during artifact deployment. See server log for details.
(...)
19-Oct-2018 13:43:22.162 SEVERE [RMI TCP Connection(3)-127.0.0.1] org.apache.tomcat.util.modeler.BaseModelMBean.invoke Exception invoking method createStandardContext
javax.management.RuntimeOperationsException: Exception invoking method manageApp
(...)
Caused by: java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException:
Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/motoPolicy]]
(...)
我在 pom 中对 Redis 的依赖是:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.0</version>
</dependency>
这是redis的配置:
@Configuration
class RedisConfig {
@Value("${ufg.redis.host}") // 127.0.0.1
private String hostName;
@Value("${ufg.redis.port}") // 6379
private int port;
@Bean
LettuceConnectionFactory redisConnectionFactory() {
LettuceConnectionFactory redisConnectionFactory = new LettuceConnectionFactory();
redisConnectionFactory.setHostName(hostName);
redisConnectionFactory.setPort(port);
return redisConnectionFactory;
}
@Bean
ObjectMapper redisObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
objectMapper.registerModule(new Jdk8Module());
objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+1:00"));
objectMapper.setDateFormat(new ISO8601DateFormat());
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
return objectMapper;
}
}
这是配置类:
@Slf4j
@Configuration
@EnableCaching
public class UfCacheConfig {
public static final String VEHICLE_HISTORY_CACHE_MANAGER = "vehicleHistoryCacheManager";
public static final String VEHICLE_HISTORY_CACHE = "vehicleHistoryCache";
public static final String VEHICLE_GENERATOR_NAME = "vehicleKeyGenerator";
public static final String PERSON_HISTORY_CACHE_MANAGER = "personHistoryCacheManager";
public static final String PERSON_HISTORY_CACHE = "personHistoryCache";
public static final String PERSON_GENERATOR_NAME = "personKeyGenerator";
@Value("${ufg.cache.expiration.validity.minutes}")
private int expirationValidityMinutes;
@Bean(value ="vehicleTemplate")
public RedisTemplate<String, GetVehicleInsuranceHistoryResponse> vehicleTemplate(RedisConnectionFactory redisConnectionFactory,
@Qualifier("redisObjectMapper") ObjectMapper objectMapper) {
RedisTemplate<String, GetVehicleInsuranceHistoryResponse> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
@Primary
@Bean(name = VEHICLE_HISTORY_CACHE_MANAGER)
public CacheManager vehicleHistoryCacheManager(@Qualifier("vehicleTemplate") RedisConnectionFactory redisConnectionFactory) {
Duration expiration = Duration.ofSeconds(expirationValidityMinutes * 60);
return RedisCacheManager.builder(redisConnectionFactory)
.cacheDefaults(RedisCacheConfiguration.defaultCacheConfig().entryTtl(expiration)).build();
}
@Bean(value ="personTemplate")
public RedisTemplate<String, GetPersonInsuranceHistoryResponse> redisTemplate(RedisConnectionFactory redisConnectionFactory,
@Qualifier("redisObjectMapper") ObjectMapper objectMapper) {
RedisTemplate<String, GetPersonInsuranceHistoryResponse> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
@Bean(name = PERSON_HISTORY_CACHE_MANAGER)
public CacheManager personHistoryCacheManager(@Qualifier("personTemplate") RedisConnectionFactory redisConnectionFactory) {
Duration expiration = Duration.ofSeconds(expirationValidityMinutes * 60);
return RedisCacheManager.builder(redisConnectionFactory)
.cacheDefaults(RedisCacheConfiguration.defaultCacheConfig().entryTtl(expiration)).build();
}
@Bean(name = VEHICLE_GENERATOR_NAME)
public KeyGenerator vehicleKeyGenerator() {
return (o, method, objects) -> new VehicleKeyGenerator((GetVehicleInsuranceHistoryRequest) objects[0]).generate();
}
@Bean(name = PERSON_GENERATOR_NAME)
public KeyGenerator personKeyGenerator() {
return (o, method, objects) -> new PersonKeyGenerator((GetPersonInsuranceHistoryRequest) objects[0]).generate();
}
private static <T> RedisTemplate<String, T> createTemplate(Class<T> clazz, RedisConnectionFactory redisConnectionFactory, ObjectMapper objectMapper) {
RedisTemplate<String, T> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
Jackson2JsonRedisSerializer<T> serializer = new Jackson2JsonRedisSerializer<>(clazz);
serializer.setObjectMapper(objectMapper);
redisTemplate.setValueSerializer(serializer);
return redisTemplate;
}
}
这是堆栈跟踪中提到的服务类:
@Slf4j
@Service
public class UfCacheServiceImpl implements UfCacheService {
private CacheManager vehicleCacheManager;
private CacheManager personCacheManager;
@Autowired
public UfCacheServiceImpl(@Qualifier(VEHICLE_HISTORY_CACHE_MANAGER) CacheManager vehicleCacheManager,
@Qualifier(PERSON_HISTORY_CACHE_MANAGER) CacheManager personCacheManager) {
this.vehicleCacheManager = vehicleCacheManager;
this.personCacheManager = personCacheManager;
}
@Override
public void clear() {
log.info("Clearing ufg cache");
vehicleCacheManager.getCache(VEHICLE_HISTORY_CACHE).clear();
personCacheManager.getCache(PERSON_HISTORY_CACHE).clear();
}
}
如您所见,我使用了类似的东西,但 redis 不起作用:
@Bean(value ="vehicleTemplate")
@Qualifier("vehicleTemplate")
编辑:
关于 Boris
的回答:我能找到的(在整个项目中)唯一带有像 jedis
或 Jedis
这样的短语的东西是 pom 依赖项。当我删除它们时,输出更改为:
Parameter 0 of method vehicleHistoryCacheManager in somePackages.config.UfCacheConfig required a bean of type 'org.springframework.data.redis.connection.RedisConnectionFactory' that could not be found.
- Bean method 'redisConnectionFactory' not loaded because @ConditionalOnClass did not find required class 'redis.clients.jedis.Jedis'
- Bean method 'redisConnectionFactory' in 'LettuceConnectionConfiguration' not loaded because @ConditionalOnMissingBean (types: org.springframework.data.redis.connection.RedisConnectionFactory;
SearchStrategy: all) found beans of type 'org.springframework.data.redis.connection.RedisConnectionFactory' redisConnectionFactory
我找不到像 JedisConnectionConfiguration
这样的东西来删除它。
在 pom 中注释依赖项后,我在外部库中找不到短语 jedis
。
我在 External Libraries
中看到 spring-boot-starter-parent
版本(在主父 pom 中)我只有 spring-data- redis
2.0.8.
最佳答案
您已放置 @ConditionalOnMissingBean仅当 BeanFactory 中不包含 RedisConnectionFactory
类的 bean 时才匹配的注解。查看日志,我们可以看到您正在尝试同时配置 Lettuce 和 Jedis 客户端库:
- Bean method 'redisConnectionFactory' in 'JedisConnectionConfiguration' not loaded because @ConditionalOnMissingBean
- Bean method 'redisConnectionFactory' in 'LettuceConnectionConfiguration' not loaded because @ConditionalOnMissingBean
但是您应该一次只使用一个。如果您想使用 Lettuce,请删除 JedisConnectionConfiguration
并删除 jedis
依赖项,反之亦然。
但是我建议改用 Redis Starter:
1. 添加spring-boot-starter-data-redis
maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2. 移除jedis
和spring-data-redis
maven 依赖
3. 删除@Qualifier 注释,因为您不需要对 autowiring selection process 进行更多控制
4. 使用 setConnectionFactory() 设置连接工厂
redisTemplate.setConnectionFactory(connectionFactory);
5. 使用 Redis Starter 提供的基本自动配置
删除 redisConnectionFactory()
方法。
像任何其他 Spring Bean 一样注入(inject)一个自动配置的 RedisConnectionFactory
或 RedisTemplate
实例以连接到 Redis。
6. 使用 Spring Boot Redis 属性
Spring Boot 使用这些默认的 Redis 属性,您可以在属性文件中覆盖这些属性:
spring.redis.host=localhost
spring.redis.port=6379
参见 here了解更多详情。
关于java - Spring boot 2 从 Spring boot 1.5 迁移到 redis 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52891979/
有人可以解释一下 spring-boot-parent 和 spring-boot-starter-parent 之间的区别吗,正如我在下面附加的 GIT HUB 代码链接之一中看到的,他们为 spr
我有与 jersey 框架集成的 Spring Boot 应用程序。 现在,当我尝试运行该应用程序时,它只是停留在 Spring 启动徽标上,之后没有任何 react 。 我也尝试添加 -X ,但徽标
我指的是 Spring Boot 关于 的文档自动配置 和 执行器 模块: 自动配置: Spring Boot AutoConfiguration attempts to automatically
我正在尝试将 apache log4j 集成到我的 Spring boot 应用程序中。这是我的 build.gradle 文件: build.gradle buildscript { rep
使用 Spring Boot Maven 插件的以下命令在生产中启动 Spring Boot 应用程序是否是一个好主意或实践? mvn spring-boot:run 最佳答案 不,这是个坏主意。 您
据我所知,spring boot 和 spring session 为我们提供了一站式自动配置,但是当我的应用程序使用 session redis 和应用程序缓存 redis 时,不是同一个 redi
我希望使用Spring Boot创建一个新的Web应用程序。不幸的是,我的服务器在技术堆栈方面相当有限。它安装了Java 5。 谁能告诉我spring boot是否可以在Java 1.5上运行以及什么
我有3个实体 CarWash(设置Wash) Wash(car_wash_id FK到CarWash) WashComment(wash_id FK到Wash) 有什么办法可以写这个查询 @Qu
我一直在关注this文章。 我正在尝试在Spring-boot应用程序中优雅地处理gRPC错误,的主要目标是能够在gRPC客户端中获取错误状态。 在上面的文章之后,我坚持为异常添加拦截器。如何在Spr
我有一个要使用的自定义log4j布局插件。在IntelliJ中运行或与./gradlew bootRun一起运行时,插件可以正常工作。不使用./gradlew bootJar构建启动jar。 启用-D
我想在给定范围 (5001-5100) 的随机端口上启动 Spring Cloud 应用程序(Spring Boot 1.5.14,Spring Cloud Edgware.SR4)。我知道我们可以使
任何人都可以向我展示或指出不使用 spring boot gradle 插件的 spring boot gradle 项目。 我正在寻找类似不使用 gradle 插件的 spring boot sta
我当时尝试包含上述依赖项之一,但找不到任何区别: spring boot starter web:我可以看到 Flux 和 Mono 类并制作一个响应式(Reactive)休息 Controller
我们一直在为我们的应用程序使用 Springboot 1.X。 现在准备开始一些新的应用程序,想知道我们是应该使用 SpringBoot2.0 还是坚持使用 SpringBoot 1.X? 对一种方式
我希望记录应用程序正在加载 application-profile.propeties 或 application.yml。怎么做。在哪种方法中,我可以听取它并检测它是成功加载还是失败。 最佳答案 您
当我在 pom.xml 中添加简单的 spring-boot-starter-data-jpa 依赖项时,在 pom.xml 文件中出现错误。如果我删除该依赖项,则不会再有错误。我不确定为什么会发生这
我希望记录应用程序正在加载 application-profile.propeties 或 application.yml。怎么做。在哪种方法中,我可以听取它并检测它是成功加载还是失败。 最佳答案 您
我在网上看了很多关于 spring-boot-devtools 的文章和问题,但仍然无法弄清楚为什么它对我不起作用。每次运行我的应用程序时,我都会得到以下信息: 17:54:28.057 [main]
我正在尝试将现有的 Spring 应用程序移植到 Spring Boot。我不使用 spring-boot-starter-data-solr 启动器,但是我的类路径上有 apache solrj (
(这主要是一个历史问题。Pivotal 建议所有论坛讨论都在 StackOverflow 上进行,这就是我在这里问它的原因。) Spring Boot 项目用来证明将应用程序的类和依赖项从可执行 ja
我是一名优秀的程序员,十分优秀!