# Spring Boot Core 어노테이션 정리 Spring Boot Core에서 사용되는 주요 어노테이션을 표로 정리한 후, 각각의 어노테이션에 대한 설명과 예제를 제공합니다. --- ## 1. 어노테이션 정리표 | 어노테이션 | 설명 | |------------------------|----------------------------------| | `@SpringBootApplication` | Spring Boot 애플리케이션의 시작점 설정 | | `@Configuration` | 스프링 설정 클래스를 정의 | | `@ComponentScan` | 스프링이 컴포넌트를 자동 검색하도록 설정 | | `@Bean` | 수동으로 빈을 등록 | | `@Component` | 일반적인 빈을 정의 | | `@Service` | 비즈니스 로직을 담당하는 빈을 정의 | | `@Repository` | 데이터 접근 계층 빈을 정의 | | `@Autowired` | 자동으로 빈을 주입 | | `@Qualifier` | 특정 빈을 지정하여 주입 | | `@Primary` | 기본적으로 주입될 빈을 지정 | | `@Value` | 프로퍼티 값을 주입 | | `@PropertySource` | 외부 설정 파일을 로드 | | `@Profile` | 특정 프로파일에서만 빈을 로드 | | `@Lazy` | 필요한 경우에만 빈을 초기화 | | `@Scope` | 빈의 스코프를 지정 | | `@DependsOn` | 특정 빈이 다른 빈보다 먼저 로드되도록 설정 | | `@PostConstruct` | 빈이 생성된 후 실행할 메서드 지정 | | `@PreDestroy` | 빈이 제거되기 전 실행할 메서드 지정 | --- ## 2. 어노테이션 설명 및 예제 ### 1) `@SpringBootApplication` Spring Boot 애플리케이션의 시작 클래스를 정의하는 어노테이션. - 내부적으로 `@Configuration`, `@EnableAutoConfiguration`, `@ComponentScan`을 포함. #### 예제: ```java @SpringBootApplication public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } ``` - 이 클래스가 애플리케이션의 시작점 역할을 합니다. --- ### 2) `@Configuration` Java 기반 설정을 정의하는 클래스에 사용. #### 예제: ```java @Configuration public class AppConfig { @Bean public MyService myService() { return new MyService(); } } ``` - `MyService` 객체를 빈으로 등록. --- ### 3) `@ComponentScan` 패키지를 검색하여 `@Component`, `@Service`, `@Repository` 등을 자동으로 등록. #### 예제: ```java @ComponentScan(basePackages = "com.example.service") @Configuration public class AppConfig { } ``` - `"com.example.service"` 패키지를 스캔하여 빈 등록. --- ### 4) `@Bean` 메서드의 반환 객체를 스프링 빈으로 등록. #### 예제: ```java @Configuration public class BeanConfig { @Bean public MyService myService() { return new MyService(); } } ``` - `myService()`의 반환 객체가 빈으로 등록. --- ### 5) `@Component` 일반적인 스프링 빈을 정의할 때 사용. #### 예제: ```java @Component public class MyComponent { public void doSomething() { System.out.println("Component working"); } } ``` - 스프링이 자동으로 빈으로 등록. --- ### 6) `@Service` 비즈니스 로직을 담당하는 서비스 계층의 빈을 정의. #### 예제: ```java @Service public class MyService { public String getMessage() { return "Hello, Service!"; } } ``` - 서비스 계층의 빈으로 등록. --- ### 7) `@Repository` 데이터 접근 계층의 빈을 정의. #### 예제: ```java @Repository public class MyRepository { public String findData() { return "Data from DB"; } } ``` - DAO(Data Access Object) 역할을 하는 클래스. --- ### 8) `@Autowired` 빈을 자동으로 주입. #### 예제: ```java @Component public class MyController { private final MyService myService; @Autowired public MyController(MyService myService) { this.myService = myService; } } ``` - `MyService` 빈이 자동 주입됨. --- ### 9) `@Qualifier` 같은 타입의 여러 빈 중 특정 빈을 선택하여 주입. #### 예제: ```java @Component public class FirstService implements MyService { } @Component public class SecondService implements MyService { } @Component public class MyController { private final MyService myService; @Autowired public MyController(@Qualifier("secondService") MyService myService) { this.myService = myService; } } ``` - `secondService` 빈이 주입됨. --- ### 10) `@Primary` 여러 빈이 있을 때 기본 빈을 지정. #### 예제: ```java @Primary @Component public class DefaultService implements MyService { } ``` - `@Autowired` 시 기본적으로 `DefaultService`가 주입됨. --- ### 11) `@Value` 설정 파일에서 값을 주입. #### 예제: ```java @Component public class ConfigComponent { @Value("${app.name}") private String appName; } ``` - `application.properties`에서 `app.name` 값을 읽음. --- ### 12) `@PropertySource` 설정 파일을 로드. #### 예제: ```java @Configuration @PropertySource("classpath:app.properties") public class AppConfig { } ``` - `app.properties` 파일을 로드. --- ### 13) `@Profile` 특정 프로파일에서만 빈을 로드. #### 예제: ```java @Profile("dev") @Component public class DevService { } ``` - `"dev"` 프로파일에서만 로드. --- ### 14) `@Lazy` 빈을 필요할 때만 초기화. #### 예제: ```java @Component @Lazy public class LazyComponent { } ``` - `LazyComponent`는 필요할 때만 생성됨. --- ### 15) `@Scope` 빈의 스코프를 설정. #### 예제: ```java @Component @Scope("prototype") public class PrototypeComponent { } ``` - 새로운 객체가 매번 생성됨. --- ### 16) `@DependsOn` 다른 빈이 먼저 로드되도록 설정. #### 예제: ```java @Component @DependsOn("anotherComponent") public class MyComponent { } ``` - `anotherComponent`가 먼저 로드됨. --- ### 17) `@PostConstruct` 빈 생성 후 실행할 메서드 지정. #### 예제: ```java @Component public class InitComponent { @PostConstruct public void init() { System.out.println("Initialized!"); } } ``` - 빈 생성 후 `init()` 실행. --- ### 18) `@PreDestroy` 빈 제거 전에 실행할 메서드 지정. #### 예제: ```java @Component public class DestroyComponent { @PreDestroy public void destroy() { System.out.println("Destroyed!"); } } ``` - 빈 제거 전 `destroy()` 실행. --- Spring Boot Core에서 사용되는 주요 어노테이션을 정리했습니다. 이를 활용하면 애플리케이션의 설정과 빈 관리를 효과적으로 수행할 수 있습니다.