Posts Spring Web MVC 3
Post
Cancel

Spring Web MVC 3

Spring Web MVC 3


๐Ÿ’ฟ Application Context ๊ณผ Servlet Context

Application Context

  • Web Application ์ตœ์ƒ๋‹จ์— ์œ„์น˜ํ•˜๊ณ  ์žˆ๋Š” Context
  • Spring์—์„œ ApplicationContext๋ž€ BeanFactory๋ฅผ ์ƒ์†๋ฐ›๊ณ  ์žˆ๋Š” Context
  • Spring์—์„œ root-context.xml, applicationContext.xml ํŒŒ์ผ์€ ApplicationContext ์ƒ์„ฑ ์‹œ ํ•„์š”ํ•œ ์„ค์ •์ •๋ณด๋ฅผ ๋‹ด์€ ํŒŒ์ผ (Bean ์„ ์–ธ ๋“ฑ..)
  • Spring์—์„œ ์ƒ์„ฑ๋˜๋Š” Bean์— ๋Œ€ํ•œ IoC Container (๋˜๋Š” Bean Container) ํŠน์ • Servlet์„ค์ •๊ณผ ๊ด€๊ณ„ ์—†๋Š” ์„ค์ •์„ ํ•œ๋‹ค (@Service, @Repository, @Configuration, @Component)
  • ์„œ๋กœ ๋‹ค๋ฅธ ์—ฌ๋Ÿฌ Servlet์—์„œ ๊ณตํ†ต์ ์œผ๋กœ ๊ณต์œ ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” Bean์„ ์„ ์–ธํ•œ๋‹ค.
  • Application Context์— ์ •์˜๋œ Bean์€ Servlet Context์— ์ •์˜ ๋œ Bean์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

Servlet-Context (servlet-context.xml)

  • Servlet ๋‹จ์œ„๋กœ ์ƒ์„ฑ๋˜๋Š” context
  • Spring์—์„œ servlet-context.xml ํŒŒ์ผ์€ DispatcherServlet ์ƒ์„ฑ ์‹œ์— ํ•„์š”ํ•œ ์„ค์ • ์ •๋ณด๋ฅผ ๋‹ด์€ ํŒŒ์ผ (Interceptor, Bean์ƒ์„ฑ, ViewResolver๋“ฑ..)
  • URL์„ค์ •์ด ์žˆ๋Š” Bean์„ ์ƒ์„ฑ (@Controller, Interceptor)
  • Application Context๋ฅผ ์ž์‹ ์˜ ๋ถ€๋ชจ Context๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
  • Application Context์™€ Servlet Context์— ๊ฐ™์€ id๋กœ ๋œ Bean์ด ๋“ฑ๋ก ๋˜๋Š” ๊ฒฝ์šฐ, Servlet Context์— ์„ ์–ธ๋œ Bean์„ ์‚ฌ์šฉํ•œ๋‹ค.
  • Bean ์ฐพ๋Š” ์ˆœ์„œ
    • Servlet Context์—์„œ ๋จผ์ € ์ฐพ๋Š”๋‹ค.
    • ๋งŒ์•ฝ Servlet Context์—์„œ bean์„ ๋ชป์ฐพ๋Š” ๊ฒฝ์šฐ Application Context์— ์ •์˜๋œ bean์„ ์ฐพ๋Š”๋‹ค.
  • Servlet Context์— ์ •์˜๋œ Bean์€ Application Context์˜ Bean์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‚˜๋ˆ„๋Š” ๊ธฐ์ค€

  • Application Context
    • ๊ณตํ†ต ๊ธฐ๋Šฅ์„ ํ•  ์ˆ˜ ์žˆ๋Š” Bean ์„ค์ •(Datasource, Service)
    • ๊ฐ Servlet์—์„œ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋Š” Bean
  • Servlet Context
    • Servlet ๊ตฌ์„ฑ์— ํ•„์š”ํ•œ Bean ์„ค์ •(Controller, Interceptor, MappingHandler๋“ฑโ€ฆ)

์ฆ‰ Servlet Context๋Š” Application Context์˜ ์„ฑ์งˆ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ AC๊ฐ€ SC์˜ ํ•˜์œ„ ์ปจํ…์ŠคํŠธ๋กœ ํƒ์ƒ‰ ์‹œ ์—ญํ–‰์€ ๋ถˆ๊ฐ€ํ•˜๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‘˜ ๋‹ค ์ปจํ…์ŠคํŠธ ์ด๋ฏ€๋กœ ์ €์žฅ๋˜๋Š” ๊ฒƒ์€ ๊ฒฐ๊ตญ java Object์ž„์— ํ‹€๋ฆผ์—†๋‹ค.

  • SC๋Š” AC๋ฅผ ๋ถ€๋ชจ๋กœ ๊ฐ€์ง€๊ณ  getServletContext()๊ฐ€ ์ถ”๊ฐ€๋œ Interface์ธ๋ฐ SC์—์„œ ํ˜ธ์ถœ ํ•  ์‹œ ํ•ด๋‹น๋˜๋Š” Servlet์œผ๋กœ ๋ณ€ํ™˜๋œ๋‹ค.
    • ์ด ๋ฉ”์„œ๋“œ๊ฐ€ ์ถ”๊ฐ€๋จ๊ณผ ๋™์‹œ์— ์„œ๋ธ”๋ฆฟ ์ปจํ…์ŠคํŠธ๋ฅผ ์ด์šฉํ•œ ๋ช‡๊ฐ€์ง€ ๋นˆ ์ƒ์•  ์ฃผ๊ธฐ ์Šค์ฝ”ํ”„(์• ํ”Œ๋ฆฌ์ผ€์ด์…˜, ๋ฆฌํ€˜์ŠคํŠธ, ์„ธ์…˜ ๋“ฑ)๊ฐ€ ์ถ”๊ฐ€๋˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.

image ___

๐Ÿ’ฟ web.xml

์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค๋ฅผ ๋“ฑ๋กํ•˜๋Š” ๊ณณ์˜ ์ด๋ฆ„์„ web application deployment descriptor ๋ผ๊ณ  ํ•˜๋Š”๋ฐ ์ด ์—ญํ• ์„ ํ•˜๋Š” ๊ฒƒ์ด web.xml

  • WAS ๊ตฌ๋™ tl /WEB-INF ๋””๋ ‰ํ† ๋ฆฌ์˜ web.xml์„ ์ฝ์–ด ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์„ค์ •์„ ๊ตฌ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ์กด์žฌํ•œ๋‹ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
      <!-- applicationContext.xml์—์„œ ์„ค์ •ํ•œ Bean์€ ๋ชจ๋“  ์„œ๋ธ”๋ฆฟ, ํ•„ํ„ฐ์—์„œ ๊ณต์œ  -->
      <context-param>  
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/applicationContext.xml</param-value>
      </context-param>
      
      <!-- Creates the Spring Container shared by all Servlets and Filters -->
      <!-- ์„œ๋ธ”๋ฆฟ๊ณผ ํ•„ํ„ฐ์— ๊ณต์œ  ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ฆฌ์Šค๋„ˆ๋ฅผ ์„ค์ • -->
      <listener>
          <listener-class>
              org.springframework.web.context.ContextLoaderListener
          </listener-class>
      </listener>
    
      <!-- Processes application requests -->
      <servlet> 
          <servlet-name>dispatcherServlet</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
          <init-param>
              <param-name>contextConfigLocation</param-name> 
              <param-value>
                  <!-- dispatcherServlet ์ƒ์„ฑ ์‹œ, ์„œ๋ธ”๋ฆฟ ์„ค์ •ํŒŒ์ผ ๋กœ๋“œ -->
                  /WEB-INF/spring/appServlet/servlet-context.xml
              </param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
      </servlet>
            
    <!-- dispatcherServlet ๋Œ€ํ•œ url-pattern์„ ์ •์˜ 
        /๋กœ ๋“ค์–ด์˜ค๋Š” ๋ชจ๋“  ์š”์ฒญ์€ DispatcherServlet์—์„œ ์ฒ˜๋ฆฌ -->
      <servlet-mapping>  
            <servlet-name>dispatcherServlet</servlet-name>
            <url-pattern>/</url-pattern>
      </servlet-mapping>
</web-app>

์ •๋ฆฌํ•˜์ž๋ฉดโ€ฆ

  1. ์š”์ฒญ์ด ๋“ค์–ด์˜จ๋‹ค.(Servlet Container ์ฆ‰ tomcat ๊ฐ™์€ WAS์—)
  2. Servlet Container๊ฐ€ ์“ฐ๋ ˆ๋“œ ํ’€์„ ์‚ฌ์šฉํ•˜์—ฌ ๋“ค์–ด์˜จ ์š”์ฒญ์„ ์•Œ๋งž์€ ์“ฐ๋ ˆ๋“œ์— ๋ฐฐ์ •ํ•œ๋‹ค.
    • ์—ฌ๊ธฐ์„œ DispatcherServlet์˜ doService()๊ฐ€ ์ผ์„ ํ•˜๋Š”๋ฐ DispatcherServlet๋„ ํ•˜๋‚˜๋งŒ ์กด์žฌํ•˜๊ณ  doService()๋งŒ ๊ฐ ์“ฐ๋ ˆ๋“œ์—์„œ ์ž‘๋™ํ•œ๋‹ค.
  3. doService๋ฅผ ํ†ตํ•ด ์‹คํ–‰๋œ ์„œ๋ธ”๋ฆฟ์€ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์ผ์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.
    • Application Context๋„ ์“ฐ๋ ˆ๋“œ ๋ณ„๋กœ ์ƒ์„ฑ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ํ•˜๋‚˜๋งŒ ์žˆ๊ณ  ๊ฐ ์“ฐ๋ ˆ๋“œ๊ฐ€ ์ด๋ฅผ ์ฐธ๊ณ ํ•˜๋Š” ๋“ฏ ํ•˜๋‹ค.
    • Application Context๊ฐ€ Bean์˜ Life-Cycle์„ ๊ด€๋ฆฌํ•˜๋Š”๋ฐ Bean์˜ ์Šค์ฝ”ํ”„์— ๋”ฐ๋ผ ๊ด€๋ฆฌํ•œ๋‹ค.

๐Ÿ’ฟ Thread ์™€ ๋นˆ

๊ทธ๋ ‡๋‹ค๋ฉด ํ•˜๋‚˜์˜ ์ปจํ…์ŠคํŠธ์—์„œ ๊ด€๋ฆฌ๋˜๋Š” ๋นˆ๋“ค์€ Thread Safe ํ• ๊นŒ? ๋งŒ์•ฝ ์“ฐ๋ ˆ๋“œ ๋ณ„๋กœ ์ปจํ…์ŠคํŠธ๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค๋ฉด ๊ทธ๋Ÿฌํ•  ๊ฒƒ์ด๋‹ค. ํ•˜์ง€๋งŒ ํ•˜๋‚˜์˜ ์ปจํ…์ŠคํŠธ๋ฅผ ์—ฌ๋Ÿฌ ์“ฐ๋ ˆ๋“œ๊ฐ€ ๊ณต์œ ํ•˜๊ณ  ์žˆ์œผ๋ฉฐ ์ปจํ…์ŠคํŠธ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ Singleton์„ ์‚ฌ์šฉํ•œ๋‹ค.

์ฆ‰ ๊ณต์œ ๋˜๋Š” Singleton Bean์€ ๊ธฐ๋ณธ์ ์œผ๋กœ Thread-Safe ํ•˜์ง€ ์•Š๋‹ค๋Š” ๊ฒฐ๋ก ์ด ๋‚˜์˜จ๋‹ค.

๊ทธ์— ๊ด€ํ•œ Stack Overflow ๋‹ต๋ณ€

1
2
3
4
5
6
7
8
Are Spring Beans Thread Safe?
No.

Spring has different bean scopes (e.g. Prototype, Singleton, etc.) but all these scopes enforce is when the bean is created. For example a "prototype" scoped bean will be created each time this bean is "injected", whereas a "singleton" scoped bean will be created once and shared within the application context. There are other scopes but they just define a time span (e.g. a "scope") of when a new instance will be created.

The above has little, if anything to do with being thread safe, since if several threads have access to a bean (no matter the scope), it would only depend on the design of that bean to be or not to be "thread safe".

The reason I said "little, if anything" is because it might depend on the problem you are trying to solve. For example if you are concerned whether 2 or more HTTP requests may create a problem for the same bean, there is a "request" scope that will create a new instance of a bean for each HTTP request, hence you can "think" of a particular bean as being "safe" in the context of multiple HTTP requests. But it is still not truly thread safe by Spring since if several threads use this bean within the same HTTP request, it goes back to a bean design (your design of a bean backing class).

https://stackoverflow.com/questions/15745140/are-spring-objects-thread-safe

์ •๋ฆฌํ•˜์ž๋ฉดโ€ฆ

์œ„์˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ DispatcherServlet ๊ณผ Application Context๋Š” ํฌํ•จ๊ด€๊ณ„์— ์žˆ๋Š” ๊ฒƒ์ด ๋งž๋‹ค. ๋‹ค๋งŒ ํ•จ์ˆ˜ ์‹คํ–‰์€ Thread Pool์—์„œ ์‹คํ–‰๋œ๋‹ค.

This post is licensed under CC BY 4.0 by the author.

JVM Tunning 2

Kubernetes 2