Java 설정을 이용하는 경우

이진영·2023년 7월 31일
0

Spring

목록 보기
9/18
최근 Java설정을 이용하는 경우도 증가하고 있기 때문에 같이 학습

Java 설정을 이용하는 경우

XML 대신 사용할 여러 설정 파일을 직접 작성해야 한다.
대개로 어노테이션을 이용한다.

web.xmlservlet-context.xml, root-context.xml을 삭제해야 한다.


1. web.xml

1) web.xml을 삭제하면 pom.xml에서 에러가 발생하는데 이는 웹 프로젝트들이 기본적으로 web.xml을 사용하는 것을 기본으로 설정했기 때문이다.
이를 해결하기 위해서는
pom.xml 하단부에 있는 plugins 내에 아래 코드를 추가해야한다.

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

2) web.xml을 대신하는 클래스는 WebConfig.java이다.
클래스 생성하여 AbstractAnnotationConfigDispatcherServletInitializer의 추상클래스를 상속한다.

3개의 메소드를 오버라이드 하고 return 값을 변경한다.

@Override
	protected Class<?>[] getRootConfigClasses() {
		return new Class[] {RootConfig.class};
	}

	@Override
	protected Class<?>[] getServletConfigClasses() {
		return new Class[] {ServletConfig.class};
	}

	@Override
	protected String[] getServletMappings() {
		return new String[] {"/"};
	}

2. root-context.xml

1-2)에서 3개의 추상메서드를 오버라이드 하면 개 중에 root-context.xml을 대신하는 getRootConfig() 클래스가 있다.


3. servlet-context.xml

기존의 servlet-context.xml에 설정된 모든 내용을 담아야 한다.
ServletConfig 클래스 생성하여 @EnableWebMvc 어노테이션을 이용한다.




『코드로 배우는 스프링 웹 프로젝트 개정판』 - 구멍가게 코딩단

0개의 댓글