如何为Spring的房东安排时间

px9o7tmv  于 2021-07-26  发布在  Java
关注(0)|答案(3)|浏览(361)

我将对象添加到会话中,如下所示:

  1. session.setAttribute("bucket", new ArrayList<ProductDto>());

我想知道是否有可能确定这个物体的寿命。例如,一小时后它就会自动销毁

nfzehxib

nfzehxib1#

如果您的意思是“会话”的“HTTPServletSession”,那么就没有办法为单个属性配置超时,不管它是不是spring。您只能使整个会话超时。
如果您使用纯spring和ServletWeb应用程序,请在web.xml中配置会话超时。
如果您的意思是“spring”的“spring boot”,请按此处所述配置会话超时:

9wbgstp7

9wbgstp72#

您可以在application.properties文件中设置会话超时

  1. server.session.timeout=1000 # session timeout in second
  2. server.session.cookie.max-age=1000 # Maximum age of the session cookie in seconds. also add if server.session.timeout not working

refer:httpshttp://javadeveloperzone.com/spring-boot/spring-boot-tomcat-session-timeout/

3pmvbmvn

3pmvbmvn3#

您可以使用缓存管理器来实现:

  1. @EnableCaching
  2. @Configuration
  3. public class CacheConfiguration implements CachingConfigurer {
  4. @Override
  5. public CacheManager cacheManager() {
  6. ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager() {
  7. @Override
  8. protected Cache createConcurrentMapCache(final String name) {
  9. return new ConcurrentMapCache(name,
  10. CacheBuilder.newBuilder().expireAfterWrite(30, TimeUnit.MINUTES).maximumSize(100).build().asMap(), false);
  11. }
  12. };
  13. return cacheManager;
  14. }
  15. @Override
  16. public KeyGenerator keyGenerator() {
  17. return new DefaultKeyGenerator();
  18. }
  19. }
展开查看全部

相关问题