waitedForU
[27][spring] Annotation 설정, component-scan 본문
[01] Annotation 설정
- Annotation 설정은 코드에 설정정보를 입력함으로써 설정파일을 사용하지 않거나
설정파일 크기를 줄이는데 유용합니다.
- @Autowired - 의존하는 객체 주입 (필드,메소드/setter,생성자 에서 사용)
- @Resource - 의존하는 객체 주입(필드,setter 에서 사용)
- @PostConstruct - 객체 생성후 호출
- @PreDestroy - 객체가 소멸되기 바로전 호출
- @Qualifier - 자동설정 제한하며, 수식어를 값으로 가짐
- @Required - 필수 프로퍼티를 명시 (setter 메소드에서 사용)
1. Annotation 예제 확인
- 스프링 프로젝트의 설정
Project Type: Spring Project --> Simple Spring Utility Project
Name: AnnoTest
Package: spring.sts.annotation
Library: 프로젝트 생성시 관련 Spring 라이브러리가 자동으로 다운로드
됩니다. 작업 컴퓨터는 인터넷에 연결되어 있어야 합니다.
packate: spring.sts.annotation
import javax.annotation.PostConstruct;
import org.springframework.context.support.AbstractApplicationContext;
>>>>>> annotation.xml
- @Component - @Repository, @Service 를 포괄적으로 사용
- @Controller - @Component가 대신 사용될수 없음
- @Service - business logic 의 서비스 패턴 클래스 에서 사용
- @Repository - database access logic의 DAO 패턴 클래스 에서 사용
[02] xml설정파일에 등록없이 빈으로 사용가능한 스캔
1. <context:component-scan>
- @Component, @Service, @Controller가 적용된 클래스를 자동으로 등록해줍니다.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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:component-scan base-package="spring.sts.annotation.*" />
</beans>
2. <context:include-filter>
<context:exclude-filter>
- 자동스캔대상에 포함시킬 클래스와 포함시키지 않을 클래스를 구체적으로
명시할 수 있습니다.
<context:component-scan base-package="sts.hello.semple">
<context:include-filter type="regex" expression="
.*Mgr"/>
<context:exclude-filter type="aspectj" expression="
..*Mgr"/>
</context:component-scan>
- regex:정규표현식
- aspectj:Aspectj표현식
'Mvc_Spring' 카테고리의 다른 글
[28][spring] @MVC, @MVC 기반 Spring MVC 프로젝트 실습, Local 저장소 변경 (2) | 2016.05.27 |
---|---|
[25][spring] Spring기반 MVC의 원리(주요구성요소, 처리순서,web.xml 설정) (0) | 2016.05.26 |
[21][Spring] STS 다운로드 및 설치, Spring Framework 특징, Spring Framework 주요 모듈 (1) | 2016.05.26 |