@Resource : 의존하는 빈 객체를 전달할 때 사용
AbstractServiceImpl.java
package service;
public class AbstractServiceImpl implements AbstractService { private String name = "111"; @Override public String getClassName() { // TODO Auto-generated method stub return "AbstractServiceImpl-" + name; }
} |
AbstractServiceImpl02.java
package service;
public class AbstractServiceImpl02 implements AbstractService { private String name = "222"; @Override public String getClassName() { // TODO Auto-generated method stub return "AbstractServiceImpl-" + name; }
} |
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.web.servlet.ModelAndView;
import service.AbstractService;
public class AbstractController extends org.springframework.web.servlet.mvc.AbstractController {
@Resource(name="abstractService02") private AbstractService service; @Override protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { // TODO Auto-generated method stub System.out.println("START >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); System.out.println(service.getClassName()); System.out.println("END >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); return null; } } |
spring301-servlet.xml
결과
START >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> AbstractServiceImpl-222 END >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
※위의 소스는 아래와 동일하게 작동함.
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.web.servlet.ModelAndView;
import service.AbstractService;
public class AbstractController extends org.springframework.web.servlet.mvc.AbstractController {
@Autowired @Qualifier("abstractService02") private AbstractService service; @Override protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { // TODO Auto-generated method stub System.out.println("START >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); System.out.println(service.getClassName()); System.out.println("END >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); return null; } } |
<?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/> <!-- HandlerMapping 설정 --> <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/abstract.sp">abstractController</prop> </props> </property> </bean>
<!-- ViewResolver 설정 : ResourceBundleViewResolver --> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> <property name="contentType"> <value>text/html; charset=UTF-8</value> </property> </bean>
<!-- 각종 bean 설정 --> <bean id="abstractController" class="controller.AbstractController"></bean> <bean id="abstractService01" class="service.AbstractServiceImpl"> <qualifier value="abstractService01"></qualifier> </bean> <bean id="abstractService02" class="service.AbstractServiceImpl02"> <qualifier value="abstractService02"></qualifier> </bean> </beans> |