Notice
Recent Posts
Recent Comments
Link
«   2024/04   »
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
Tags
more
Archives
Today
Total
관리 메뉴

web application developer johnsuhr

<Spring Framework> Java Code로 xml기반 설정 대체하기 - 1 본문

카테고리 없음

<Spring Framework> Java Code로 xml기반 설정 대체하기 - 1

JohnSuhr 2019. 6. 12. 11:33
<properties>

    <java-version>1.8</java-version>

    <org.springframework-version>5.1.5.RELEASE</org.springframework-version>

    <org.aspectj-version>1.9.2</org.aspectj-version>

    <org.slf4j-version>1.7.25</org.slf4j-version>

</properties>

pom.xml을 수정해야 합니다.

 

1. 버전을 최신으로 변경합니다.

 

 

2. 서블릿을 수정해야 합니다.

servlet 디펜던시를 찾아서 아래와 같은 의존성으로 수정합니다.

<dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>javax.servlet-api</artifactId>
	<version>3.1.0</version>
	<scope>provided</scope>
</dependency>

 

3. 기존의 maven-compiler-plugins 플러그인의 버전을 최신 버전으로 수정합니다.

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-compiler-plugin</artifactId>
    	<!-- 플러그인 버전을 최신 버전으로 수정합니다 -->
	<version>3.7.0</version>
	<configuration>
    	<!-- 이 부분을 1.8버전으로 수정합니다. -->
		<source>1.8</source>
		<target>1.8</target>
		<compilerArgument>-Xlint:all</compilerArgument>
		<showWarnings>true</showWarnings>
		<showDeprecation>true</showDeprecation>
	</configuration>
</plugin>

 

4. 플러그인을 추가합니다.

web.xml이 삭제되면 오류를 던지는데, 이를 막아줍니다.

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-war-plugin</artifactId>
	<version>3.2.0</version>
	<configuration>
		<failOnMissingWebXml>false</failOnMissingWebXml>
	</configuration>
</plugin>

 

5. root-context.xml 대체

아래와 같이 기존 root-context.xml 파일이 있습니다.

component-scan 태그로 Controller를 제외한 나머지 @Component, @Resource 등등의 모든 빈을 여기에 등록하는 정의만 있습니다.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.1.xsd">
	
      <context:component-scan base-package="com.java" use-default-filters="true">
          <context: exclude-filter type="annotation" 
          expression="org.springframework.stereotype.Controller"/>
      </context:component-scan>
		
</beans>

root-context.xml을 대체하는 자바 클래스를 작성합니다.

"com.java.config"이라는 패키지 경로에 RootConfig이라는 클래스를 작성합니다.

@Configuration // 해당 클래스는 단순 클래스가 아니라 설정 정보를 담는 클래스입니다.
@ComponentScan( // 컴포넌트를 스캔하여 여기에서 관리합니다.
	basePackages = {"com.java"}, // 스캔 경로를 정의합니다. 해당 경로 아래의 모든 요소를 스캔.
    useDefaultFilters = true, // 기본(Primary)으로 사용
    excludeFilter = {@Filter(Controller.class)}) // 단, @Controller는 제외
public class RootConfig {
    
}

6. servlet-context.xml 대체

기존 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 
	http://www.springframework.org/schema/mvc/spring-mvc.xsd
	http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context.xsd">

	<annotation-driven />

	<resources mapping="/resources/**" location="/resources/" />

	<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.java" use-default-filters="false">
		<context:include-filter type="annotation" expression=""/>
	</context:component-scan>

</beans:beans>

 

<mvc:annotation-driven />

mvc를 활성화시키는 태그입니다.

 

<mvc:resourcesmapping="/resources/**" location="/resources/" />

정적 리소스에 대해 서블릿을 거치지 않게 하는 태그입니다.

 

<beans:bean ~>

서블릿이 반환하는 값에 대해 InternalResourceViewResolver라는 클래스에 맡기는 ViewResolver 클래스 빈입니다.

 

<contet:component-scan ~>

root-context.xml와 같이 스캐닝하는 컴포넌트에 대해 어떤 컴포넌트를 스캔할 것이냐는 것을 정의하는 부분입니다.

 

아래와 같은 WebConfig이라는 클래스로 대체합니다.

@Configuration
@EnableWebMvc // <mvc:annotation-driven />과 동치입니다.
@ComponentScan( // <context:component-scan />과 동치입니다.
		basePackages = {"com.java"},
		useDefaultFilters = false,
		includeFilters = {@Filter(Controller.class))
public class WebConfig implements WebMvcConfigurer {
	
	// <mvc:resources />와 동치입니다.
	@Override
	protected void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry
			.addResourceHandler("/resources/**")
			.addResourceLocations("/resources/");
	}
	
	// <beans:bean />와 동치입니다.
	@Bean
	public ViewResolver viewResolver() {
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		viewResolver.setViewClass(JstlView.class);
		viewResolver.setPrefix("/WEB-INF/views/");
		viewResolver.setSuffix(".jsp");
		return viewResolver;
	}
}

 

이후는 다음 포스팅에서 진행합니다.