๋กœ์ผ“๐Ÿพ
article thumbnail
๋ฐ˜์‘ํ˜•

 

์ด ๊ธ€์€ ์ œ๊ฐ€ ์ดํ•ดํ•œ ๊ฐœ๋…์„ ๋ฐ”ํƒ•์œผ๋กœ ์ž‘์„ฑํ•˜์˜€๊ธฐ์— ํ‹€๋ฆฐ ๋ถ€๋ถ„์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ‹€๋ฆฐ ๋ถ€๋ถ„ ์žˆ๋‹ค๋ฉด ์ง€์ ํ•ด์ฃผ์„ธ์š” :)


์Šคํ”„๋ง์—์„œ ๋งํ•˜๋Š” ์ปจํ…์ŠคํŠธ(context)๋Š” ์Šคํ”„๋ง์ด ๊ด€๋ฆฌํ•˜๋Š” ๋นˆ๋“ค์ด ๋‹ด๊ฒจ ์žˆ๋Š” ์ปจํ…Œ์ด๋„ˆ ๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.


์Šคํ”„๋ง์„ ๊ณ„์† ๊ณต๋ถ€์ค‘์ด์ง€๋งŒ ์—„์ฒญ ํ—ท๊ฐˆ๋ ธ๋˜ ๋ถ€๋ถ„์ด WebApplicationContext์™€ ApplicationContext์˜ ๊ด€๊ณ„, web.xml์—์„œ์˜ servlet-context์™€ root-context์˜ ๊ด€๊ณ„ ๊ทธ๋ฆฌ๊ณ  ContextLoaderListenter์™€ dispatcher-servlet์˜ ๊ด€๊ณ„ ์ด ์ด 3๊ฐœ์˜ ๊ด€๊ณ„๊ฐ€ ๋ณผ๋•Œ๋งˆ๋‹ค ํ—ท๊ฐˆ๋ ธ์Šต๋‹ˆ๋‹ค. 

 

๋”ฐ๋ผ์„œ ์ด ๊ด€๊ณ„๋“ค์„ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ทธ๋ฆผํ™”ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

์Šคํ”„๋ง ๋‚ด์—์„œ์˜ context ๊ตฌ์กฐ

 


 

# web.xml์—์„œ์˜ Context

 

<?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 https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:spring/context-*.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>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
		
	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>

 

 

ApplicationContext๋Š” ContextLoaderListener ํด๋ž˜์Šค์— ์˜ํ•ด ๋งŒ๋“ค์–ด์ง€๊ณ ,

WebApplicationContext๋Š” DispatcherSerlvet ํด๋ž˜์Šค์— ์˜ํ•ด ๋งŒ๋“ค์–ด์ง‘๋‹ˆ๋‹ค.

 

 

ใ…ฃ ApplicationContext

  • ์ตœ์ƒ์œ„ ์ปจํ…์ŠคํŠธ ์ž…๋‹ˆ๋‹ค.
  • root-context์— ๋“ฑ๋ก๋˜๋Š” ๋นˆ๋“ค์€ ๋ชจ๋“  ์ปจํ…์ŠคํŠธ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (๊ณต์œ  ๊ฐ€๋Šฅ)
  • service๋‚˜ dao๋ฅผ ํฌํ•จํ•œ, ์›น ํ™˜๊ฒฝ์— ๋…๋ฆฝ์ ์ธ ๋นˆ๋“ค์„ ๋‹ด์•„๋‘ก๋‹ˆ๋‹ค.
  • ์„œ๋กœ ๋‹ค๋ฅธ servlet-context์—์„œ ๊ณต์œ ํ•ด์•ผ ํ•˜๋Š” ๋นˆ๋“ค์„ ๋“ฑ๋กํ•ด๋†“๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • servlet-context ๋‚ด ๋นˆ๋“ค์€ ์ด์šฉ์ด ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

 

ใ…ฃ WebApplicationContext

  • servlet-context์— ๋“ฑ๋ก๋˜๋Š” ๋นˆ๋“ค์€ ํ•ด๋‹น ์ปจํ…Œ์ŠคํŠธ์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • DispatcherServlet์ด ์ง์ ‘ ์‚ฌ์šฉํ•˜๋Š” ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ํฌํ•จํ•œ ์›น ๊ด€๋ จ ๋นˆ์„ ๋“ฑ๋กํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • ๋…์ž์ ์ธ ์ปจํ…์ŠคํŠธ๋“ค์„ ๊ฐ€์ง€๋ฉฐ, root-context ๋‚ด ๋นˆ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

 


# ์Šคํ”„๋ง์—์„œ WebApplicataionContext ๊ฐ์ฒด ์–ป์–ด๋‚ด๊ธฐ

 

public class A  {
    @Autowired
    WebApplicationContext applicationContext;
   
    //Container ๋‚ด์— ๊ฐ์ฒด์–ป๊ธฐ
    public method() {
        B b = context.getBean(B.class);    
    }
}
 
 

public class A implements ApplicationContextAware {
  private ApplicationContext context;
 
  public void setApplicationContext(ApplicationContext context) {
      this.context = context;
  }
}

 

 


# WebApplicationContext vs ApplicationContext

 

์Šคํ”„๋ง ์•ˆ์—๋Š” ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ApplicationContext ๊ตฌํ˜„์ฒด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. WebApplicationContext๋Š” ApplicationContext๋ฅผ ํ™•์žฅํ•œ WebApplicationContext ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„์ฒด๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. WebApplicationContext๋Š” ApplicationContext์— getServletContext() ๋ฉ”์„œ๋“œ๊ฐ€ ์ถ”๊ฐ€๋œ ์ธํ„ฐํŽ˜์ด์Šค์ž…๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ServletContext๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ WebApplicationContext๋Š” ApplicationContext์˜ ์ž์‹์ด๋ฉด์„œ ServletContext์™€ ์—ฐ๊ด€ ๊ด€๊ณ„์— ์žˆ๋‹ค๋Š” ์ •๋„๋กœ ์ •๋ฆฌ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๊ฐ€ ์ถ”๊ฐ€๋˜๋ฉด ServletContext๋ฅผ ์ด์šฉํ•œ ๋ช‡๊ฐ€์ง€ ๋นˆ ์ƒ์•  ์ฃผ๊ธฐ ์Šค์ฝ”ํ”„๊ฐ€ ์ถ”๊ฐ€ ๋˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.

 

 

์ฝ์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

์งˆ๋ฌธ์€ ์–ธ์ œ๋‚˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค.

 

"๋‚œ ๋ฐ˜๋“œ์‹œ ๋ฐฑ์—”๋“œ ์™•์ด ๋ ๊ฑฐ์•ผ"

๋ฐ˜์‘ํ˜•
profile on loading

Loading...