2

我正在尝试遵循https://docs.spring.io/spring-session/docs/current/reference/html5/guides/java-redis.html上的 Spring 教程。
我添加了列出的依赖项,但 @EnableRedisHttpSession 无法解析为类型。

我是 Spring 新手,所以也许我遗漏了一些明显的东西,但我确实看到这个注释存在于https://docs.spring.io/spring-session/docs/current/api/index 的 api 文档中。 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(); 
    }
}

我错过了什么?任何帮助表示赞赏。

4

2 回答 2

4

将以下依赖项添加到您的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();
    }
}
于 2019-09-13T18:43:42.027 回答
1

我要说的第一枪是您的配置类中缺少的“@Configuration”。这是我在您的 Config.class 和文档中的示例之间看到的明显区别。也许先试试这个,如果问题仍然存在,至少你会有一个例外在这里显示。:D

@Configuration
@EnableRedisHttpSession 
public class Config {
于 2019-09-13T18:39:24.823 回答