[ Spring ] @Configuration Annotation
2024. 3. 24. 16:21ㆍ· BACK-END/└ Spring Boot
[ `@Configuration` 어노테이션 ]
`@Configuration` 어노테이션은 Spring Framework에서 사용되는 어노테이션 중 하나로, 해당 클래스가 하나 이상의 Bean을 정의하고, Spring 컨테이너에게 Bean 정의를 제공한다는 것을 나타낸다. 일반적으로 XML 파일 대신 Java 클래스에서 설정을 정의하는데, 이때 `@Configuration` 어노테이션을 붙인 클래스를 사용합니다.
아래는 `@Configuration` 어노테이션을 사용한 Java 기반 설정 클래스의 예시입니다:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
@Bean
public AnotherService anotherService() {
return new AnotherServiceImpl();
}
}
위의 예시에서 `@Configuration` 어노테이션이 붙은 `AppConfig` 클래스는 두 개의 Bean을 정의하고 있습니다. `@Bean` 어노테이션은 해당 메서드가 Bean을 생성하고 반환한다는 것을 나타냅니다. 이 클래스를 스프링 애플리케이션 컨텍스트에 등록하면, `myService`와 `anotherService`라는 두 개의 Bean이 생성되어 사용할 수 있게 됩니다.
이렇게 `@Configuration` 어노테이션을 사용하면 스프링이 해당 클래스를 스캔하고, 그 안에 정의된 Bean들을 컨테이너에 등록하게 됩니다. 이를 통해 코드 기반의 설정을 효과적으로 관리하고, 의존성 주입 및 빈 생성을 선언적으로 정의할 수 있습니다.
개인 공부 기록용입니다:)
Chat GPT와 함께
728x90
'· BACK-END > └ Spring Boot' 카테고리의 다른 글
[ Spring / jsp ] 페이징 이해 - 클래스로 나누지 않고 풀어보기 (0) | 2024.03.31 |
---|---|
[ Spring MVC ] MVC Patterns (1) | 2024.03.25 |
[ Spring ] @RequestMapping Annotation (0) | 2024.02.17 |
[ Spring ] @RequestParam Annotation (0) | 2024.02.17 |
[ Spring ] @ModelAttribute Annotation (0) | 2024.02.15 |