转 Spring Boot之No session repository could be auto-configured, check your configuration问题解决

时间:2023-03-08 16:58:31
转 Spring Boot之No session repository could be auto-configured, check your configuration问题解决

1.  环境介绍

JDK 1.8  Spring-Boot 1.5.1.RELEASE, STS IDE

2.  问题的提出

创建了一个非常简约的Spring Boot Web Application,其中使用了Spring-Session,具体的maven依赖如下:

  1. <dependency>
  2. <groupId>org.springframework.session</groupId>
  3. <artifactId>spring-session</artifactId>
  4. lt;/dependency>

在启动过程中,出现了如下错误信息:

  1. Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
  2. 2017-02-23 17:48:10.710 ERROR 29484 --- [  restartedMain] o.s.boot.SpringApplication               : Application startup failed
  3. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.session.SessionAutoConfiguration$SessionRepositoryValidator': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No session repository could be auto-configured, check your configuration (session store type is 'null')
  4. at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137)
  5. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409)
  6. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1620)
  7. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
  8. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
  9. at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
  10. at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
  11. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
  12. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
  13. at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
  14. at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
  15. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
  16. at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
  17. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737)
  18. at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370)
  19. at org.springframework.boot.SpringApplication.run(SpringApplication.java:314)
  20. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)
  21. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)
  22. at org.rosebird.data.stock.StockdataApplication.main(StockdataApplication.java:10)
  23. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  24. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  25. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  26. at java.lang.reflect.Method.invoke(Method.java:497)
  27. at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
  28. Caused by: java.lang.IllegalArgumentException: No session repository could be auto-configured, check your configuration (session store type is 'null')
  29. at org.springframework.util.Assert.notNull(Assert.java:115)
  30. at org.springframework.boot.autoconfigure.session.SessionAutoConfiguration$SessionRepositoryValidator.checkSessionRepository(SessionAutoConfiguration.java:100)
  31. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  32. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  33. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  34. at java.lang.reflect.Method.invoke(Method.java:497)
  35. at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366)
  36. at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311)
  37. at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134)
  38. ... 23 common frames omitted

异常栈很长,关键的错误信息是:No session repository could be auto-configured, check your configuration (session store type is 'null'),这个信息对于我们进行问题分析提供了非常大的指引, 我们将重点放在session store type的功能分析上。

3.  问题的解决与分析

在网上搜索之后,我们发现session store type使用来存放session的存储方式,目前Spring boot中只支持redis方式。 由于本应用暂无需将session放入redis的需求,故这里就可以将session store type设置为none.

这里我们将此配置信息放入application.properites之中:

  1. # default-store in spring session. it will be set in redis only outside.
  2. spring.session.store-type=none

重新启动应用,错误信息消失, 我们可以看到了启动正常的信息了:

    1. 2017-02-23 19:36:17.437  INFO 10500 --- [  restartedMain] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: OrRequestMatcher [requestMatchers=[Ant [pattern='/css/**'], Ant [pattern='/js/**'], Ant [pattern='/images/**'], Ant [pattern='/webjars/**'], Ant [pattern='/**/favicon.ico'], Ant [pattern='/error']]], []
    2. 2017-02-23 19:36:17.823  INFO 10500 --- [  restartedMain] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: OrRequestMatcher [requestMatchers=[Ant [pattern='/**']]], [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@6c7bc3c3, org.springframework.security.web.context.SecurityContextPersistenceFilter@7e251380, org.springframework.security.web.header.HeaderWriterFilter@d2c9b20, org.springframework.security.web.authentication.logout.LogoutFilter@32d5aa81, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@57b802cf, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@6a11f15, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@510cb143, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@69e3d43f, org.springframework.security.web.session.SessionManagementFilter@182c322e, org.springframework.security.web.access.ExceptionTranslationFilter@72545397, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@7c8a6e92]
    3. 2017-02-23 19:36:18.163  INFO 10500 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
    4. 2017-02-23 19:36:18.346  INFO 10500 --- [  restartedMain] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
    5. 2017-02-23 19:36:18.570  INFO 10500 --- [  restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
    6. 2017-02-23 19:36:18.588  INFO 10500 --- [  restartedMain] o.r.data.stock.StockdataApplication      : Started StockdataApplication in 25.393 seconds (JVM running for 27.444)