[Spring]Spring DI(2) 복습__개발공부 87일차
스프링 프레임워크 사용하면
객체 생성 + 조립
new 연산자 사용해서 객체 생성할 필요가 없다.
( Sample s = new Sample(); X )
예제별 요약
[springDI 프로젝트]
ApplicationContext 스프링 컨테이너 ctx 객체 생성 <- xml설정 파일
ctx.getBean(빈 이름)
[springDI02 프로젝트]
ApplicationContext 스프링 컨테이너 ctx 객체 생성 <- config.java 자바코딩(파일)
ctx.getBean(빈 이름)
[springDI03 프로젝트]
의존하는 객체를 주입할떄 자동으로 의존 주입 @Autowire,@Resource, @inject 어노테이션
// @Autowired
@Resource( name = "record" )
필드 선언
private RecordImple record = null;
@Autowired
생성자
@Autowired
setter
[springDI04 프로젝트]
스프링 컨테이너 안에 스프링 빈 객체 * 수백개 이상.. + 조립 -> 자동
<bean></bean>
<bean></bean>
<bean></bean>
<bean></bean>
:
:
<bean></bean>
<bean></bean>
DI
-> 콤퍼넌트 스캔(스프링 기능)
[스캔 대상이 되는 클래스] 스프링 빈 객체 생성
@Component
public class RecordImpl{}
@Component
public class RecordViewImpl{ //recordViewImpl 로 자동으로 잡힘
@Autowired
private RecordImple record = null
}
xml 파일
<context:component-scan base-package="부모패키지"></context:component-scan>
부모패키지만 주면 자동으로 하이 패기지까지 자동으로 스캔한다.
<context:component-scan base-package="di"></context:component-scan>
p118 Top
@Component 어노테이션
ㄴ@Service 서비스 클래스
ㄴ@Repository : DAO 클래스
ㄴ@Controller : MV[C] 컨트롤러 클래스
p119 스캔 대상 클래스 범위 지정하기
<context:component-scan base-package="di">
<context:include-filter 특정패키지는 포함>
<context:exclude-filter 특정패키지는 제외>
</context:component-scan>