Spring 환경설정 JAVA 기반
처음 Spring으로 프로젝트를 진행할때.
XML기반으로 하곤했는데, 스프링3버전 이후로부터 Java기반으로 설정이 가능하다.
자바 기반으로 환경설정할때는 뭐가다를까?
1. web.xml의 파일 삭제 및 스프링 관련 파일 삭제
web.xml , servlet-context.xml, root-context.xml 삭제
2. pom.xml의 수정 및 스프링 버전 변경
pom.xml에서
맨아래 다음을추가
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
▲ 맨아래 위의 내용을 추가
...
<properties>
<java-version>1.8</java-version>
<org.springframework-version>5.0.7.RELEASE</org.springframework-version>
...
▲ 찾아서 수정
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>-Xlint:a
▲ 찾아서 수정
3. Java 설정 관련 패키지 생성
다음 처럼 org.zerock.config 패키지를 생성한다
이제 지워야할건 다 지우고 수정한건데,
xml파일을 대신할 자바코드를 작성해보자
1. root-context.xml을 대신하는 RootConfig 클래스
다음 처럼 생성해주고
1
2
3
4
5
6
7
8
9
|
package org.zerock.config;
import org.springframework.context.annotation.Configuration;
//XML대신 자바로 설정할때 이클래스파일이 설정파일이라고 알려주는 어노테이션 , root-context.xml임을 나타냄
@Configuration
public class RootConfig {
}
|
cs |
ㅁㅁㅁㅁㄴㅇ
다음 처럼 @Configuration을 입력하자
2.web.xml을 대신하는 WebConfig 클래스
이 클래스는 상속을 받아야하는데 정말 긴이름이다 Abstract까지입력하고
[컨트롤 + 스페이스바] 를 눌러서 제일 긴이름을 고르면 된다
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
|
package org.zerock.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
//web.xml을 대신하는 클래스
public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
// TODO Auto-generated method stub
// root-context.xml을 여기에 등록한다 RootConfig 클래스가 해당된다
return new Class[] {RootConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
// TODO Auto-generated method stub
return null;
}
@Override
protected String[] getServletMappings() {
// TODO Auto-generated method stub
return null;
}
}
|
cs |
반드시 추상메서드 3개를 받아야한다(상속받았으니까)
그리고 9번에 해당하는 메소드를 보자 딱봐도 root관련된 설정파일 클래스를 등록하라는것같다
여기에 root-context.xml을 대신하는 자바 클래스인 RootConfig 를 등록하자.
이렇게하면 일단 기본뼈대는 완성된다
(코드를 복붙못해서 다른방법을 찾아보니 이렇게 복붙이되다니 앞으로 사진복붙이아닌 드래그할수있는 코드를 복붙하겠다.)