java

์Šคํ”„๋ง ์‹œ์ž‘ํ•˜๊ธฐ

dev_summer 2021. 2. 27. 13:51

 

 

 

์ˆ˜์—…์ด ๋๋‚˜๊ฐ€์„œ ์•„์‰ฝ๋‹ค ..๋€จ์šฐ

 

 

Spring์€ ์›นํ”„๋กœ์ ํŠธ์—์„œ ์ฝ”๋””์— ์ข€ ๋” ์ง‘์ค‘ํ• ์ˆ˜์žˆ๋„๋ก ์›นํ”„๋กœ๊ทธ๋žจ์˜ ๋ผˆ๋Œ€๋ฅผ ์„ธ์›Œ๋†“์€ ํ”„๋ ˆ์ž„์›Œํฌ์ด๋‹ค. ์ด๋Ÿฐ ํ”„๋ ˆ์ž„์›Œํฌ ๋˜ํ•œ ๋งŽ์€ ๋ฐœ์ „์„ ํ–ˆ๋‹ค ๊ธฐ์กด์— xml์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐœ๋ฐœํ™˜๊ฒฝ์„ ์ค€๋น„ํ•˜๋˜ ๋ ˆ๊ฑฐ์‹œ๋ฒ„์ „์ด ์žˆ๊ณ  ํ˜„์žฌ๋Š” configuration๊ณผ ์–ด๋…ธํ…Œ์ด์…˜ ๊ธฐ๋ฐ˜์˜ ์„ค์ •์„ ์ด์šฉํ•œ๋‹ค.

๊ธฐ์กด์— jsp์—์„œ๋Š” ํ†ฐ์บฃ์ด ์ง„์ž…ํ• ๋•Œ server.xml(ํฌํŠธ์™€ ํŒจ์Šค๋“ฑ ์„ค์ •), web.xml(ํ•„ํ„ฐ๋ง), context.xml(DB์—ฐ๊ฒฐ)์„ ์ง€๋‚˜ ๋ฆฌํ€˜์ŠคํŠธ ๋””์ŠคํŒจ์ณ๋กœ ๊ฐ„๋‹ค.

ํ˜„์žฌ ์Šคํ”„๋ง์€ ์ด๋Ÿฐ xml์„ค์ •์„ ํ•œ๊ฐœ์˜ ํŒŒ์ผ๋กœ ๊ด€๋ฆฌ๋ฅผ ํ•˜๊ณ  ๋‚ด์žฅํ†ฐ์บฃ์„ ์ด์šฉํ•˜์—ฌ ํ†ฐ์บฃ์„ ์„ค์น˜ ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ๋˜ํ•œ ๋ฐ˜๋ณต์ ์œผ๋กœ ์‚ฌ์šฉํ–ˆ๋˜ SQL์„ ํŽธํ•˜๊ฒŒ ์ฟผ๋ฆฌ๋งŒ ์ ์„์ˆ˜์žˆ๊ณ  ๋ฐ”๋กœ ์˜ค๋ธŒ์ ํŠธํ˜•์‹์œผ๋กœ ๋ฐ›์„์ˆ˜์žˆ๊ป˜ ํ•˜๋Š” ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค ๋“ฑ์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ™œ์šฉํ•œ๋‹ค.

๊ทธ ์ „์— ์Šคํ”„๋ง์˜ ๊ธฐ๋ณธ์ ์ธ ๊ฐœ๋…์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•œ ๋ ˆ๊ฑฐ์‹œ ๋ฒ„์ „์„ ์‚ดํŽด ๋ณผ ์˜ˆ์ •์ด๋‹ค. ๋ ˆ๊ฑฐ์‹œ๋Š” ๋ง ๊ทธ๋Œ€๋กœ ์˜ค๋ž˜๋œ ์Šคํ”„๋ง ๋ฒ„์ „์ด๋‹ค. ์ด๋•Œ์—๋Š” xml๋กœ ๋ชจ๋“  ์„ค์ •์„ ํ•œ๋‹ค.

 

 

1. Sping ๋“ค์–ด๊ฐ€๊ธฐ์ „ ์„ ํ–‰ํ•™์Šต ์‚ฌํ•ญ

- java์–ธ์–ด์˜ ๊ธฐ๋ณธ ์ง€์‹

- ์„œ๋ธ”๋ฆฟ,JSP ๊ด€ํ•œ ๊ธฐ๋ณธ ์ง€์‹ ๋ชจ๋ธ1,๋ชจ๋ธ2๊ธฐ๋ณธ์ ์ธ๊ฐœ๋…

- html, javascript, css ์™ธ ์ œ์ด์ฟผ๋ฆฌ ๊ธฐ๋ณธ ์ง€์‹

- sql ๊ธฐ๋ณธ ์ง€์‹

 

 

 

2. Spring ์„ค์น˜ํ•˜๊ธฐ

- ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์„ค์ • (๋‚ดpc ๊ณ ๊ธ‰์„ค์ •์— ๋“ค์–ด๊ฐ€์„œ jdk์…‹ํŒ…)

- ํ†ฐ์บฃ8.5 ์‚ญ์ œ 9 ์„ค์น˜

 

https://spring.io/tools3/sts/all

 

Spring Tools 4 is the next generation of Spring tooling

Largely rebuilt from scratch, Spring Tools 4 provides world-class support for developing Spring-based enterprise applications, whether you prefer Eclipse, Visual Studio Code, or Theia IDE.

spring.io

 

 

 

 

 

3. Spring legacy ๊ตฌ์กฐ ์‚ดํŽด๋ณด๊ธฐ

 

 

์ ๋‹ค๊ฐ€ ๋งŒ๊ฒƒ์€ ๋†“์นœ๊ฒƒ..

 

 

1) Maven Dependencies

 

maven dependencies(๋ฒ„์ „๊ด€๋ฆฌํ”„๋กœ๊ทธ๋žจ)์€ ํ”„๋กœ์ ํŠธ ๋‚ด ์กด์žฌํ•˜์ง€์•Š๊ณ  ๋ฏธ๋Ÿฌ์‚ฌ์ดํŠธ์— ์กด์žฌํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์ด๋‹ค. C:\Users\it\.m2 ์—์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์˜ ์‹ค์ œ ๋ชฉ๋ก์ด ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค.

(๋ฆฌ๋ˆ…์Šค์˜ ๋ ˆํŒŒ์ง€ํ† ๋ฆฌ์™€ ๊ฐ™์€ ์—ญํ• , ์šฐ๋ถ„ํˆฌ ๋ฏธ๋Ÿฌ์‚ฌ์ดํŠธ์— ๋งŽ์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋ชจ์—ฌ์žˆ๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค.)

๋ฉ”์ด๋ธ ๋””ํŽœ๋˜์Šค๋„ ๋™์ผํ•˜๊ฒŒ ๋ฏธ๋Ÿฌ์‚ฌ์ดํŠธ๊ฐ€ ์กด์žฌํ•˜๊ณ  ๊ทธ ๋ชฉ๋ก์„ ๋„์–ด์ค€๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋งŽ์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ค‘ ํ”„๋กœ์ ํŠธ์—์„œ ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ด๋ฆ„์„ pom.xml ์— ์ ์–ด๋†“๊ณ  ๋ฐฐํฌํ• ๋•Œ ์ด๋ฆ„์œผ๋กœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ฐพ์•„ ์—ฐ๊ฒฐํ•˜์—ฌ ์‹คํ–‰ํ•ด์ค€๋‹ค.

(๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‹ค์šด์€ m2์— ๋ฐ›๊ณ  ๋นŒ๋”์‹œํ‚จ๋‹ค. )

 

 

๋งŒ์•ฝ m2๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด ์‚ญ์ œ๋˜๋ฉด ํ•˜๋‹จ์˜ problems์— ์•Œ๋ฆผ์ฐฝ์ด ๋œจ๊ฒŒ๋˜๋Š”๋ฐ ๋ ˆํŒŒ์ง€ํ† ๋ฆฌ๋Š” ์—…๋ฐ์ดํŠธ๋˜์ง€๋งŒ ๋นŒ๋“œ๊ฐ€ ๋˜์ง€์•Š๋Š”๋‹ค. ๋นŒ๋“œ๋ฅผ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ์˜์กด์„ฑ๋•Œ๋ฌธ์— ์‚ญ์ œ๋œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์–ด๋„ ์•ˆ๋ ๋•Œ๊ฐ€ ์žˆ๋Š”๋ฐ ์ด๋• ๋ ˆํŒŒ์ง€ํ† ๋ฆฌํŒŒ์ผ์„ ํ†ต์งธ๋กœ ๋‚ ๋ ค๋ฒ„๋ฆฌ๊ณ  ํ‚ค๋ฉด ๋‹ค์‹œ ์…‹ํŒ…์ด ๋œ๋‹ค.

 

๋ฉ”์ด๋ธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋”ฐ๋กœ ์„ค์น˜ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค.

 

ex) gson๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์ž

์Šคํ”„๋ง์—์„œ๋Š” ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋ฉ”์ด๋ธ์œผ๋กœ ์ด๋ฆ„๋งŒ ์“ฐ๋ฉด ๋œ๋‹ค.

์‚ฌ์ง„ ์‚ญ์ œ

์‚ฌ์ง„ ์„ค๋ช…์„ ์ž…๋ ฅํ•˜์„ธ์š”.

<dependencise><dependencise> ๊ฐ€ ํ•˜๋‚˜์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

pom.xml์— ์ ์–ด์ค€ ํŒŒ์ผ๊ณผ ์‹ค์ œ ๊ฒฝ๋กœ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.โ€‹

๊ธฐ์กด์— ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋‹ค์šด๋ฐ›์•„ libํด๋”์— ์ง์ ‘ ๋ณต์‚ฌ๋ถ™์—ฌ๋„ฃ๊ธฐ๋ฅผ ํ•ด์ฃผ๋˜ ๋ฐฉ๋ฒ•์ด ์•„๋‹Œ ๋ณต์‚ฌ ๋ถ™์—ฌ๋„ฃ๊ธฐ๋งŒํ•˜๋ฉด ๋ฉ”์ด๋ธ์ด ์•Œ์•„์„œ ํ•ด์ค€๋‹ค. ๋ ์šฉ ๋˜‘๋˜‘์ด

 

 

2) xml์„ค์ •ํŒŒ์ผ ์‚ดํŽด๋ณด๊ธฐ

์ฒ˜์Œ ์„œ๋ฒ„๊ฐ€ ์‹คํ–‰์ด ๋˜๋ฉด ํ†ฐ์บฃ์ด xmlํŒŒ์ผ๋“ค์„ ์‹คํ–‰ํ•œ๋‹ค.

๊ฐ€์žฅ๋จผ์ € ์‹คํ–‰๋˜๋Š” ํŒŒ์ผ์€ src ๋‚ด์˜ web.xmlํŒŒ์ผ์ด๋‹ค.

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
	<context-param> // 1. ์ตœ์ดˆ ์ง„์ž…๋กœ๋ฅผ ์„ค์ •ํ• ์ˆ˜ ์žˆ๋‹ค.
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/root-context.xml</param-value>
	</context-param>
	
	<!-- Creates the Spring Container shared by all Servlets and Filters -->
	<listener> // 2. root์™€ servlet-context๋ฅผ ์ฝ์–ด์ฃผ๋Š” ๋กœ๋“œ๋ฆฌ์Šค๋„ˆ ์ƒ์„ฑ
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- Processes application requests -->
	<servlet> // 3. ์„œ๋ธ”๋ฆฟ ๋„ค์ž„์œผ๋กœ ๋“ค์–ด์˜จ ๊ฒƒ๋“ค์„ ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค๊ฒฝ๋กœ๋กœ ์—ฐ๊ฒฐํ•ด์ค€๋‹ค.
		<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-context.xml ์„ ๋จผ์ € ์ฝ๋Š”๋‹ค.
	</servlet>
		
	<servlet-mapping> // 4. / ๋กœ ๋“ค์–ด์˜จ ๊ฒƒ์„ appServlet์— ๋งตํ•‘ํ•ด์ค€๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. 
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

 

 

/WEB-INF/spring/appServlet/servlet-context.xml ์„ ์ฝ๋Š”๋‹ค

 

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
	
	<!-- Enables the Spring MVC @Controller programming model -->
	<!-- 1. ์•ž์œผ๋กœ๋Š” @์›น์„œ๋ธ”๋ฆฟ์ด ์•„๋‹Œ ์›น ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ๋จผ์ € ์ฐพ๊ฒ ๋‹ค!  -->
	<annotation-driven />

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
	<!-- 2. img src="/resources/aa.jpg">๋กœ ์ฐพ์•„๋„ /resources/ ๋กœ ์ฐพ์•„์ฃผ๊ฒ ๋‹ค.  -->
	<resources mapping="/resources/**" location="/resources/" />

	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<!-- 3. ๊ฐ์ฒด๋กœ ๋ฉ”๋ชจ๋ฆฌ์— ๋„์–ด๋ฒ„๋ฆฐ๋‹ค.  -->
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>
	<!-- 4. ์•ž์œผ๋กœ prefix๋Š” /WEB-INF/views/ ์œผ๋กœ ์ฐพ๊ณ   suffix ์€ .jsp๋กœ ์ฐพ๊ฒ ๋‹ค -->
	<!-- 5. ํŒŒ์ผ์„ ๋ฆฌํ„ดํ• ๋•Œ ํ•ญ์ƒ ๊ฒฝ๋กœ ๋ฐ .jsp๋กœ ์ ์–ด์ฃผ๊ฒ ์œผ๋‹ˆ ํŒŒ์ผ๋ช…๋งŒ ์ ์œผ๋ฉด ๋œ๋‹ค. -->
	
	
	<context:component-scan base-package="com.cos.springlegacy" />
	<!-- ๋””์ŠคํŒจ์ฒ˜๊ฐ€ ๊ฐ์‹œํ•  ํŒจํ‚ค์ง€  -->
	<!-- ์–ด๋…ธํ…Œ์ด์…˜๋“œ๋ฆฌ๋ธ๊ณผ  -->
	
	
	<!--  ํ”„๋ ˆ์ž„์›Œํฌ๋Š” ๊ณ ์น˜๋Š”๊ฑฐ ์•„๋ƒ ๊ทธ๋ƒฅ ์จ -->
</beans:beans>

 

์ž์„ธํžˆ ๋ด๋„ ๋Œ€์ถฉ๋ด๋„ ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ.. ๋Œ€๋žต์ ์œผ๋กœ ์ •๋ฆฌ๋ฅผ ํ•ด๋ณด์•„๋„ ์Šคํ”„๋ง์˜ ํŠน์ง•์ด ๋‚˜ํƒ€๋‚œ๋‹ค.

 

์ผ๋‹จ ์‹คํ–‰์ด ๋˜๋ฉด ์Šคํ”„๋ง ์ปจํ…์ŠคํŠธ, ์Šคํ”„๋ง์˜ ๋ชจ๋“  ์ •๋ณด๊ฐ€ ์žˆ๋Š” ๋ณด๋”ฐ๋ฆฌ๊ฐ€ ์ƒ๊ธด๋‹ค. ๊ทธ ์•ˆ์—๋Š” ์—ฌ๋Ÿฌ ๋ฐ•์Šค๊ฐ€ ์žˆ๊ณ  ์—ฌ๋Ÿฌ ์ •๋ณด๊ฐ€ ์žˆ๋‹ค. ์Šคํ”„๋ง๋ณด๋”ฐ๋ฆฌ ์•ˆ์—์žˆ๋Š” ๋ฐ•์Šค๋Š” component, controller , service, bean, dao, restcontroller ๋“ฑ์ด ์žˆ๋‹ค. ์ด๋•Œ component ๋Š” ์Šคํ”„๋ง์˜ ์ •๋ง ์ค‘์š”ํ•œ ํŠน์ง•์ธ ioc, ์ œ์–ด๊ถŒ์˜ ์—ญ์ „์ด๋ผ๋Š” ํŠน์ง•์„ ๊ฐ€์ง„๋‹ค. ์ด๋Š” ๊ธฐ์กด์— A๊ฐ์ฒด์—์„œ B๊ฐ์ฒด๊ฐ€ ํ•„์š”ํ•˜๋ฉด A๊ฐ์ฒด์—์„œ๋Š” new B()๋ฅผ ํ•ด์ค€๋‹ค. ๋˜ C๊ฐ์ฒด์—์„œ B๊ฐ์ฒด๊ฐ€ ํ•„์š”ํ•˜๋ฉด C๊ฐ์ฒด๋Š” new B()๋ฅผ ํ•˜๊ฒŒ๋œ๋‹ค. ์ด์ฒ˜๋Ÿผ ๊ฐ๊ฐ์˜ ๊ฐ์ฒด๊ฐ€ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ํ•„์š”๋กœ ํ• ๋•Œ ์ง์ ‘new๋ฅผํ•˜์—ฌ ๋ถ€๋ฅด์ง€๋งŒ ์Šคํ”„๋ง์—์„œ IOC์˜ ํŠน์ง•์ด๋ž€, A๊ฐ์ฒด๊ฐ€ B๋ผ๋Š” ๊ฐ์ฒด๊ฐ€ ํ•„์š”ํ• ๋•Œ new B()ํ•˜์ง€์•Š๊ณ  ์Šคํ”„๋ง์—์„œ ๋งŒ๋“ค์–ด์ค€ B๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

*์›”์š”์ผ์— ์งˆ๋ฌธํ•ด์•ผ๊ฒŒ๋•… DB์—์„œ getinstanseํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ๋งฅ๋ฝ์ธ๊ฑด๊ฐ€? ์‹ฑ๊ธ€ํ†คํŒจํ„ด??

์Šคํ”„๋ง์ด ๋งŒ๋“ค์–ด์ค€, ์™ธ๋ถ€์—์„œ ์ƒ์„ฑํ•ด์„œ A๊ฐ์ฒด์—์„œ ์ฃผ์ž…, ์ฃผ์ž…ํ•˜๋Š” ๊ฒƒ์ด ๋‘๋ฒˆ์งธ ํŠน์ง•์ธ DI

 

 

 

 

 

com.cos.__๋ฐ‘์˜ ๊ณต๊ฐ„์„ ์ „~๋ถ€ ์Šค์บ”ํ•œ๋‹ค.--> @component๋ฅผ ์ฐพ์•„ ์Šคํ”„๋ง์ด ๊ณต๊ฐ„ ์•ˆ์— new๋ฅผ ํ•ด์ค€๋‹ค.

์ œ์–ด์˜ ์—ญ์ „, ๋‚ด๊ฐ€new๋ฅผ ํ•˜๋Š”๊ฒŒ ์•„๋‹Œ ์Šคํ”„๋ง์ด ๊ฐ์ฒด๋ฅผ ์ œ์–ดํ•œ๋‹ค.

์˜์กด์„ฑ ์ฃผ์ž… (DI)

 

 

 

 

๊ทธ ์—ญํ• ์„ web.xml์˜ <context:component-scan base-package="com.cos.springlegacy"/> ๊ฐ€ ํ•ด์ค€๋‹ค.

root context์ฝ์Œ

 

 

 

 

์–ด๋ ต๋‹ค์•„ใ…~~~