ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Spring MVC - ApplicationScope
    Spring/Spring MVC 2021. 12. 29. 22:06
    반응형

    ApplicationScope

    • 서버가 가동될 때부터 서버가 종료되는 시점까지의 범위
    • SevletContext 클래스 타입의 객체로 관리된다.
    • ServletContext에 저장된 데이터나 객체는 서버가 종료되기 전까지 웹브라우저에 관계없이 동일한 메모리 공간을 사용하게 된다.

    SevletContext

    • HttpServletRequest 객체로부터 추출이 가능
    • Controller에서 주입받을 수 있다.

    @Controller
    public class testController {
    
    	@Autowired
    	ServletContext application;
        
    	@GetMapping("/test1")
    //	public String test1(HttpServletRequest request) {
    	public String test1() {
    		
    //		ServletContext application = request.getServletContext();
    		application.setAttribute("data1", "문자열1");
    		
    		return "test1";
    	}
    	
    	@GetMapping("/result1")
    //	public String result1(HttpServletRequest request) {
    	public String result1() {
    		
    //		ServletContext application = request.getServletContext();
    		String data1 = (String)application.getAttribute("data1");
    		
    		System.out.println(data1);
    
    		return "result1";
    	}
    }

     

    @Autowired
    ServletContext application;
     - Controller에서 ServletContext를 주입받을 수 있다.

    // public String test1(HttpServletRequest request) {
    // ServletContext application = request.getServletContext();
     - Controller에서 ServletContext를 주입받지 않았을 때 HttpServletRequest에서 가져올 수 있다.

    public String test1() {
     - Controller에서 주입받았기때문에 HttpServletRequest를 매개변수로 가져올 필요가 없다

     

    반응형

    'Spring > Spring MVC' 카테고리의 다른 글

    Spring MVC - Cookie  (0) 2021.12.30
    Spring MVC - ApplicationScope 빈 주입  (0) 2021.12.30
    Spring MVC - SessionScope 빈 주입  (0) 2021.12.29
    Spring MVC - Session  (0) 2021.12.29
    spring MVC - requestScope 빈 주입  (0) 2021.12.27

    댓글

Designed by Tistory.