Java Configuration

  • xml 대신 java로 root, servlet 설정을 함(xml 대신 java로 설정이 느는 추세)
  • 아래 예제는 ‘코드로 배우는 스프링 웹 프로젝트’ 책 예제를 정리한 내용

Java Configuration 방법

  • 프로젝트를 생성한다. 임의로 ex.config.test로 지정하였다
  • pom.xml 설정 ~~~
4.0.0 org.zerock controller jex00 war 1.0.0-BUILD-SNAPSHOT 1.6 5.1.5.RELEASE 1.6.10 1.6.6 org.springframework spring-context ${org.springframework-version} commons-logging commons-logging org.springframework spring-webmvc ${org.springframework-version} org.springframework spring-test ${org.springframework-version} org.aspectj aspectjrt ${org.aspectj-version} org.slf4j slf4j-api ${org.slf4j-version} org.slf4j jcl-over-slf4j ${org.slf4j-version} runtime org.slf4j slf4j-log4j12 ${org.slf4j-version} runtime log4j log4j 1.2.17 org.projectlombok lombok 1.18.6 provided javax.inject javax.inject 1 javax.servlet servlet-api 2.5 provided javax.servlet.jsp jsp-api 2.1 provided javax.servlet jstl 1.2 junit junit 4.12 test maven-eclipse-plugin 2.9 org.springframework.ide.eclipse.core.springnature org.springframework.ide.eclipse.core.springbuilder true true org.apache.maven.plugins maven-compiler-plugin 2.5.1 1.8</source> 1.8 -Xlint:all true true org.codehaus.mojo exec-maven-plugin 1.2.1 org.test.int1.Main org.apache.maven.plugins maven-war-plugin 3.2.2 false
- webapp/resources/spring 아래에 있는 xml 설정파일을 삭제한다
- ex.config.config 패키지를 생성한다. 그리고 RootConfig.java 를 만들고 아래와 같이 코드를 작성
- xml 설정 파일 대신  ex.config.sample 에 있는 클래스를 bean으로 등록

package ex.config.config;

import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;

@Configuration @ComponentScan(basePackages= {“ex.config.sample”}) public class RootConfig {

}

- Webconfig.xml

package ex.config.config; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer{ @Override protected Class[] getRootConfigClasses() { 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; } }


- ex.config.sample 패키지를 만들고 bean 등록할 class를 생성한다
- Chef.java

package ex.config.sample;

import org.springframework.stereotype.Component;

import lombok.Data;

@Component @Data public class Chef {

}

- Restaurant.java

package ex.config.sample;

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;

import lombok.Data; import lombok.Setter;

@Component @Data public class Restaurant {

@Setter(onMethod_ = @Autowired)
private Chef chef;

}

- test 폴더에 ex.config.test 패키지 생성. 그리고 junit으로 Test할 JUnitCase 생성

package ex.config.test; import static org.junit.Assert.assertNotNull; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import ex.config.config.RootConfig; import ex.config.sample.Restaurant; import lombok.Setter; import lombok.extern.log4j.Log4j; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {RootConfig.class}) @Log4j public class SampleTests {

   @Setter(onMethod_ = @Autowired)
   private Restaurant restaurant;
   
   
   @Test
   public void testExist() {
          
          assertNotNull(restaurant);
          log.info(restaurant);
          
          log.info("-----------------------------------------");
          
          log.info(restaurant.getChef());
          
   } } ~~~