How to add an object to application scope in Spring

后端 未结 3 1567
长情又很酷
长情又很酷 2021-02-14 10:06

We can set the request attributes using Model or ModelAndView object in Spring.

We can use @SessionAttributes to keep attributes

3条回答
  •  一个人的身影
    2021-02-14 10:43

    In the spring you can get application scope by using @Autowired annotation

    @Autowired
    private ServletContext servletContext;
    

    Then you can access you element by using .getAttribute Method

    Object someObj = servletContext.getAttribute("object",someObj);
    if(someObj==null)
    someObj = new Object();  //This will create new Object if it doesn't exists.
    servletContext.setAttribute("object",someObj);
    

提交回复
热议问题