Spring/Spring MVC

Spring MVC - ApplicationScope

jddng 2021. 12. 29. 22:06
728x90
반응형

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를 매개변수로 가져올 필요가 없다

 

728x90
반응형