[Spring] @Bean, @Component 차이

서규범·2022년 10월 20일
0

@Bean

  • 개발자가 컨트롤이 불가능한 외부 라이브러리들을 Bean으로 등록하고싶은 경우에 사용
  • ex) Spring Security에서 제공하는 PasswordEncoder는 Spring Security 개발자에 의해 만들어진 클래스(라이브러리)
@Bean
public PasswordEncoder passwordEncoder() {
	return new BCryptPasswordEncoder();
}

@Component

  • 개발자가 직접 컨트롤할 수 있는 클래스에 사용
@Component
public class Utility {
   // ...
}

정리

  • 개발자가 직접 제어 가능한 경우 : @Component
  • 개발자가 직접 제어 불가능한 경우 : @Configuration + @Bean
profile
하려 하자

0개의 댓글