| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 |
- grafana
- Python
- CVAT
- Kafka
- CSV
- airflow
- Network
- jvm
- tcp
- Trino
- Packet
- MAC address
- Vision
- kubernetes
- Operating System
- aws s3
- JavaScript
- PostgreSQL
- docker
- EC2
- helm
- log
- java
- OS
- Spring
- kubeadm
- zookeeper
- ip
- kubectl
- AWS
- Today
- Total
JUST WRITE
Bean Annotations 본문
이 글은 Baeldung 사이트 'Spring Bean Annotations'를 해석, 정리한 글입니다.

Bean Annotations
Spring에서 Bean 생성 관련 Annotation에 대해서 알아보려 한다.
해당 Annotation은 org.springframework.stereotype Package에 존재한다.
- @ComponentScan
- @Component
- @Repository
- @Service
- @Controller
- @Configuration
@ComponentScan
Spring은 자동으로 Package에서 Bean들을 검색할 수 있다.
@ComponentScan은 Annotation으로 설정한 Bean을 검색할 Package를 설정할 수 있다.
@Configuration
@ComponentScan(basePackages = "com.baeldung.annotations")
class VehicleFactoryConfig {}
@Configuration
@ComponentScan(basePackageClasses = VehicleFactoryConfig.class)
class VehicleFactoryConfig {}
// JAVA 8부터 반복 Annotation 가능
@Configuration
@ComponentScan(basePackages = "com.baeldung.annotations")
@ComponentScan(basePackageClasses = VehicleFactoryConfig.class)
class VehicleFactoryConfig {}
@Configuration
@ComponentScans({
@ComponentScan(basePackages = "com.baeldung.annotations"),
@ComponentScan(basePackageClasses = VehicleFactoryConfig.class)
})
class VehicleFactoryConfig {}
basePackage에서부터 Annotation으로 지정한 Bean 검색한다.
basePackage 없이 @Component Annotation을 사용하면 현재 있는 Package에서부터 검색한다.
Java 8에서부터 반복 Annotation이 가능하므로 3,4번째처럼 선언이 가능하다.
XML 설정을 한다면 아래와 같다.
<context:component-scan base-package="com.baeldung" />
@Component
@Component은 Class 레벨의 Annotation이다.
Spring Framework는 자동적으로 @Component로 지정된 Class를 찾는다.
기본적으로 소문자로 시작하는 Class명의 Bean Instance가 만들어진다.
@Repository, @Service, @Configuration, @Controller 모두 @Component의 meta-annotation들이다.
@Component
class CarUtility {
// ...
}
@Repository
DAO, Repository Class들은 Application에서 보통 Database Layer를 대표한다.
@Repository는 persistence exception을 자동으로 해석해준다.
예를 들어, Hibernate와 같은 Persistence Framework를 사용한다고 가정한다.
@Repository로 Annotation 된 Class에서 native exception이 발생한다.
그러면 자동으로 Spring의 DataAccessException으로 해석해준다.
그렇게 자동하기 위해서 PersistenceExceptionTranslationPostProcessor가 필요하다.
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
@Service
Application에서 Business Logic은 보통 Service Layer에서 이뤄진다.
보통 Class가 위 Layer에 있으면 해당 @Service Annotation을 사용한다.
@Service
public class VehicleService {
// ...
}
@Controller
해당 Class가 Spring MVC의 Controller로 작동할 때 해당 Annotation을 사용한다.
@Controller
public class VehicleController {
// ...
}
@Configuration
해당 Class에서는 @Bean Annotation을 한 Method가 사용될 수 있다.
@Configuration
class VehicleFactoryConfig {
@Bean
Engine engine() {
return new Engine();
}
}
'Programing > Spring' 카테고리의 다른 글
| @ControllerAdvice Exception 처리 (0) | 2022.01.03 |
|---|---|
| BeanFactory vs ApplicationContext (0) | 2021.12.21 |
| @Autowired (0) | 2021.10.22 |
| DispatcherSerlvet (0) | 2021.10.15 |
| MappingJackson2JsonView (0) | 2021.10.09 |