@ImportResource - @Configuration 클래스에서 XML 설정 정보를 사용할 때 사용

 

 

 

 

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";
 }

}

◆위 소스에서 @Autowired는 주석 처리해도 상관 없음( //@Autowired )

 

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();
 }
}

●두 개 이상의 XML 설정 파일을 사용하고 싶다면 배열 형태로 설정 파일 목록을 지정 해주면 됨

@Configuration
@ImportResource({"classpath:/board-config.xml", "classpath:/edu-config.xml"})

 

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

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <context:annotation-config/>
 
 <!-- 각종 bean 설정 -->
 <bean id="boardService" class="service.BoardService"></bean>
 
</beans>

 

spring301-servlet.xml 

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <context:annotation-config/>
 
 <context:component-scan base-package="controller"/>
 <context:component-scan base-package="service"/>
 <context:component-scan base-package="config"/>
 
 <!-- HandlerMapping 설정 -->
 <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="mappings">
   <props>
    <prop key="/abstract.sp">abstractController</prop>
   </props>
  </property>
 </bean>

 
 <!-- 각종 bean 설정 -->
 <!-- bean id="springConfig" class="config.SpringConfig"></bean -->
 
</beans>

@Configuration 어노테이션이 적용된 클래스는 @Component 어노테이션의 적용된 클래스와 마찬가지로 컴포넌트 스캔 대상임 => <context:component-scan> 태그를 사용함으로써 자동으로 빈 등록

 

 

결과 

START >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
BoardService
NoticeService
END >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
MyHome02, Y 

 

 

 

[참고자료] Spring 3.0 프로그래밍 - 최범균

+ Recent posts