개발 공부/Spring
[Spring] Annotation
journey
2022. 2. 27. 03:33
728x90
Spring Annotation
- 어노테이션은 자바 1.5버전 부터 지원.
- 스프링은 어노테이션을 이용한 빈과 관련된 정보를 설정할 수 있다.
Spring Framework에서 annotation 사용하기.
- Spring Bean 등록 과정
- CommonAnnotationBeanPostProcessor 클래스를 설정파일에 bean 객체로 등록한다.
<bean class="org.springframework.beans.factory.annotation.CommonAnnotationBeanPostProcessor" />
- annotation
<context:annotation-config>
태그를 이용한다.
- @Autowired, @Required, @Resource, @PostConstructor, @PreDestroy 등의 annotation을 자동 처리해주는 bean post processor
- component Scan
<context:component-scan base-package="" />
태그를 이용한다.
- @Componenet, @Controller, @Service, @Repository 등의 annotation을 자동 처리
- MVC annotation
<mvc:annotation-driven />
- @RequestMapping, @Valid 등 Spring mvc component들을 자동 처리.
- HandlerMapping, HandlerAdapter를 등록하여 @Controller에 요청 연결.
- 해당 설정이 없어도 component-scan이 있으면 mvc application 동작
Stereotype annotation
component-scan에 의해 자동으로 등록
- @Component : stereotype annotation의 조상
- @Controller : Spring MVC에서 controller로 인식
- @Service : 역할 부여 없이 스캔 대상. 비즈니스 클래스(biz)에 사용
- @Repository : 일반적으로 dao에 사용. Exception을 DataAccessException으로 변환
- @Component
- 패키지 : org.springframework.streotype
- 버전 : spring 2.5
- 클래스에 선언하여 해당 클래스를 자동으로 bean 등록. 객체화
- bean의 이름은 해당 클래스의 이름(첫글자 소문자)
- 범위는 default로 singleton. @Scope로 지정 가능
- @Autowired
- 패키지 : org.springframework.beans.factory.annotation
- 버전 : spring 2.5
- Autowired annotation은 spring에서 의존관계를 자동으로 설정할 때 사용한다.
- 이 어노테이션은 생성자, 필드, 메서드 세곳에 적용이 가능하며 타입을 프로퍼티 자동 설정 기능을 제공한다. 즉, 해당 타입의 빈 객체가 없거나 2개 이상일 경우 예외를 발생시킨다.
- 프로퍼티 설정 메서드(setter) 형식이 아닌 일반 메서드에도 적용이 가능하다.
- 프로퍼티 설정이 필수가 아닐 경우 @Autowired(required=false)로 선언한다. (default : true)
- byType으로 의존관계를 자동으로 설정할 경우 같은 타임의 빈이 2개 이상 존재하게 되면 예외가 발생하는데, @Autowired에서도 같은 문제가 발생한다.
- 이 때, @Qualifier를 사용하면 동일한 타입의 빈 중 특정 빈을 사용하도록 하여 문제를 해결할 수 있다.
- Example)
@Autowired
@Qualifier("test")
private Test test;
- @Qualifier
- 패키지 : org.springframwork.beans.factory.annotation
- 버전 : spring 2.5
- @Autowired annotation이 타입 기반이기 때문에 2개 이상의 동일타입 빈 객체가 존재할 시 특정 빈을 사용하도록 선언한다.
- @Qualifier("beanName")의 형태로 @Autowired와 같이 사용하며 메서드에서 두 개 이상의 파라미터를 사용할 경우에는 파라미터 앞에 선언해야 한다.
- @Require
- 패키지 : org.springframework.beans.factory.annotation
- 버전 : spring 2.5
- 필수 프로퍼티임을 명시하는 것으로, 프로퍼티 설정 메서드(setter)에 붙이면 된다.
- 필수 프로퍼티를 설정하지 않을 경우 빈 생성시 예외를 발생시킨다.
- @Repository
- 패키지 : org.springframework.streotype
- 버전 : spring 2.0
- dao에 사용되며 Exception을 DataAccessException으로 변환한다.
- @Service
- 패키지 : org.springframework.sterotype
- 버전 : spring 2.0
- @Service를 적용한 class는 비즈니스 로직(biz)로 간주한다.
- @Resource
- 패키지 : javax.annotation.Resource
- 버전 : java6 & jee5
- 어플리케이션에서 필요로 하는 자원을 자동 연결할 대 사용한다.
- name 속성에 자동으로 연결될 빈 객체의 이름으로 입력한다.
- @Resource(name="testDao") // byName -> byType
객체 생성과 @Autowired
xml로 객체 생성.
public void setExam(Exam exam) {
this.exam = exam;
}
// IoC 컨테이너. 객체를 생성하고 조립해주는 과정의 객체를 담는 것.
// exam 객체 만들고
<bean id="exam" class="com.test01.Student" />
// console 객체 만들고
<bean id="console" class="com.test01.Console">
// console 객체가 exam 객체를 참조한다.
<property name="exam" ref="exam">
</bean>
@Autowired
@Autowired
public void setExam(Exam exam) {
this.exam = exam;
}
<context:annotation-config />
<bean id="exam" class="com.test01.Student" />
<bean id="console" class="com.test01.Console">
</bean>
@Autowired의 위치
- 필드
- constructor
- setter