프로그램/Spring 3.0
@Configuration - @Import
영혼과영혼의양식
2012. 4. 30. 13:08
@Import
-하나의 @Configuration 클래스에서 다수의 @Configuration 클래스를 묶을 수 있음.
-장점은 개발자가 모든 @Configuration 클래스 목록을 기억할 필요 없이 @Import 어노테이션이 적용된 클래스만 기억하면 손쉽게 설정 정보 추적이 가능
spring301.zip

BoardService.java
package service;
public class BoardService implements AbstractService { @Override public String getClassName() { // TODO Auto-generated method stub return "BoardService"; }
} |
NoticeService.java
package service;
import org.springframework.beans.factory.annotation.Autowired;
public class NoticeService implements AbstractService {
//@Autowired private BoardService boardService; //생성자 public NoticeService(BoardService boardService){ this.boardService = boardService; } @Override public String getClassName() { // TODO Auto-generated method stub System.out.println(boardService.getClassName()); return "NoticeService"; }
} |
SpringConfig.java
package config;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource;
import service.BoardService; import service.NoticeService; import vo.MyHome01; import vo.MyHome02;
@Configuration @ImportResource("classpath:/board-config.xml") public class SpringConfig {
@Autowired private BoardService boardService; @Bean(name="noticeService") public NoticeService noticeService(){ return new NoticeService(boardService); } @Bean public MyHome01 myHome01(){ return new MyHome01(); } @Bean(name="myhome") public MyHome02 myHome02(){ return new MyHome02(); } } |
SpringConfigWithImport.java
package config.total;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import;
import config.SpringConfig;
@Configuration @Import({SpringConfig.class}) public class SpringConfigWithImport {
} |
여러개의 클래스를 사용하고 싶을 때는 다음과 같이 콤마(,)를 사용
@Import({AAA.class, BBB.class, CCC.class})
AbstractController.java
package controller;
import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; import org.springframework.web.servlet.ModelAndView;
import service.AbstractService; import service.NoticeService; import vo.MyHome; import vo.MyHome01;
@Component public class AbstractController extends org.springframework.web.servlet.mvc.AbstractController {
@Resource(name="noticeService") private NoticeService noticeService; @Resource(name="myhome") private MyHome myHome; @Override protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { // TODO Auto-generated method stub System.out.println("START >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); System.out.println(noticeService.getClassName()); System.out.println("END >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); myHome.myHomeInfo(); return null; } } |
board-config.xml
spring301-servlet.xml
@Configuration 어노테이션이 적용된 클래스는 @Component 어노테이션이 적용된 클래스와 마찬가지로 컴포넌트 스캔 대상임.
결과
START >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> BoardService NoticeService END >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> MyHome02, Y |
[참고자료] Spring 3.0 프로그래밍 - 최범균