@ComponentScan
지정된 패키지 및 하위 패키지에서
기본적으로 어플리케이션의 메인 클래스에 선언.
Spring Component로 등록되어야 하는 클래스들을 검색한다.
= @Component, @Service, @Repository, @Controller, @Configuration이 붙은 클래스 Bean들을 찾아
Context에 bean등록한다.
@SpringBootApplication
@ComponentScan(basePackages = "com.example.myapp")
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
예를 들어, com.example.myapp.controller 패키지에
UserController 클래스가 존재한다면,
@ComponentScan 어노테이션은 해당 클래스를 Spring Component로 자동으로 등록한다.
https://standout.tistory.com/476
Annotation - @Component
@Component 개발자가 직접 작성한 Class를 Bean으로 등록하기 위한 Annotation 스프링 MVC에서는 @Component 어노테이션의 특화된 형태인 @Controller, @Service, @Repository 등이 사용된다. @Component public class MyComponent
standout.tistory.com
https://standout.tistory.com/482
Annotation - @Service
@Service 서비스 계층에서 사용되는 클래스임을 알림 public class UserService { @Autowired private UserRepository userRepository; public User getUserById(Long id) { return userRepository.findById(id).orElse(null); } public User saveUser(User
standout.tistory.com
https://standout.tistory.com/483
Annotation - @Repository
@Repository DAO class에서 쓰임 데이터베이스와 연동하여 데이터를 저장하거나 조회하는 데이터 계층의 클래스 @Repository public class UserRepository { @Autowired private JdbcTemplate jdbcTemplate; public User findById(Long
standout.tistory.com
https://standout.tistory.com/478
Annotation - @Controller
@Controller @Component 어노테이션의 특수한 종류 Spring MVC에서 Controller클래스 HTTP 요청을 처리하고, 적절한 비즈니스 로직을 호출 @Controller @RequestMapping("/user") public class UserController { @Autowired private UserS
standout.tistory.com
https://standout.tistory.com/484
Annotation - @Configuration
@Configuration Spring Framework가 해당 클래스를 구성 클래스로 인식하고 구성을 위해 사용한다. 다른클래스에서 @Autowired로 Bean을 부를 수 있다. import org.springframework.context.annotation.Bean; import org.springframe
standout.tistory.com
https://standout.tistory.com/474
Annotation - @Bean @Scope
@Bean 빈은 Spring 컨테이너에서 생성되고 관리되는 객체 개발자가 직접 제어가 불가능한 외부 라이브러리등을 Bean으로 만들려할 때 주로 사용된다. @Configuration public class MyConfiguration { @Bean public MyBe
standout.tistory.com
'JAVA > Spring' 카테고리의 다른 글
| Annotation - @EnableAutoConfiguration (0) | 2023.04.20 |
|---|---|
| Annotation - @SpringBootApplication (0) | 2023.04.20 |
| Annotation - @Configuration (0) | 2023.04.20 |
| Annotation - @Repository (0) | 2023.04.19 |
| Annotation - @Service (0) | 2023.04.19 |