我将对象添加到会话中,如下所示:
session.setAttribute("bucket", new ArrayList<ProductDto>());
我想知道是否有可能确定这个物体的寿命。例如,一小时后它就会自动销毁
nfzehxib1#
如果您的意思是“会话”的“HTTPServletSession”,那么就没有办法为单个属性配置超时,不管它是不是spring。您只能使整个会话超时。如果您使用纯spring和ServletWeb应用程序,请在web.xml中配置会话超时。如果您的意思是“spring”的“spring boot”,请按此处所述配置会话超时:
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
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/
3pmvbmvn3#
您可以使用缓存管理器来实现:
@EnableCaching@Configurationpublic class CacheConfiguration implements CachingConfigurer {@Overridepublic 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;}@Overridepublic KeyGenerator keyGenerator() { return new DefaultKeyGenerator();}}
@EnableCaching
@Configuration
public class CacheConfiguration implements CachingConfigurer {
@Override
public CacheManager cacheManager() {
ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager() {
protected Cache createConcurrentMapCache(final String name) {
return new ConcurrentMapCache(name,
CacheBuilder.newBuilder().expireAfterWrite(30, TimeUnit.MINUTES).maximumSize(100).build().asMap(), false);
}
};
return cacheManager;
public KeyGenerator keyGenerator() {
return new DefaultKeyGenerator();
3条答案
按热度按时间nfzehxib1#
如果您的意思是“会话”的“HTTPServletSession”,那么就没有办法为单个属性配置超时,不管它是不是spring。您只能使整个会话超时。
如果您使用纯spring和ServletWeb应用程序,请在web.xml中配置会话超时。
如果您的意思是“spring”的“spring boot”,请按此处所述配置会话超时:
9wbgstp72#
您可以在application.properties文件中设置会话超时
refer:httpshttp://javadeveloperzone.com/spring-boot/spring-boot-tomcat-session-timeout/
3pmvbmvn3#
您可以使用缓存管理器来实现: