【问题标题】:How can I use/import the @EnableRedisHttpSession Spring annotation?如何使用/导入 @EnableRedisHttpSession Spring 注释?
【发布时间】:2020-01-15 15:01:17
【问题描述】:

我正在尝试在 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.html?org/springframework/session/data/redis/config/annotation/web/http/EnableRedisHttpSession.html 的 api 文档中。

我的 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(); 
    }
}

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

【问题讨论】:

    标签: java spring maven spring-boot


    【解决方案1】:

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

    @Configuration
    @EnableRedisHttpSession 
    public class Config {
    

    【讨论】:

    • 谢谢。实际上,我已经/尝试过那里的 @Configuration 注释,它似乎没有任何区别..然后将其拉出,因为上面的 */java-redis.html 链接没有那个。
    • 我现在无法正确测试您的构建,但从过去的测试中,我可以保证 @Configuration 在您在 Spring 下明确声明配置类时总是会有所作为。帮了我很多:howtodoinjava.com/spring-core/spring-configuration-annotation 很抱歉一开始没有解决问题的根源。
    • 感谢收看。 Zgurskyi 的回答似乎有效,但感谢您的链接。当我了解更多关于 Spring 的信息时,我会使用它。
    【解决方案2】:

    将以下依赖项添加到您的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>
    

    基本上,您应该将&lt;type&gt;pom&lt;/type&gt; 依赖项放在pom.xml&lt;dependencyManagement&gt; 部分。另外,你不需要版本,因为你使用spring-boot-starter-parent

    此外,要启用 Spring Boot 自动配置,请在同样使用 @Configuration 注释的类上使用 @EnableRedisHttpSession,或者将其与您的 @SpringBootApplication 一起添加。所以,例如,你应该有这样的东西:

    @Configuration
    @EnableRedisHttpSession
    public class HttpSessionConfig {
        @Bean
        public LettuceConnectionFactory connectionFactory() {
            return new LettuceConnectionFactory();
        }
    }
    

    【讨论】:

    • 谢谢,这似乎有效。 (在我导入了建议的导入 org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession 之后)我很惊讶 spring.io 上的指南显示的部门似乎不起作用。干杯!
    猜你喜欢
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    • 2013-07-22
    • 1970-01-01
    • 2016-04-22
    相关资源
    最近更新 更多