<์ถœ์ฒ˜ ์ธํ”„๋Ÿฐ>

 

์ปดํฌ๋„ŒํŠธ ์Šค์บ”๊นŒ์ง€ ์–ด์ฐŒ์ €์ฐŒ ์™”๋‹ค. ์‚ฌ์‹ค ๋ธ”๋กœ๊ทธ ์ •๋ฆฌ๋งŒ ์•„๋‹ˆ์—ˆ์–ด๋„ ๊ฐ•์˜๋Š” ํ•œ๋‹ฌ ์•ˆ์— ๋๋ƒˆ์„ ๋“ฏ... ๊ทธ๋ž˜๋„ ๋ธ”๋กœ๊ทธ ์ •๋ฆฌํ•˜๋ฉด์„œ ํ‰์†Œ ๊ทธ๋ƒฅ ๋„˜๊ฒผ์„ ์งˆ๋ฌธ๋“ค ๋ชฝ๋•… ์ฐพ์•„๋ณด๋Š” ์ค‘์ด๋‹ค. ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ์ง€๋งŒ ๊ทธ๋งŒํผ ๋‚ด ๋จธ๋ฆฌ์˜ ์ง€์‹๋„ ํ•˜๋‚˜ ๋‘˜ ์ฑ„์›Œ์ง€๊ณ  ์žˆ์Œ์„ ๋Š๋‚€๋‹ค. (์‚ฌ์‹ค ๋ธ”๋กœ๊ทธ ์ •๋ฆฌ์— ์˜ค๋žœ ์‹œ๊ฐ„์ด ๊ฑธ๋ ค ํ˜„ํƒ€์™€์„œ ์ฃผ์ ˆ์ฃผ์ ˆ ์ ์–ด๋ณธ๋‹ค) ์ด๋ฒˆ ์„น์…˜ 6์€ ๊ฐ•์˜๊ฐ€ ์งง์•„์„œ ๋นจ๋ฆฌ ๋๋‚ ์ง€๋„ ๋ชจ๋ฅธ๋‹ค๋Š” ๊ธฐ๋Œ€๊ฐ์ด ์žˆ๋‹ค. ์šฐ๋ฆฌ์ง‘ ์—์–ด์ปจ ๊ณ ์žฅ๋‚˜์„œ ์•ˆ๋‚˜์˜ค์ง€๋งŒ,,,๐Ÿฅฒ ๋”์œ„ ์ด๊ฒจ๋‚ด๊ณ ! ๋ชจ๋‘๋“ค ํ™”์ดํŒ…!!!

 

 

 

๐Ÿงธ 


 

๐Ÿงธ ๊ตฌ์กฐํŒŒ์•… ๋ ˆ์ธ ๊ผฌ์šฐ- ๐Ÿงธ

  • main
    ใ„ด class : AutoAppConfig
  • test - scan
    ใ„ด class : AutoAppConfigTest

  • test - scan - filter
    ใ„ด class : BeanA, BeanB, ComponentFilterAppConfigTest

    ใ„ด Annotation : MyExcludeComponent, MyIncludeComponent

 

 


 

Q.  ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์ด๋ž€?

A. ์„ค์ •์ •๋ณด๊ฐ€ ์—†์–ด๋„ ์ž๋™์œผ๋กœ ์Šคํ”„๋ง ๋นˆ์„ ๋“ฑ๋กํ•œ๋‹ค.

 

 

 

โœ”๏ธ  AutoAppConfig

์ปดํฌ๋„ŒํŠธ ์Šค์บ” ์„ค์ •

 

@ComponentScan(
        basePackages = "hello.core.member",
        basePackageClasses = AutoAppConfig.class,
        excludeFilters = @ComponentScan.Filter
			(type = FilterType.ANNOTATION, classes = Configuration.class)
)

 

 

@ComponentScan

: @Component์–ด๋…ธํ…Œ์ด์…˜ ๋ฐ streotype(@Service, @Repository, @Controller.)์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ€์—ฌ๋œ ํด๋ž˜์Šค๋“ค์„ ์ž๋™์œผ๋กœ Scanํ•˜์—ฌ Bean์œผ๋กœ ๋“ฑ๋กํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ด๋‹ค.

 

 

basePackages

:  String์œผ๋กœ ์ž…๋ ฅ๋œ ํŒจ์บ์ง€์˜ ๊ฒฝ๋กœ๋ฅผ ์Šค์บ”ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์ง์ ‘ ์Šค์บ”ํ•  ์œ„์น˜๋ฅผ ์ง€์ •ํ•œ๋‹ค.

 

 

basePackageClasses

์ ํžŒ ํด๋ž˜์Šค๊ฐ€(ํŒจํ‚ค์ง€ ์•ˆ) ์œ„์น˜ํ•œ ๊ณณ๋ถ€ํ„ฐ ๋ชจ๋“  ์–ด๋…ธํ…Œ์ด์…˜์„ ๋ถ€์—ฌ๋œ ํด๋ž˜์Šค๋ฅผ bean์œผ๋กœ ๋“ฑ๋กํ•ด์ค€๋‹ค. 

 

 

excludeFilters 

:  ๊ฐ์ฒด๋ฅผ ์ œ์™ธํ•œ๋‹ค. (AppConfig์— ์ˆ˜๋™์œผ๋กœ ๋“ฑ๋กํ•œ ๊ฒƒ๋“ค์€ ๋นผ์ค€๋‹ค)

 

 

includeFilters

@Component ์–ด๋…ธํ…Œ์ด์…˜์ด ์‚ฌ์šฉ๋œ ๊ฐ์ฒด๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ณ  ์‹ถ์œผ๋ฉด,  includeFilters ์„ค์ •์„ ์‚ฌ์šฉํ•œ๋‹ค. ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ํด๋ž˜์Šค๋งŒ์„ ์Šค์บ”ํ•˜๋„๋ก ์ง€์ •ํ•œ๋‹ค. 

 

 

 

- @ComponentScan ์ถœ์ฒ˜

 

Spring - @ComponentScan ์–ด๋…ธํ…Œ์ด์…˜์ด๋ž€?

@ComponentScan @Componentscan ์–ด๋…ธํ…Œ์ด์…˜์€ @Component์–ด๋…ธํ…Œ์ด์…˜ ๋ฐ streotype(@Service, @Repository, @Controller.)์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ€์—ฌ๋œ Class๋“ค์„ ์ž๋™์œผ๋กœ Scanํ•˜์—ฌ Bean์œผ๋กœ ๋“ฑ๋กํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ž…๋‹ˆ

galid1.tistory.com

 

 

- @ComponentScan ์ถœ์ฒ˜

 

[Spring] @ComponentScan - basePackages, basePackageClasses

@ComponentScan@Configuration์ด ๋ถ™์€ ๋นˆ ์„ค์ • ์ž๋ฐ”ํด๋ž˜์Šค์— ๋ถ™์—ฌ์„œ ์Šคํ…Œ๋ ˆ์˜ค ํƒ€์ž… ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ...

blog.naver.com

 

 

 

Q. basePackages๋ณด๋‹ค basePackagesClasses๋ฅผ ์“ฐ๋Š”๊ฑธ ๊ถŒ์žฅํ•œ๋‹ค. ์™œ ๊ทธ๋Ÿฐ ๊ฒƒ์ผ๊นŒ?

A. ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์ด ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š”์ง€ ์ดํ•ดํ•ด์•ผํ•œ๋‹ค. ์ปจํฌ๋„ŒํŠธ ์Šค์บ”์„ ํ•˜๋ ค๋ฉด @SpringBootApplication์„ ์‚ดํŽด๋ด์•ผํ•œ๋‹ค. @SpringBootApplication์—๋Š” @ComponentScan์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ  @ComponentScan์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ด ์‹œ์ž‘์ง€์ ์ด๋‹ค. @ComponentScan์„ ๋ถ™์ด๊ณ  ์žˆ๋Š” ๊ทธ ํด๋ž˜์Šค๋ถ€ํ„ฐ ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์„ ์‹œ์ž‘ํ•œ๋‹ค. ์„ค์ •ํ•ด ๋†“์€ ํŒจํ‚ค์ง€๋“ค ์ค‘์—์„œ, ํ•ด๋‹น ํŒจํ‚ค์ง€์™€ ํ•˜์œ„ ํŒจํ‚ค์ง€์— ๋Œ€ํ•ด์„œ @Component ๊ฐ™์€ ์Šคํ…Œ๋ ˆ์˜ค ํƒ€์ž… ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ํด๋ž˜์Šค๋ฅผ ๋ชจ๋‘ ์ฐพ์•„์„œ ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ์ด๋Ÿฐ์‹์œผ๋กœ ๊ฐ€๋Š”๊ฒŒ spring์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋ณธ์ ์ธ ๊ฒฝ๋กœ์ธ ๊ฒƒ ๊ฐ™๋‹ค. ๊ทธ๋Ÿฐ๋ฐ basePackages๋Š” ๋‚ด๊ฐ€ ์ง์ ‘ ๊ฒฝ๋กœ๋ฅผ ์ ๋Š” ๊ฒƒ์œผ๋กœ springboot์—์„œ ์ œ๊ณต๋˜๋Š” ์ตœ์ƒ์œ„ ํŒจํ‚ค์ง€๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์ง€ ์•Š๊ฒŒ๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. springboot์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋ณธ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ํšจ์œจ์ ์ด๊ณ  ํ–ฅํ›„ ํ”„๋กœ์ ํŠธ๊ฐ€ ์ปค์ง์— ๋”ฐ๋ผ ๋ณต์žก์„ฑ์„ ์ค„์—ฌ์ค€๋‹ค.

 

 

- basePackage๋ฅผ ๊ถŒ์žฅํ•˜์ง€ ์•Š๋Š” ์ด์œ  ์ถœ์ฒ˜

 

spring[5] ComponentScan๊ณผ Component

Component Sacn ์ง€๊ธˆ๊นŒ์ง€ ๋นˆ์„ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•ด์„œ AppConfig class์— ์‚ฌ์šฉ์ž๊ฐ€ ์ผ์ผํžˆ @Bean์œผ๋กœ ๊ด€๋ฆฌํ•ด์ฃผ์–ด์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๊ฑด ๊ฐœ๋ฐœ์ž์—๊ฒŒ ๋„ˆ๋ฌด ๊ท€์ฐฎ์€ ์ž‘์—…์ด๊ณ  ๋ˆ„๋ฝํ•˜๊ฒŒ ๋˜๋ฉด ํฐ ์žฅ์• ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ

ililil9482.tistory.com

 

- ComponentScan ๋™์ž‘ ์ถœ์ฒ˜

 

์Šคํ”„๋ง ํ•ต์‹ฌ ๊ธฐ์ˆ -03.@Component์™€ ์ปดํฌ๋„ŒํŠธ ์Šค์บ”

IoC ์ปจํ…Œ์ด๋„ˆ 4๋ถ€ : @Component์™€ ์ปดํฌ๋„ŒํŠธ ์Šค์บ”

jaeuk2274.github.io

 

 

 

••• ์ด์–ด์„œ ๋‹ค๋ฅธ ์ฝ”๋“œ ๋ถ€๋ถ„ •••

 

 

 

public class AutoAppConfig {

}

 

ํ˜„์žฌ AutoAppConfig ํด๋ž˜์Šค์˜ ์—ญํ• ์€ ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์„ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”์„œ๋“œ๋‚˜ ๋‹ค๋ฅธ ์ฝ”๋“œ๋Š” ์ ์ง€ ์•Š๋Š”๋‹ค.

 

 

 

 


 

 

 

 

โœ”๏ธ  OrderServiceImpl

์˜์กด๊ด€๊ณ„์ž๋™์ฃผ์ž…

 

@Component
public class OrderServiceImpl implements OrderService{

    private final MemberRepository memberRepository;
    private final DiscountPolicy discountPolicy;

    @Autowired
    public OrderServiceImpl(MemberRepository memberRepository, DiscountPolicy discountPolicy) {
        this.memberRepository = memberRepository;
        this.discountPolicy = discountPolicy;
    }
    
    ...
}

 

 

@Component

@Component ์–ด๋…ธํ…Œ์ด์…˜์„ ์ด์šฉํ•˜๋ฉด Bean Configuration ํŒŒ์ผ์— Bean์„ ๋”ฐ๋กœ ๋“ฑ๋กํ•˜์ง€ ์•Š์•„๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

@Autowired

์ƒ์„ฑ์ž์—์„œ ์—ฌ๋Ÿฌ ์˜์กด๊ด€๊ณ„๋„ ํ•œ๋ฒˆ์— ์ฃผ์ž…๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

 

 

• @Component
: Bean ๊ฐ์ฒด๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์œผ๋ฉฐ(์ปดํฌ๋„ŒํŠธ ์Šค์บ”์— ์‚ฌ์šฉ), IoC์ปจํ…Œ์ด๋„ˆ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์ž๋™์œผ๋กœ ๊ฐ์ฒด ์ƒ์„ฑ๋˜๋ฉด Sigleton์ด๋‹ค. 

• @Controller
: ์Šคํ”„๋ง MVC ์ปจํŠธ๋กค๋Ÿฌ์— ์‚ฌ์šฉํ•˜๋ฉฐ, Dispatcher(HTTPํ”„๋กœํ† ์ฝœ๋กœ ๋“ค์–ด์˜จ ์š”์ฒญ์„ ๋ฐ›์•„ ์ ํ•ฉํ•œ ์ปจํŠธ๋กค๋Ÿฌ์—๊ฒŒ ์œ„์ž„)์€ @Controller๊ฐ€ ์žˆ๋Š” ํด๋ž˜์Šค๋“ค์„ ํ™•์ธํ•ด์„œ @RequestMapping์ด ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋“ค์„ ํ™•์ธํ•ฉํ•˜์—ฌ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.

• @Service
: ๋น„์ง€๋‹ˆ์Šค ๋กœ์ง์— ์žˆ์œผ๋ฉฐ ์ €์žฅ์†Œ ๊ณ„์ธต์„ ์‚ฌ์šฉํ•œ๋‹ค.

• @Repository
: ๋ฐ์ดํ„ฐ ์—‘์„ธ์Šค ๊ณ„์ธต์—์„œ ์‚ฌ์šฉํ•œ๋‹ค. ์ฃผ๋กœ ์ปดํฌ๋„ˆ๋Š” ์Šค์บ” ๋Œ€์ƒ์ด ๋˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜์ง€๋งŒ, ์Šคํ”„๋ง๋ถ€ํŠธ์™€ JPA๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์˜ˆ์™ธ ๋ณ€ํ™˜๊ธฐ๋ฅผ ์ž๋™์œผ๋กœ ๋“ฑ๋กํ•ด์„œ @Repository๋ฅผ ์ ์šฉํ•œ ๋นˆ์„ ํ”„๋ก์‹œ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.

• @Configuration
:  ์„ค์ •ํŒŒ์ผ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ, Bean์„ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•œ ์• ๋…ธํ…Œ์ด์…˜์ด๋‹ค. ์‹ฑ๊ธ€ํ†ค์„ ์œ ์ง€ํ•˜๋„๋ก ์ถ”๊ฐ€์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค.

 

- @Controller, @Service ์ถœ์ฒ˜

 

@Controller, @Service, @Repository ์ฐจ์ด

๊ฐœ์š” ์Šคํ”„๋ง์„ ์‚ฌ์šฉํ•˜๋ฉด์„œ ์•„๋งˆ ๊ฐ€์žฅ ์ฒ˜์Œ ์ ‘ํ–ˆ๋˜ ๊ฒƒ์ด @Controller, @Service, @Repository์˜€๋‹ค. ์ด ์–ด๋…ธํ…Œ์ด์…˜๋“ค์„ ๋ฌด์˜์‹์ ์œผ๋กœ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ, ๊ฐ ์ฐจ์ด์ ์€ ์žˆ์„๊นŒ? ์žˆ๋‹ค๋ฉด ์–ด๋–ค ๊ฒƒ์ผ๊นŒ? ์•Œ์•„๋ณด๋„๋ก ํ•ด๋ณด

escapefromcoding.tistory.com

 

 

- @Repository ์ถœ์ฒ˜

 

@Repository ๊ธฐ๋Šฅ

@Repository ๋Š” ๋ฐ์ดํ„ฐ ์•ก์„ธ์Šค ๊ณ„์ธต์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์• ๋„ˆํ…Œ์ด์…˜์ด๋‹ค. ๋ณดํ†ต์€ ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์˜ ๋Œ€์ƒ์ด ๋˜๊ธฐ ์œ„ํ•ด ์ ์šฉํ•˜์ง€๋งŒ ์Šคํ”„๋ง ๋ถ€ํŠธ์™€ JPA ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์˜ˆ์™ธ ๋ณ€ํ™˜๊ธฐ(PersistenceExceptionTranslationPostP

hoonsmemory.tistory.com

 

 

- @Configuration ์ถœ์ฒ˜

 

[Spring] @Configuration ๊ฐœ๋…๊ณผ ์žฅ์ 

๋”๋ณด๊ธฐ 1. @Configuration ์ด๋ž€? : @Configuration์ด๋ผ๊ณ  ํ•˜๋ฉด ์„ค์ •ํŒŒ์ผ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ์• ๋…ธํ…Œ์ด์…˜ or Bean์„ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•œ ์• ๋…ธํ…Œ์ด์…˜์ด๋‹ค. ๋”๋ณด๊ธฐ 2. @Configuration์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ญ๊ฐ€ ์ข‹์€๋ฐ? : @Configuration ์• 

castleone.tistory.com

 

 

 

 


 

 

 

 

โœ”๏ธ  AutoAppConfigTest

์ปดํฌ๋„ŒํŠธ ์Šค์บ”์ด ์ž˜ ์ž‘๋™๋˜๋Š”์ง€ ํ™•์ธ

 

 @Test
    void basicScan() {
        AnnotationConfigApplicationContext ac 
				 = new AnnotationConfigApplicationContext(AutoAppConfig.class);

 

 

AnnotationConfigApplicationContext๋Š” AutoAppConfig์˜ ์ธ์ž๋ฅผ ๋ฐ›์•„ ์Šคํ”„๋ง ์„ค์ • ํด๋ž˜์Šค๋กœ ์ธ์‹ํ•œ๋‹ค. AutoAppConfig์— @Configuration์„ ์ ์–ด๋†“์•˜์œผ๋‹ˆ ์ธ์‹์ด ๋˜๋Š” ๊ฒƒ์ด๋‹ค. AutoAppConfig์—๋Š” ์•„๊นŒ @Componentscan์— ๋Œ€ํ•œ ์ •๋ณด๋“ค์„ ์ ์–ด๋†“์•˜๋‹ค. ๊ทธ๋Ÿผ ๊ทธ๊ฑธ ๊ธฐ๋ฐ˜์œผ๋กœ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์Šค์บ”ํ•ด ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•˜๊ณ  ์˜์กด์„ฑ์„ ์ฃผ์ž…ํ•œ๋‹ค. ๋ชจ๋“  ์ค€๋น„๊ฐ€ ์™„๋ฃŒ๋˜๋ฉดAnnotationConfigApplicationContext๋Š” ์™„์ „ํžˆ ์ดˆ๊ธฐํ™”๋˜๋ฉฐ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค.

 

 

 

••• ์ด์–ด์„œ ๋‹ค๋ฅธ ์ฝ”๋“œ ๋ถ€๋ถ„ •••

 

 

        MemberService memberService = ac.getBean(MemberService.class);
        assertThat(memberService).isInstanceOf(MemberService.class);
    }

 

์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์—์„œ MemberService ํƒ€์ž…์˜ ๋นˆ์„ ์กฐํšŒํ•œ๋‹ค. ์กฐํšŒ๋œ ๋นˆ์ด MemberServiceํƒ€์ž…์ธ์ง€ ๊ฒ€์ฆํ•˜์—ฌ ์„ค์ •์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

 

 

assertThat(A).isInstanceOf(B);

: isInstanceOf() ๋Š” ํ•ด๋‹น ํƒ€์ž…์˜ ์ธ์Šคํ„ด์Šค์ธ์ง€๋ฅผ ๋น„๊ตํ•˜๋Š” ๋ฉ”์„œ๋“œ๋‹ค. 

 

 

 

 


 

 

 

โœ”๏ธ  MyExcludeComponent

์ปดํฌ๋„ŒํŠธ ์Šค์บ” ๋Œ€์ƒ์—์„œ ์ œ์™ธํ•  ์• ๋…ธํ…Œ์ด์…˜

 

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyExcludeComponent {

}

 

 

@interface 

: ์ž๋ฐ”์—์„œ ์ œ๊ณตํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜ ์™ธ์—, ์‚ฌ์šฉ์ž๊ฐ€ ์ปค์Šคํ…€ํ•  ์ˆ˜ ์žˆ๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ด๋‹ค. @interface๋Š” ํด๋ž˜์Šค๋‚˜ ๋ณ€์ˆ˜๋ฅผ ํŠน์ • ๊ฐ’์œผ๋กœ ์ง€์ •ํ•˜์—ฌ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

@Target

:  ์‚ฌ์šฉ์ž๊ฐ€ ๋งŒ๋“  annotation์ด ๋ถ€์ฐฉ๋  ์ˆ˜ ์žˆ๋Š” ํƒ€์ž…์„ ์ง€์ •ํ•œ๋‹ค. 

@Target({ElementType.TYPE})์€ ํด๋ž˜์Šค, ์ธํ„ฐํŽ˜์ด์„œ, Enum ๋˜๋Š” Record์— ๋ถ™์ผ ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

 

 

@Retention

: ํ•ด๋‹น ์–ด๋…ธํ…Œ์ด์…˜์„ ์–ธ์ œ๊นŒ์ง€ ์œ ์ง€ํ• ๊ฑด์ง€ ์ง€์ •ํ•œ๋‹ค.

@Retention(RetentionPolicy.RUNTIME)์€ Java๋กœ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๊ฐ€ ๋Œ์•„๊ฐ€๋Š” ๋™์•ˆ ์œ ์ง€ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

 

 

@Documented

:  ์–ด๋…ธํ…Œ์ด์…˜์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ javadoc์œผ๋กœ ์ž‘์„ฑํ•œ ๋ฌธ์„œ์— ํฌํ•จ๋˜๋„๋ก ํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ด๋‹ค.

 

Q. Docummented๋ฅผ ์จ์„œ ๋ฌธ์„œ์— ํฌํ•จ๋˜๋„๋กœ ํ•˜๋Š” ์ด์œ ๊ฐ€ ๋ฌด์—‡์ธ๊ฐ€?

A. ๋‹ค๋ฅธ ๊ฐœ๋ฐœ์ž๊ฐ€ ๊ฐœ๋ฐœ๋œ ์ฝ”๋“œ๋ฅผ ๋ณด์•˜์„ ๋•Œ ์–ด๋–ค ์ฝ”๋“œ์ธ์ง€ ์‰ฝ๊ฒŒ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค€๋‹ค. ์–ด๋…ธํ…Œ์ด์…˜์„ ์™œ ์ผ๋Š”์ง€ ๋“ฑ ์„ค๋ช…์„ ํฌํ•จ์‹œํ‚ฌ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— @Documented๋ฅผ ํ•จ๊ป˜ ์ ์–ด ๋‹ค๋ฅธ ๊ฐœ๋ฐœ์ž์˜ ์ดํ•ด๋ฅผ ๋•๋Š”๋‹ค. ํŠนํžˆ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‚˜ ํ”„๋ ˆ์ž„์›Œํฌ ๊ฐœ๋ฐœํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค.

 

 

 

- @Target @Retention ์ถœ์ฒ˜

 

[Spring] ์ปค์Šคํ…€ Annotation ๋งŒ๋“ค๊ธฐ

๋‚˜๋งŒ์˜ ์–ด๋…ธํ…Œ์ด์…˜์„ ๋งŒ๋“ค์–ด๋ณด์ž

velog.io

 

 

- @Docummented ์ถœ์ฒ˜

 

[Java] ์ž๋ฐ” ๋ฉ”ํƒ€ ์–ด๋…ธํ…Œ์ด์…˜ ์ •๋ฆฌ ๋ฐ ํ™œ์šฉ

๋ฉ”ํƒ€ ์–ด๋…ธํ…Œ์ด์…˜ (Meta Annotation) ๋ฉ”ํƒ€ ์–ด๋…ธํ…Œ์ด์…˜์€ ์–ด๋…ธํ…Œ์ด์…˜์— ๋ถ™์ด๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ด๋‹ค. ์ฆ‰, ์‚ฌ์šฉํ•  ์–ด๋…ธํ…Œ์ด์…˜์„ ์ •์˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•œ๋‹ค. ๋ฉ”ํƒ€ ์–ด๋…ธํ…Œ์ด์…˜์€ ์‚ฌ์šฉํ•  ์–ด๋…ธํ…Œ์ด์…˜์˜ ์ ์šฉ๋Œ€์ƒ ๋˜

ittrue.tistory.com

 

 

 

 


 

 

 

 

โœ”๏ธ  BeanB

์ปดํฌ๋„ŒํŠธ ์Šค์บ”์—์„œ ์ œ์™ธํ•  ํด๋ž˜์Šค

 

@MyExcludeComponent
public class BeanB {

}

 

์•„์ง๊นŒ์ง„ ์ œ์™ธ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋Š” ์–ด๋–ค ๊ธฐ๋Šฅ์ด ์‹คํ–‰๋œ ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. @MyExcludeComponent๋ฅผ ์ ์–ด์ฃผ์–ด ์•„๊นŒ ๋งŒ๋“ค์—ˆ๋˜ ์• ๋…ธํ…Œ์ด์…˜๊ณผ ์—ฐ๊ฒฐํ–ˆ๋‹ค.

 

 

Q. ์™œ ์• ๋…ธํ…Œ์ด์…˜์„ ๋งŒ๋“œ๋Š”๊ฐ€?

A. ์• ๋…ธํ…Œ์ด์…˜์„ ๋งŒ๋“œ๋Š” ์ด์œ ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์›ํ•˜๋Š”๋Œ€๋กœ ์„ค์ • ์ •๋ณด๋ฅผ ์ปค์Šคํ…€ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ผญ ์ปดํฌ๋„ˆํŠธ ์Šค์บ”์— ์–ด๋–ค ํด๋ž˜์Šค๋ฅผ ํฌํ•จํ•˜๊ณ  ์ œ์™ธ์‹œํ‚ค๊ธฐ ์œ„ํ•จ์€ ์•„๋‹ˆ๋‹ค. (์ œ์™ธ๋Š” ๋‹ค๋ฅธ ํด๋ž˜์Šค์—์„œ ๋ถ€์—ฌํ•จ) ์•„๊นŒ ๋Ÿฐํƒ€์ž„ ์‹œ๊ฐ„์„ ์ง€์ •ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์ด ๊ฐœ๋ฐœ์ž๊ฐ€ ํ•„์š”ํ•œ ์„ค์ •์ •๋ณด๋ฅผ ๋งŒ๋“ค์–ด ๋‹ค๋ฅธ ํด๋ž˜์Šค์— ํŽธ๋ฆฌํ•˜๊ฒŒ ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

 


 

 

 

 

โœ”๏ธ ComponentFilterAppConfigTest

 

BeanA beanA = ac.getBean("beanA", BeanA.class);
Assertions.assertThat(beanA).isNotNull();

 

getBean

: ac(์Šคํ”„๋ง ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ)์—์„œ beanA ์ด๋ฆ„์œผ๋กœ BeanA๋ผ๋Š” ํƒ€์ž…์œผ๋กœ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์„ ๋œปํ•œ๋‹ค.

 

 

Assertions.assertThat(A).isNotNull( )

: A๊ฐ€ Null์ธ์ง€ ํ™•์ธํ•œ๋‹ค.

 

 

- Assertions.assertThat().isNotNull() ์ถœ์ฒ˜

 

[Spring] Junit5 Assertions, assertThat()

์ด ๊ธ€์€ ๊ณต๋ถ€๋ฅผ ํ•˜๋ฉด์„œ ์•Œ๊ฒŒ ๋œ ๋‚ด์šฉ๋“ค์„ ๊ธฐ๋กํ•˜๋Š” ๊ธ€ ์ž…๋‹ˆ๋‹ค. ์˜ค๋ฅ˜๋‚˜ ๊ณ ์ณ์•ผ ํ•  ์‚ฌํ•ญ๋“ค์ด ์žˆ๋‹ค๋ฉด ์ง€์  ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค! โ›…๏ธ Assertions ์ด์ „ ํฌ์ŠคํŠธ์—์„œ Junit5๋ฅผ ํ†ตํ•ด ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋ฅผ ์ž‘์„ฑํ•ด๋ณด์•˜๋‹ค.

m42-orion.tistory.com

 

 

 

 

assertThrows(
        NoSuchBeanDefinitionException.class,
        () -> ac.getBean("beanB", BeanB.class));
}

 

 

NoSuchBeanDefinitionException

: BeanFactory์ •์˜๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†๋Š” ๋นˆ ์ธ์Šคํ„ด์Šค๋ฅผ ์š”์ฒญํ–ˆ์„ ๋•Œ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

 

NoSuchBeanDefinitionException.class, () -> ac.getBean("beanB", BeanB.class)์€ BeanB๋ฅผ beanB๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๊ฐ€์ง€๊ณ  ์™”๋Š”๋ฐ ๊ทธ๊ฒŒ ์ •์˜๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†๋Š” ๋นˆ ์ธ์Šคํ„ด์Šค์—ฌ์•ผ ํ…Œ์ŠคํŠธ๊ฐ€ ์„ฑ๊ณตํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

 

 

- NoSuchBeanDefinitionException  ์ถœ์ฒ˜

 

NoSuchBeanDefinitionException (Spring Framework 6.1.11 API)

Return the required type of the missing bean, if it was a lookup by type that failed.

docs.spring.io

 

 

 

••• ์ด์–ด์„œ ๋‹ค๋ฅธ ์ฝ”๋“œ ๋ถ€๋ถ„ •••

 

 

@ComponentScan(
            includeFilters = @Filter(classes = MyIncludeComponent.class),
            excludeFilters = @Filter(classes = MyExcludeComponent.class)
    )

 

 

@Filter

: ํ•„ํ„ฐ๋Š” ์ฃผ๋กœ ์š”์ฒญ์— ๋Œ€ํ•œ ์ธ์ฆ, ๊ถŒํ•œ ์ฒดํฌ ๋“ฑ์—  ์“ด๋‹ค. ๊ตฌ์ฒด์ ์œผ๋กœ ๋“ค์–ด์˜จ ์š”์ฒญ์ด DispatcherServlet์— ์ „๋‹ฌ๋˜๊ธฐ ์ „, ํ—ค๋”๋ฅผ ๊ฒ€์‚ฌํ•ด ์ธ์ฆ ํ† ํฐ์ด ์žˆ๋Š”์ง€ ์—†๋Š”์ง€, ์˜ฌ๋ฐ”๋ฅธ์ง€, ์˜ณ์ง€ ์•Š์€์ง€ ๋“ฑ์„ ๊ฒ€์‚ฌํ•œ๋‹ค.

 

 

- Filter ์ถœ์ฒ˜

 

spring - ์Šคํ”„๋ง์—์„œ์˜ ํ•„ํ„ฐ ๊ฐœ๋… ๋ฐ ์˜ˆ์ œ

๋ธ”๋กœ๊ทธ์—์„œ ์‚ฌ์šฉํ•œ ์†Œ์Šค์ฝ”๋“œ๋Š” https://github.com/97e57e/BLOG ์—์„œ ๋ณด์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Filter ๋ž€? ์‚ฌ์‹ค ํ•„ํ„ฐ๋Š” ์Šคํ”„๋ง์˜ ๋…์ž์ ์ธ ๊ธฐ๋Šฅ์ด ์•„๋‹Œ ์ž๋ฐ” ์„œ๋ธ”๋ฆฟ์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ

gardeny.tistory.com

 

 

 

excludeFilters

:  ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ๋Œ€์ƒ์„ ์Šค์บ”์—์„œ ์ œ์™ธ์‹œํ‚จ๋‹ค.

 

 

Q. ๊ทธ๋Ÿผ ๋‹ค์‹œ ํฌํ•จ์‹œํ‚ค๊ณ  ์‹ถ์œผ๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ•˜๋‚˜?

A. includeFilters = @Filter(classes = MyExcludeComponent.class)๋กœ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋œ๋‹ค.

 

- excludeFilters ์ถœ์ฒ˜

 

component scan

@Component ํด๋ž˜์Šค ์„ ์–ธ๋ถ€ ์œ„ ์ชฝ์— @Component ์–ด๋…ธํ…Œ์ด์…˜์„ ๋ถ™์ด๋ฉด @Configuration ํด๋ž˜์Šค์— ๋ณ„๋„๋กœ Bean ๋“ฑ๋ก ๊ณผ์ •์„ ๊ฑฐ์น˜์ง€ ์•Š์•„๋„ ์Šคํ”„๋ง์ด ํŒจํ‚ค์ง€ ์ „์ฒด๋ฅผ ํƒ์ƒ‰ํ•ด์„œ Bean์œผ๋กœ ๋“ฑ๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค.

hyeon9mak.github.io

 

 

@ComponentScan์„ ํ†ตํ•ด ์†์„ฑ์„ ์‚ดํŽด๋ณธ๋‹ค. ์†์„ฑ์—” MyExcludeComponent๋ฅผ excludeFiltersํ•˜๋ผ๊ณ  ์ ํ˜€์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด MyExcludeComponent๋ฅผ ์ œ์™ธ์‹œํ‚ค๋„๋ก ๋˜์–ด์žˆ์œผ๋‹ˆ @MyExcludeComponent๊ฐ€ ๋ถ™์–ด์žˆ์œผ๋ฉด ์ œ์™ธ์‹œํ‚ค๋„๋ก ํ•œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ BeanB์—๋Š” @MyExcludeComponent๊ฐ€ ๋ถ™์–ด์žˆ์œผ๋‹ˆ ์ œ์™ธ๊ฐ€ ๋˜์—ˆ๋‹ค.

 

 

 

 


 

๐ŸŒท

 

์ปดํฌ๋„ŒํŠธ ์Šค์บ”์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ @ComponentScan์„ ์“ฐ๊ณ  { } ์•ˆ์— ์›ํ•˜๋Š” ํŠน์„ฑ์„ ๋„ฃ์„ ์ˆ˜ ์žˆ์Œ์„ ์•Œ์•„๋ดค๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์— ๋Œ€ํ•œ ์š”๊ตฌ๋ฅผ ์„ค์ •ํ•˜๊ณ  ์‹ถ์€ ๊ฒจ์šฐ @interface์™€ ํด๋ž˜์Šค๋ฅผ ์—ฐ๊ฒฐ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

+ Recent posts