有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java什么是SpringBoot的会话范围?为什么这样不行?

我有如下SpringBoot应用程序代码:

    class HelloMessageGenerator{
        public static int num = 0;
        public HelloMessageGenerator(){
            System.out.println("new session");
        }

        public void doSomething(){
            System.out.println("doSomething");
        }
    }

    @Controller
    class ScopesController {
        @Resource(name = "sessionScopedBean")
        HelloMessageGenerator sessionScopedBean;

        @Bean
        @SessionScope
        public HelloMessageGenerator sessionScopedBean() {
            return new HelloMessageGenerator();
        }

        @RequestMapping("/")
        public String getSessionScopeMessage(final Model model) {
            sessionScopedBean.doSomething();
            return "hello";
        }
    }

    @SpringBootApplication
    public class DemoApplication {

        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }

    }

我设置了server.sessionTimeout=1

但是当我刷新浏览器页面时,它不会处理HelloMessageGenerator的构造函数

输出如下:

enter image description here

我认为输出应该是:

新会话

doSomething

新会话

doSomething

新会话

doSomething


共 (0) 个答案