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.