πŸ’» BackEnd/🟒 Spring | SpringBoot

🟒 [Spring] μŠ€ν”„λ§ μ„€μ • 파일 (XML)

Dbswnstjd 2023. 6. 28. 17:57

πŸ“Œ μŠ€ν”„λ§ ν”„λ‘œμ νŠΈ μ„€μ • 파일 관리

μŠ€ν”„λ§ ν”„λ‘œμ νŠΈλ₯Ό μ‹œμž‘ν•˜κΈ°μ— μ•žμ„œ μŠ€ν”„λ§μ€ μ„€μ • νŒŒμΌμ„ μ„€μ •ν•˜λŠ” 뢀뢄이 μ–΄λ €μš΄ νŽΈμ΄λ‹€. Node.js 둜만 ν”„λ‘œμ νŠΈλ₯Ό ν•΄λ΄€λ˜ λ‚˜λ‘œμ¨λŠ” κ½€ νž˜λ“  μž‘μ—…λ“€μ΄μ—ˆλ‹€. XML νŒŒμΌλ“€λ„ μ²˜μŒμ΄μ—ˆκ³  JSP μ‚¬μš©λ„ μ²˜μŒμ΄μ–΄μ„œ κ½€λ‚˜ λ§Žμ€ μ‹œκ°„μ„ μ†ŒλΉ„ν•˜μ˜€λ‹€.

μ§€κΈˆκΉŒμ§€ 배운 μ„€μ • νŒŒμΌμ„ web.xml / servlet-context.xml / pom.xml / application-config.xml νŒŒμΌμ΄λ‹€. μ—¬κΈ°μ„œ  λ‚˜λŠ” λ”°λ‘œ config νŒŒμΌμ„ λ§Œλ“€μ–΄ 경둜λ₯Ό λ³€κ²½ν•΄μ£Όμ—ˆκΈ° λ•Œλ¬Έμ— application-config.xml 은 root-context.xml κ³Ό κ°™λ‹€κ³  λ³΄λ©΄λœλ‹€.

이 λ‚΄μš©μ€ μ•„λž˜μ—μ„œ μ„€λͺ…ν•œλ‹€.

 

1. Web.xml

<?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:application-config.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>

μœ„μ˜ xml νŒŒμΌμ€ 섀정을 쑰금 λ°”κΎΌ xml이닀. context-param 을 μ œμ™Έν•˜κ³ λŠ” κΈ°λ³Έ 생성이 λœλ‹€. 

context-param을 λ°”κΎΌ μ΄μœ κ°€ λ°”λ‘œ application-config.xml νŒŒμΌμ„ μƒˆλ‘œ λ§Œλ“€μ—ˆκΈ° λ•Œλ¬Έμ΄λ‹€.

root-context.xml 은 ν”„λ‘œμ νŠΈλ₯Ό μƒμ„±ν•˜λ©΄ μžλ™μœΌλ‘œ μƒμ„±λœλ‹€. 여기에 빈 섀정을 해주어도 λ˜μ§€λ§Œ λ‚˜λŠ” λ”°λ‘œ λ§Œλ“€κ³  관리λ₯Ό ν•΄μ„œ param-value의 경둜λ₯Ό classpath:application-config.xml 둜 λ°”κΎΈμ–΄ μ£Όμ—ˆλ‹€.

그리고 Servlet을 μ‚¬μš©ν•˜μ˜€κΈ° λ•Œλ¬Έμ— servlet-context.xml도 μ„€μ •ν•΄μ€˜μ•Ό ν•œλ‹€. 

 

- classpath : eclipse의 src/main/resources 

 

 

2. 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 -->
	<annotation-driven />

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/resources/**" location="/resources/" />
	<resources location="/images/" mapping="/images/**"/>
	
	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<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>
	
	<context:component-scan base-package="com.example.myapp.controller" />
	
</beans:beans>

- <annotation-driven/> : transactional μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•˜κΈ° μœ„ν•œ νƒœκ·Έ

- <resources> : JSPμ—μ„œ Imgνƒœκ·Έ, JS, CSS νŒŒμΌμ„ 읽어 였기 μœ„ν•œ νƒœκ·Έ

<beans: ~> : JSPλ₯Ό μ‚¬μš©ν•˜κΈ° μœ„ν•œ νƒœκ·Έ

<context-component-scan> : packageμ•ˆμ—μ„œ @Controller, @Service, @Repository λ“± μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•˜κΈ° μœ„ν•œ νƒœκ·Έ