【问题标题】:What is the difference between "STS - Run As - Spring Boot App" vs. using "gradlew bootRun", for running a Spring Boot Application?“STS - Run As - Spring Boot App”与使用“gradlew bootRun”运行 Spring Boot 应用程序有什么区别?
【发布时间】:2019-11-09 18:14:49
【问题描述】:

Spring Boot 版本:2.1.5.RELEASE

我有一个简单的 Spring Boot MVC 应用程序,它使用 Redis 进行 Spring 会话存储。

spring:

  session:
    store-type: redis

    redis:
      flush-mode: on-save
      namespace: spring:session

  redis:
    host: localhost
    password: 
    port: 6379
    database: 0

当我在 STS(“Run as” - “Spring Boot App”)上运行它时,它使用我的 Redis 安装进行会话存储。我可以看到在 Redis 上添加的键(键 *)。

但是,当我在命令提示符下使用 bootRun 运行它时,与会话相关的一切似乎都可以正常工作,但我在 Redis 服务器上看不到任何键。似乎它甚至没有连接到我的 Redis 服务器。我什至可以安全地关闭 Redis 而不会影响任何东西。

我已确保我在两种运行模式之间具有相同的系统属性。有什么区别?在后一种情况下它可以连接到哪里?

[编辑]

在 org.springframework.session 上启用 TRACE 后,我在日志文件中注意到的一些内容 -

在 STS 上运行时,我看到这些日志条目(预期)-

2019-06-27 14:23:47.086 DEBUG No session found by id: Caching result for getSession(false) for this HttpServletRequest. | HP-20111108 | org.springframework.session.web.http.SessionRepositoryFilter.SESSION_LOGGER | nio-8080-exec-1 
2019-06-27 14:23:47.087 DEBUG A new session was created. To help you troubleshoot where the session was created we provided a StackTrace (this is not an error). You can prevent this from appearing by disabling DEBUG logging for org.springframework.session.web.http.SessionRepositoryFilter.SESSION_LOGGER | HP-20111108 | org.springframework.session.web.http.SessionRepositoryFilter.SESSION_LOGGER | nio-8080-exec-1 
java.lang.RuntimeException: For debugging purposes only (not an error)
    at org.springframework.session.web.http.SessionRepositoryFilter$SessionRepositoryRequestWrapper.getSession(SessionRepositoryFilter.java:332) [spring-session-core-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.session.web.http.SessionRepositoryFilter$SessionRepositoryRequestWrapper.getSession(SessionRepositoryFilter.java:354) [spring-session-core-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    .....
2019-06-27 14:24:00.003 DEBUG Cleaning up sessions expiring at Thu Jun 27 14:24:00 CDT 2019 | HP-20111108 | org.springframework.session.data.redis.RedisSessionExpirationPolicy | pool-2-thread-1 

当使用 bootRun 运行时,这是我看到的唯一日志条目 -

2019-06-27 14:27:05.936  WARN Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [382] milliseconds. | HP-20111108 | org.apache.catalina.util.SessionIdGeneratorBase | nio-8080-exec-1

当使用 bootRun 运行时,Tomcat 会接管会话管理吗?这是怎么发生的?

【问题讨论】:

  • 在这两种情况下您是否使用相同的弹簧型材?
  • 是的。我只有一个个人资料

标签: spring-boot spring-mvc redis


【解决方案1】:

当!看来这是我的错。我在 STS 的项目已关闭。这是一个 Gradle 项目,我对该项目进行了一些更改,但不知道我必须做一个“刷新 Gradle 项目”。无论如何,它们现在是同步的并且行为相似,但可悲的是它的行为方式都是错误的(不使用 Redis),但我想这是一个单独的问题。

【讨论】:

  • 只是这样它会帮助某人-问题是我使用了对“org.springframework.boot:spring-boot-starter-data-redis”的依赖。显然,Spring Boot 需要依赖于“org.springframework.session:spring-session-data-redis”才能使用 Spring Session。
猜你喜欢
  • 2019-01-31
  • 2017-06-22
  • 2020-07-21
  • 1970-01-01
  • 2016-09-17
  • 1970-01-01
  • 1970-01-01
  • 2020-11-06
  • 1970-01-01
相关资源
最近更新 更多