我正在尝试遵循 https://docs.spring.io/spring-session/docs/current/reference/html5/guides/java-redis.html 上的 Spring 教程.
我添加了列出的依赖项,但 @EnableRedisHttpSession 无法解析为类型。
我是 Spring 新手,所以也许我错过了一些明显的东西,但我确实看到这个注释存在于 api 文档中 https://docs.spring.io/spring-session/docs/current/api/index.html?org/springframework/session/data/redis/config/annotation/web/http/EnableRedisHttpSession.html .
我的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.in28minutes.springboot</groupId>
<artifactId>springtest1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>adamspringtest1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>2.1.8.RELEASE</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
和我的 Config.java :
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
@EnableRedisHttpSession
public class Config {
@Bean
public LettuceConnectionFactory connectionFactory() {
return new LettuceConnectionFactory();
}
}
我错过了什么?任何帮助表示赞赏。
最佳答案
将以下依赖项添加到您的 pom.xml
:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
</dependency>
并删除:
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>2.1.8.RELEASE</version>
<type>pom</type>
</dependency>
基本上,你应该输入 <type>pom</type>
<dependencyManagement>
中的依赖项pom.xml
的部分。另外,您在使用 spring-boot-starter-parent
时不需要版本。 .
此外,要启用 Spring Boot 自动配置,请使用 @EnableRedisHttpSession
类上还注释有 @Configuration
,或者将其与您的 @SpringBootApplication
添加在一起。因此,例如您应该有这样的内容:
@Configuration
@EnableRedisHttpSession
public class HttpSessionConfig {
@Bean
public LettuceConnectionFactory connectionFactory() {
return new LettuceConnectionFactory();
}
}
关于java - 如何使用/导入 @EnableRedisHttpSession Spring 注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57928641/