【问题标题】:Error in Spring boot Session with Redis - No qualifying bean of type [...SessionRepository]使用 Redis 的 Spring 启动会话中的错误 - 没有 [...SessionRepository] ​​类型的限定 bean
【发布时间】:2016-04-08 13:09:36
【问题描述】:

我正在关注spring官方教程,为spring boot添加redis session支持。

http://docs.spring.io/spring-session/docs/current/reference/html5/guides/boot.html

pom.xml

    ...
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.1.RELEASE</version>
    ...
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session</artifactId>
    ... 
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-redis</artifactId>
    ...

我没有从 Spring Boot 的1.3.0.RELEASE 开始将版本添加到 Spring 会话中,jar 包含在其中。即使按照教程添加版本1.0.2.RELEASE 也不能解决我的问题

请注意spring-boot 1.3.1.RELEASE,使用的spring版本是4.2.4.RELEASE

配置

@EnableRedisHttpSession
public class HttpSessionConfig {
}

属性文件

#redis
spring.redis.host=127.0.0.1
spring.redis.port=6379

我没有添加密码,因为我的 redis 服务器密码为空。即使添加密码也没有解决我的问题。

当我运行应用程序时,它会给出以下错误

原因:..NoSuchBeanDefinitionException:没有为依赖项找到类型为 [...SessionRepository] ​​的合格 bean:expect...endency。依赖注释:{}

我还为下面的参考添加了完整的错误堆栈

016-01-05 01:49:50.775 ERROR 7616 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
    ...
    at com.enbiso.proj.estudo.Application.main(Application.java:25) [classes/:na]
Caused by: org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.java:99) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
    ...
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
    ... 8 common frames omitted
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'springSessionRepositoryFilter' defined in class path resource [org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.session.SessionRepository]: : No qualifying bean of type [org.springframework.session.SessionRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.session.SessionRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:464) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    ...
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_20]
    at java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_20]
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.session.SessionRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    ...
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    ... 27 common frames omitted

【问题讨论】:

  • 发布豆类的文件
  • 我没找到你。你想要哪个 bean 文件?与 Redis 相关,我还没有创建任何 bean。我错过了什么吗?
  • 在哪里定义这个 bean SessionRepository
  • 它没有在我的代码中的任何地方定义。根据spring boot不是应该由框架本身启动吗?

标签: java spring spring-mvc spring-boot spring-session


【解决方案1】:

您缺少创建 RedisConnectionFactory 步骤。

试试这个:

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

【讨论】:

    【解决方案2】:

    这些是我解决此问题所遵循的步骤:

    1 - application.properties:

    spring.session.store-type=redis
    spring.redis.host=localhost
    spring.redis.password=Password123OrSomething
    spring.redis.port=1234
    

    2 - pom.xml

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.3.RELEASE</version>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session</artifactId>
        <version>1.3.0.RELEASE</version>
    </dependency>
    
    <dependency>
    <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-redis</artifactId>
        <version>1.7.4.RELEASE</version>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    

    3 - Redis 配置类(Spring Boot):

    @ConditionalOnProperty(name = "spring.session.store-type", havingValue = "redis")
    @EnableRedisHttpSession
    public class RedisSessionConfig {
    
      @Value("${spring.session.store-type}")
      private String sessionStoreType;
    
      private static final Logger LOGGER = LoggerFactory.getLogger(RedisSessionConfig.class);
    
      @PostConstruct
      public void init() {
        LOGGER.info("spring.session.store-type=none turns spring session off.");
        LOGGER.info("Redis Session Replication is turned {}.", sessionStoreType.equals("redis") ? "ON"
            : "OFF");
      }
    
      @Bean
      public ConfigureRedisAction configureRedisAction() {
        LOGGER.info("Preventing auto-configuration in secured environments.");
        return ConfigureRedisAction.NO_OP;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-11-17
      • 2016-11-30
      • 1970-01-01
      • 1970-01-01
      • 2015-03-27
      • 1970-01-01
      • 2017-05-23
      • 2019-03-11
      • 1970-01-01
      相关资源
      最近更新 更多