@Autowired
-의존관계 자동 설정(type 기반)
-생성자, 필드, 메서드에 적용 가능
-setXXXX()의 형식이 아니어도 적용가능
AbstractController.java
package controller;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.servlet.ModelAndView;
import service.AbstractService;
public class AbstractController extends org.springframework.web.servlet.mvc.AbstractController {
@Autowired private AbstractService service;
//@Autowired //public void prepare(AbstractService service){ // this.service = 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; }
} |
AbstractService.java
package service;
public interface AbstractService { String getClassName(); } |
AbstractServiceImpl.java
package service;
public class AbstractServiceImpl implements AbstractService {
@Override public String getClassName() { // TODO Auto-generated method stub return "AbstractServiceImpl"; }
} |
spring301-servlet.xml
▶결과
START >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> AbstractServiceImpl END >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
아래와 같이 메서드에 적용도 가능(setXXX() 형태가 아니어도 적용가능!!!)
@Autowired public void prepare(AbstractService service){ this.service = service; } |
/////////////////////////////////////////////////////////////////////////////////////////////
@Autowired(required=false)
-프로퍼티를 반드시 설정할 필요가 없는 경우 사용
-default : true
AbstractController.java
package controller;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.servlet.ModelAndView;
import service.AbstractService;
public class AbstractController extends org.springframework.web.servlet.mvc.AbstractController {
@Autowired(required=false) 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
-required 속성의 값을 false로 지정할 경우, 해당 타입의 빈 객체가 존재하지 않더라도 예외가 발생하지 않음
(실제 로직 처리시 exception 발생)
-즉 위의 예제의 경우 <!-- bean id="abstractService" class="service.AbstractServiceImpl"></bean -->
처럼 주석 처리가 되어 있지만 @Autowired(required=false)이므로 웹서버가 설정파일 로드시 에러가 발생하지 않음.
required=true로 설정했다면 웹서버가 설정파일 로드시 에러발생
/////////////////////////////////////////////////////////////////////////////////////////////
@Qualifier
-동일한 타입의 빈 객체들 중 특정 빈을 사용하도록 설정
package controller;
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("service") 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; }
}
==========================================================================================
//아래처럼 사용해도 문제없음
private AbstractService service;
@Autowired public void prepare(@Qualifier("service") AbstractService service){ this.service = service; } |