gpt4 book ai didi

Spring Data Redis 不初始化 RestTemplate

转载 作者:IT王子 更新时间:2023-10-29 06:06:13 28 4
gpt4 key购买 nike

我已经将两个 Spring Data 模块集成到我的应用程序中:Spring Data MongoDB 和 Spring Data Redis。

虽然 Spring Data MongoDB 模块工作得很好,但由于与连接 RestTemplate bean 相关的问题,我正在努力使用 Spring Data Redis,这是与 Redis 交互所必需的。

一旦 Spring Boot 应用程序启动,我将遇到以下错误:

*************************** APPLICATION FAILED TO START


Description:

Parameter 0 of constructor in com.github.wjoz.talkative.messageService.repository.redis.impl.RedisMessageRepositoryImpl required a bean of type 'org.springframework.data.redis.core.RedisTemplate' that could not be found. - Bean method 'redisTemplate' in 'RedisAutoConfiguration.RedisConfiguration' not loaded because @ConditionalOnMissingBean (names: redisTemplate; SearchStrategy: all) found bean 'redisTemplate'

Action:

Consider revisiting the conditions above or defining a bean of type 'org.springframework.data.redis.core.RedisTemplate' in your configuration.

Spring 说 bean 不能被实例化,但是,当我在 redisTemplate() 中调试时,bean 是在 BeanFactory(IoC 容器)中创建和实例化的,因为执行在那里停止。

我有两个 Spring Data 模块,因此 Spring Data 正在进入严格的存储库配置模式,如日志中所述:

Multiple Spring Data modules found, entering strict repository configuration mode!

对于 Spring Data Redis,我既不使用 Repository 也不使用 CrudRepository 接口(interface),相反,由于此处提到的问题,我使用普通的 @Repository 注释实现了它: Repositories - Multiple Modules

我认为 Spring Boot starter 中的某处可能存在版本冲突问题。但我想不通。

提前致谢。

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.github.wjoz</groupId>
<artifactId>talkative-message-service</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>talkative-message-service</name>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>5.0.1.RELEASE</spring.version>
<spring.security.jwt.version>1.0.7.RELEASE</spring.security.jwt.version>
<spring.cloud.starter.version>1.2.3.RELEASE</spring.cloud.starter.version>
<spring.cloud.eureka.server.version>1.3.4.RELEASE</spring.cloud.eureka.server.version>
<oauth.version>2.0.12.RELEASE</oauth.version>
<modelmapper.version>0.7.7</modelmapper.version>
<junit.version>4.12</junit.version>
<h2.version>1.4.193</h2.version>
<log4j2.version>2.8.1</log4j2.version>
<jackson.version>2.8.7</jackson.version>
<apachecommons.version>2.5</apachecommons.version>
</properties>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>

<dependencies>
<!-- Setup Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<!-- Setup Spring MVC & REST, use Embedded Tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>

<!-- Spring Cloud starter -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
<version>${spring.cloud.starter.version}</version>
</dependency>

<!-- Spring Data Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<!-- Spring Data MongoDB -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

<!-- Spring Boot Starter Security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<!-- Spring Boot Starter Security OAuth 2 -->
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>${oauth.version}</version>
</dependency>

<!-- Spring Test Framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>

<!-- Spring Security module for JSON Web Token support -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-jwt</artifactId>
<version>${spring.security.jwt.version}</version>
</dependency>

<!-- Eureka for service registration -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>${spring.cloud.eureka.server.version}</version>
</dependency>

<!-- ModelMapper for DTO/Domain object conversion -->
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>${modelmapper.version}</version>
</dependency>

<!-- JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<!-- Embedded Redis for integration tests -->
<dependency>
<groupId>it.ozimov</groupId>
<artifactId>embedded-redis</artifactId>
<version>0.7.1</version>
<scope>test</scope>
</dependency>

<!-- Spring Boot Hot Swap -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>

<!-- Log4j 2 API -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j2.version}</version>
</dependency>

<!-- Log4j 2 Core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>

<!-- Log4j 2 YAML support -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${apachecommons.version}</version>
</dependency>

</dependencies>

<build>
<resources>
<!--<resource>-->
<!--<directory>src/main/resources</directory>-->
<!--<filtering>true</filtering>-->
<!--<excludes>-->
<!--<exclude>*.jks</exclude>-->
<!--</excludes>-->
<!--</resource>-->
</resources>
<plugins>
<!-- Package as an executable jar -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>

BaseAppConfig - 配置类

    @Configuration
@Profile({ApplicationProfiles.DEVELOPMENT, ApplicationProfiles.PRODUCTION})
@EnableMongoRepositories(basePackages = Packages.MONGO_REPOSITORY)
public class BaseAppConfig {

/**
* Configures the Java client for Redis.
* @return the Java Redis client object
*/
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
return new JedisConnectionFactory();
}

/**
* Provides the central class of the Redis module for Redis interactions.
* @return the Redis interaction class
*/
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(jedisConnectionFactory());
return redisTemplate;
}
}

MongoMessageRepository

    public interface MongoMessageRepository extends CrudRepository<Message, String> {

}

RedisMessageRepositoryImpl - 我尝试 Autowiring RestTemplate 的地方

@Repository
public class RedisMessageRepositoryImpl implements RedisMessageRepository {

private static final String KEY = "Message";
private final RedisTemplate<String, Message> redisTemplate;
private HashOperations<String, String, Message> hashOperations;

@Inject
private RedisMessageRepositoryImpl(RedisTemplate<String, Message> redisTemplate) {
this.redisTemplate = redisTemplate;
}

@Override
public void save(Message message) {
hashOperations.put(KEY, message.getId(), message);
}
}

更新:我已经添加了相当简单的主 MessageServer 类:

@EnableDiscoveryClient
@SpringBootApplication(scanBasePackages = Packages.COMPONENT_ROOT)
@EnableFeignClients
public class MessageServer {
public static void main(String[] args) {
SpringApplication.run(MessageServer.class, args);
}
}

最佳答案

好的,我已经设法找出问题的原因。该 bean 在 IoC 容器中实例化,但由于我的两个愚蠢错误,在尝试将其自动连接到 RedisMessageRepositoryImpl 时出现问题:

首先,我不小心在

中使用了 private修饰符而不是 public
 @Inject
private RedisMessageRepositoryImpl(RedisTemplate<String, Message> redisTemplate) {
this.redisTemplate = redisTemplate;
}

因此即使用@Repository 标记了bean 本身也无法实例化。

我必须删除 RedisTemplate 的通用声明:

private final RedisTemplate<String, Message> redisTemplate;

private final RedisTemplate redisTemplate;

通过这种方式,bean 现在可以毫无问题地 Autowiring ,并且可以在 IntelliJ 中找到,一切都很好。

关于Spring Data Redis 不初始化 RestTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47212722/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com