如何为Spring的房东安排时间

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

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

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

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

nfzehxib

nfzehxib1#

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

9wbgstp7

9wbgstp72#

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

server.session.timeout=1000         # session timeout in second 
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#

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

@EnableCaching
@Configuration
public class CacheConfiguration implements CachingConfigurer {

@Override
public CacheManager cacheManager() {
    ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager() {

        @Override
        protected Cache createConcurrentMapCache(final String name) {
            return new ConcurrentMapCache(name,
                CacheBuilder.newBuilder().expireAfterWrite(30, TimeUnit.MINUTES).maximumSize(100).build().asMap(), false);
        }
    };

    return cacheManager;
}

@Override
public KeyGenerator keyGenerator() {
    return new DefaultKeyGenerator();
}

}

相关问题