@Component vs @Configuration

Haechan Kim·2025년 9월 26일

Spring

목록 보기
71/72

@Component와 @Configuration 모두 스프링 Bean으로 등록할 클래스를 표기하는 어노테이션이다.

@Component

개발자가 직접 작성한 클래스를 Bean으로 등록할 때 사용한다.
@Controller, @Service, @Repository의 기반이 되는 어노테이션이다.

일반 비지니스 로직을 담은 클래스에 사용되고 컴포넌트 스캔의 대상이 된다.

@Configuration


Bean 설정을 담당하는 클래스임을 나타내며, 하나 이상의 @Bean 메서드를 포함하는 클래스에 사용된다.

@Configuration
public class AppConfig {
    @Bean
    public DataSource dataSource() {
        return new HikariDataSource();
    }
    
    @Bean
    public UserRepository userRepository() {
        return new UserRepository(dataSource()); // 싱글톤 보장
    }
}

사용 시기

@Component@Configuration
직접 작성한 클래스 빈으로 등록할 때외부 라이브러리 객체를 빈으로 등록할 때
단순 클래스 자체 빈으로 등록할 때여러 빈 생성, 의존성 설정할 때
복잡한 빈 설정 필요할 때

0개의 댓글