Bean with Component Scan(2)
π‘ How to add Bean
@Bean
λ±λ‘νλ λ°©λ²- 1οΈβ£ manual bean configuration with
@Configuration
- 2οΈβ£ component scan with
@ComponentScan
,@Autowired
- μ΄ κ²μκΈμμλ 2οΈβ£λ² λ°©λ²μ λν΄ μ€λͺ
β Component Scan
- Spring does not magically know what to add as bean β‘οΈ scans your project marked as bean
this scanning is called:
@Component Scan
@Component Scan
μ@Component
μ΄λ Έν μ΄μ μ΄ λΆμ ν΄λμ€λ λͺ¨λSpring Bean
μΌλ‘ μμμ λ±λ‘νλ€.@Component
@Controller
@Service
@Repository
@Configuration
- μμ‘΄μ± μ£Όμ
:
@Autowired
@Autowired
κ° λΆμ μμ±μλ μλμΌλ‘ ν΄λΉSpring Bean
μ μ°Ύμμ μμ‘΄μ±μ μ£Όμ νλ€.- 1οΈβ£ (before)manual bean configuration
Bean
μΌλ‘ λ±λ‘νκ³ μΆμ ν΄λμ€λ€μAppConfig
μ λͺ μν΄μΌ νμ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Configuration
public class AppConfig {
@Bean
public MemberService memberService(){
return new MemberServiceImpl(memberRepository());
}
@Bean
public OrderService orderService(){
return new OrderServiceImpl(
memberRepository(),
discountPolicy());
}
}
- 2οΈβ£ component scan with
@ComponentScan
- μ΄μ
config
νΌμΌμ ν ν λΉμλ€! @ComponentScan
μ@Component
κ° λΆμ ν΄λμ€λ₯Ό μ°Ύμ spring beanμΌλ‘ λ±λ‘νλ€.
1
2
3
@Configuration
@ComponentScan
public class AutoAppConfig {}
- κ·Έλ¦¬κ³ beanμΌλ‘ λ±λ‘νκ³ μΆμ ꡬ체 ν΄λμ€μ
@Component
λ₯Ό λΆμΈλ€. - 그리κ³
@Autowired
κ° λΆμ λ©μλλ₯Ό μλμΌλ‘ μμ‘΄μ±μ μ£Όμ νλ€.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Component //μλμΌλ‘ spring beanλ±λ‘
public class OrderServiceImpl implements OrderService {
private final MemberRepository repository;
private final DiscountPolicy discountPolicy;
@Autowired //μλμΌλ‘ μμ‘΄μ± μ£Όμ
public OrderServiceImpl(MemberRepository repository, DiscountPolicy discountPolicy) {
this.repository = repository;
}
@Component //μλμΌλ‘ spring beanλ±λ‘
public class MemberServiceImpl implements MemberService{
private final MemberRepository memberRepository;
@Autowired //μλμΌλ‘ μμ‘΄μ± μ£Όμ
public MemberServiceImpl(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
}
}
@Component //μλμΌλ‘ spring beanλ±λ‘
public class OrderServiceImpl implements OrderService {
}
β Component Scan λ²μ μ§μ νκΈ°
- place
config file
in root of project file basePackages
: search below this packageexcludeFilters
: do not add this class as bean
1
2
3
4
5
6
@Configuration
@ComponentScan(
basePackages = "com.example.spring_basic",
excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Configuration.class )
)
public class AutoAppConfig { }
β Filter
- filter what/what not to include in component scan
includeFilters
excludeFilters
1
2
3
4
5
6
@Configuration
@ComponentScan(
includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = MyIncludeComponent.class),
excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = MyExcludeComponent.class))
static class ComponentFilterAppConfig{
}
β Bean duplication, bean collision
- β What happens if bean names are identical?
- μ»΄ν¬λνΈ μ€μΊμ μν΄ μλμΌλ‘ μ€νλ§ λΉμ΄ λ±λ‘λλλ°, κ·Έ μ΄λ¦μ΄ κ°μ κ²½μ° μ€νλ§μ μ€λ₯ λ°μ μν΄
π΄
ConflictingBeanDefinitionException
μμΈ λ°μ- β What happens if bean is added both manually and auto with component scan?
- There are two ways of adding bean
- 1οΈβ£ manual bean configuration with
@Configuration
- 2οΈβ£ component scan with
@ComponentScan
λ λ°©λ² λͺ¨λμΌλ‘ κ°μ beanμ΄ λ λ² λ±λ‘λλ€λ©΄?
- 1οΈβ£ manual bean configuration has priority
manual bean
overridescomponent scan
- π΄ κ·Έλ¬λ κΆμ₯νλ λ°©λ²μ΄ μλλ©°, Springbootλ μ΅κ·Όμ μ€λ₯λ₯Ό λ°μμν€λλ‘ λ°λ
β
β
β
β
β
This post is licensed under CC BY 4.0 by the author.