swimminginthecode DIVE!

BACK/Spring

Spring 02 - annotation

dazz6 2024. 7. 4. 17:40
@Configuration
스프링 IoC Container에게 해당 Class가 Bean 구성 Class 임을 알려 주는 어노테이션.
@Bean을 해당 클래스의 메소드에 적용하면 @Autowired로 빈을 부를 수 있다.

@Bean
개발자가 직접 제어가 불가능한 외부 라이브러리 등을 Bean으로 만들려고 할 때 사용되는 어노테이션

@Component
개발자가 직접 작성한 Class를 Bean으로 등록하기 위한 어노테이션.
@ComponentScan 선언에 의해 특정 패키지 안의 클래스들을 자동 스캔하여 @Component 어노테이션이 있는 클래스들에 대하여 Bean 인스턴스를 생성.
Component에 대한 추가 정보가 없다면 Class의 이름을 camelCase로 변경한 것이 Bean id로 사용되지만, @Component는 name이 아닌 value를 이용해 Bean의 이름을 지정.

@Autowired
field, setter method, constructor에 사용하며 type에 따라 알아서 Bean을 주입해 주는 역할.
객체에 대한 의존성을 주입하며, 해당 어노테이션을 사용하면 스프링이 자동으로 값을 할당.

@Controller, @Service, @Repository
종류 설명
@Controller Spring MVC의 Controller로 사용되는, 클래스 선언을 단순화해 주는 어노테이션
@Service Service class에서 쓰이는 어노테이션으로, 비즈니스 로직을 수행하는 Class라는 것을 나타내는 용도
@Repository DAO Class에서 쓰이는 어노테이션으로, DB에 접근하는 method를 가지고 있는 Class에서 쓰임

@ComponentScan
@Component, @Service, @Repository, @Controller, @Configuration이 붙은 빈을 찾아서 Context에 빈을 등록해 주는 어노테이션.
@Component 어노테이션이 있는 클래스들에 대하여 Bean 인스턴스를 생성.

 

 

참고 블로그

[Spring] 어노테이션(Annotation) (velog.io)

'BACK > Spring' 카테고리의 다른 글

Spring 04 - annotation  (0) 2024.07.15
Spring 03 - annotation  (0) 2024.07.12
Spring 03  (0) 2024.07.11
Spring 02  (0) 2024.07.04
Spring 01  (0) 2024.07.03