์ปจํ ์ด๋๋ฅผ ์์ฑํ๋ค. ์คํ๋ง ์ปจํ ์ด๋ ๋ด์์ ๋น ์ด๋ฆ๊ณผ ๋น ๊ฐ์ฒด๋ฅผ ์ ์ฅํ์ฌ ๊ด๋ฆฌํด์ค๋ค. Public๊ณผ ํจ๊ป ์ด ๊ฒ์ ํ์ ๊ณผ ์ด๋ฆ์ด ๋๋ค. ์์๋ก public MemberService memberService( )์ ๊ฒฝ์ฐ MemberService๋ ํ์ ์ด ๋๊ณ memberService๋ ๋น์ ์ด๋ฆ์ด ๋๋ค. return new์ ํจ๊ป ์ฐ์ด๋ฉด ๋น ๊ฐ์ฒด๊ณผ ๋๋ค. ์๋ก return MemberServiceImpl(memberRepository( ))์ ๊ฒฝ์ฐ MemberServiceImpl์ด ๋น ๊ฐ์ฒด๊ฐ ๋๋ ๊ฒ์ด๋ค. ๊ทธ๋ ๊ฒ ์คํ๋ง ์ปจํ ์ด๋์ ์ ์ฅ๋์ด ๊ด๋ฆฌ๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ด๋ฒ์ ๋น์ด ์ ๋๋ก ๋ฑ๋ก๋์ด์๋์ง ํ์ธํ๋ ๋ฐฉ๋ฒ์ ๋ฐฐ์ธ ๊ฒ์ด๋ค.
[ โ๏ธ ]
[Spring] ๊น์ํ ๊ธฐ๋ณธํธ_ ์น์ 4. ๊ฐ์ฒด์งํฅ์๋ฆฌ์ ์ฉ
์น์ 3. ์์ ๋ง๋ค๊ธฐ์์ ์ ๊น ์ธ๊ธํ์ง๋ง ๊ตฌํ์ฒด์ ์์กด์ฑ์ ์์ ํ ๋ฐฐ์ ํ ์ฝ๋๊ฐ ์๋์๋ค. ์ธํฐํ์ด์ค์ ์ฐ๊ฒฐ๋ ๊ตฌํ์ฒด๋ฅผ ๋ณ๊ฒฝํ๋ ค๋ฉด ํ๋ํ๋ ํด๋์ค๋ฅผ ์ฐพ์๊ฐ ์์ ํด์ผํ๋ ์ผ์ด ๋ฐ์ํ
post-this.tistory.com
๐งธ
์๋ชป๋ ์ ๋ณด๊ฐ ์๋ค๋ฉด ๋๊ธ๋ก ๋จ๊ฒจ์ฃผ์ธ์
๐งธ ๊ตฌ์กฐํ์ ๋ ์ธ ๊ผฌ์ฐ- ๐งธ
test์ ์ด 3๊ฐ์ง์ ํจํค์ง๋ฅผ ๋ง๋ค์๋ค.
- beanfind
ใด ApplicationContextInfoTest, ApplicationContextBasicFindTest, ApplicationnContextExtendsFindTest
, ApplicationContextSameBeanFindTest - beandefinition
ใด BeanDefinitionTest - xml
ใด XmlAppContext
โ๏ธ beanfind_ApplicationContextInfoTest
โบ findAllBean
: ๋ด๋ถ์ ํฌํจ๋ Bean์ ์ถ๋ ฅํ๋ค.
AnnotationConfigApplicationContext ac
= new AnnotationConfigApplicationContext(AppConfig.class);
AnnotationConfigApplicationContext
: Bean์ผ๋ก ์ง์ ๋ ๊ฐ์ฒด๋ค์ ๊ฐ์ง๊ณ ์๋ context๊ณ ์ปจํ ์ด๋๋ผ๊ณ ๋ ๋ถ๋ฅธ๋ค.
ApplicationContext๋ ์ธํฐํ์ด์ค๋ก ๋ค์ํ ๊ตฌํ์ฒด๋ฅผ ๊ฐ์ง๊ณ ์๋ค. ๊ทธ ์ค ํ๋๊ฐ ์ด๋ ธํ ์ด์ ์ ์ด์ฉํ ์ ์๋๋ก ํด์ฃผ๋ AnnotationConfigApplicationContext์ด๋ฉฐ ํ์ฌ ๊ฐ์์์ ๋ค๋ฃจ๋ ๊ฒ์ด๋ค. ์น์ 4 ๊ฐ์์์ AppConfig์ @Configuration๊ณผ @Bean ์ด๋ ธํ ์ด์ ์ ์ ์ํ๋ค. ์ด๋ ๊ฒ ์ด๋ ธํ ์ด์ ์ ์ ์๋ฅผ ํตํด ๋น์ ์๋กญ๊ฒ ์์ฑํ๊ฑฐ๋ ๊ด๋ฆฌํ๊ณ , ์ปดํฌ๋ํธ ์ค์บ ํน์ ํ๋กํ์ผ ๊ด๋ฆฌ๋ฅผ ํ ์ ์๊ฒ ๋๋ค.
ใ ค- ApplicationContext ์ค๋ช ์ถ์ฒ
[Spring] ์ ํ๋ฆฌ์ผ์ด์ ์ปจํ ์คํธ(Application Context)์ ์คํ๋ง์ ์ฑ๊ธํค(Singleton)
์ด๋ฒ์๋ ์ ํ๋ฆฌ์ผ์ด์ ์ปจํ ์คํธ์ ๋ํด ๊ฐ๋จํ ์์๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค. 1. ์ ํ๋ฆฌ์ผ์ด์ ์ปจํ ์คํธ(Application Context) [ ์ ํ๋ฆฌ์ผ์ด์ ์ปจํ ์คํธ(Application Context)๋? ] Spring์์๋ ๋น์ ์์ฑ๊ณผ ๊ด๊ณ
mangkyu.tistory.com
- AnnotationConfigApplicationContext ์ถ์ฒ
Bean ๊ฐ์ฒด๋ค์ ๋ง๋จ์ ๊ด์ฅ : AnnotationConfigApplicationContext
AnnotationConfigApplicationContext AnnotationConfigApplicationContext ์ Bean์ผ๋ก ์ง์ ๋ ๊ฐ์ฒด๋ค์ ๊ฐ์ง๊ณ ์๋ context ์ ๋๋ค. ์ปจํ ์ด๋๋ผ๊ณ ๋ ๋ถ๋ฆ ๋๋ค. AnnotationConfigApplicationContext ์ ํตํด ํ์ฌ ์ด๋ค Bean ๊ฐ์ฒด๋ค
jindevelopetravel0919.tistory.com
- AnnotationConfigApplication ์ถ์ฒ
[Spring] AnnotationConfigApplicationContext์ ์ญํ ๊ณผ ํ์ฉ ๋ฐฉ๋ฒ
Spring ํ๋ ์์ํฌ์์๋ ApplicationContext ์ธํฐํ์ด์ค์ ๋ค์ํ ๊ตฌํ์ฒด ์ค ํ๋๋ก AnnotationConfigApplicationContext๋ฅผ ์ ๊ณตํฉ๋๋ค. AnnotationConfigApplicationContext๋ Java ๊ธฐ๋ฐ์ ์ค์ ํด๋์ค๋ฅผ ์ฌ์ฉํ์ฌ ๋น(Bean)
jjangadadcodingdiary.tistory.com
••• ์ด์ด์ ๋ค์ ์ฝ๋ •••
String[] beanDefinitionNames = ac.getBeanDefinitionNames();
getBeanDefinitionNames( )
: ํฉํ ๋ฆฌ์ ๋ฑ๋ก๋ ๋ชจ๋ bean์ ๋ฐํํ๊ณ @bean์ ์ ์ธํ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ผ๋ก ๋ฑ๋ก๋ ์ฑ๊ธํค Bean์ ๋ฌด์ํ๋ค. ๋ํ ๊ณ์ธต๊ตฌ์กฐ๋ ๋ฌด์ํ๋ค.
ac๋ AppConfig์ ๋ฑ๋ก๋ bean์ ๋ํ ์ค์ ์ ๋ณด๊ฐ ๋ด๊ฒจ์๋ค. ํ์ง๋ง getBeanDefinitionNames๋ ๋ด๋ถ์ ๋ชจ๋ bean์ ๋ฐํํ๋ค๊ณ ์ ์๋์ด์๋๋ฐ, ๊ทธ ์ด์ ๋ฅผ ์ดํด๋ณด๋ฉด AppConfig์ ์ค์ ์ ์ ์ฉํ๋ฉด์ Spring์ ๊ธฐ๋ณธ์ ์ผ๋ก ํ์๋กํ๋ ๋ด๋ถ ๋น๋ค๋ ์๋์ ์ผ๋ก ํจ๊ป ๋ฑ๋ก๋๊ธฐ ๋๋ฌธ์ด๋ค. ๊ทธ๋ ๊ธฐ์ ๋ด๋ถ ๋น๋ค๋ ๋ชจ๋ ๋ณด๊ณ ์ถ์ ๊ฒฝ์ฐ getBeanDefinitionNames()๋ฅผ ์ฌ์ฉํ๋ค.
- getBeanDefinitionNames ์ ์ ์ถ์ฒ
ListableBeanFactory (Spring Framework 6.1.11 API)
Extension of the BeanFactory interface to be implemented by bean factories that can enumerate all their bean instances, rather than attempting bean lookup by name one by one as requested by clients. BeanFactory implementations that preload all their bean d
docs.spring.io
ใ คใ ค••• ๊ทธ ๋ค์ ์ฝ๋ •••
for (String beanDefinitionName : beanDefinitionNames) {
Object bean = ac.getBean(beanDefinitionName);
System.out.println ("name = " + beanDefinitionName + "object = " + bean); }
getBean( );
: ํ์ฌ ์ฌ์ฉ ์ค์ธ bean์ ๋ฐํํ๋ค.
Object
: ์ต์์ ํด๋์ค๋ก ์๋ฐ ํด๋์ค์ ๋ฐ๋ก extendsํ์ง ์์ผ๋ฉด java.lang.Object๋ฅผ ์์ํ๋ค. ํ๋๊ฐ ์๋ ๋ฉ์๋๋ก ๊ตฌ์ฑ๋์ด์์ผ๋ฉฐ ๋ชจ๋ ํ์ ์ ๋ค ๋ฐ์ ์ ์๋ค.
beanDefinitionNames๋ ๋ชจ๋ ๋น์ ๋ฐํ ๋ฐ์์จ ๋ฌธ์์ด์ด๋ค. for๋ฌธ์ ์ด์ฉํ์ฌ beanDefinitionNames์ ๋ค์ด์๋ ๋น ์ด๋ฆ์ beanDefinitionName ๋ณ์์ ์ ์ฅํ๋ค. ๊ทธ๋ฆฌ๊ณ ac.getBean์ ์ด์ฉํ์ฌ ๋น ๊ฐ์ฒด๋ฅผ ์กฐํํ๋ฉฐ ์ด๋ bean์ ์ ์ฅ๋๋ ํ์ ์ Object๋ก ๋ชจ๋ ํ์ ์ ๋ค ๋ฐ์ ์ ์๊ฒ ๋๋ค. ๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ์คํ๋ง ๋ด๋ถ์ ์๋ ๋น๊น์ง ๋ชจ๋ ์ถ๋ ฅํ ์ ์๋ ๊ฒ์ด๋ค.
์๋ <๊ฒฐ๊ณผ>๋ฅผ ๋ณด๋ฉด name์ ๋น์ ์ด๋ฆ์ด ์ถ๋ ฅ๋์๊ณ object๋ ์ง์ ๋ ํด๋์ค๊ฐ ๋ฌด์์ธ์ง ์ถ๋ ฅ๋์๋ค. ์ฆ discountPolicy๋ hello.core.discount.RateDiscountPolicy์ ์ฐ๊ฒฐ๋์ด์์์ ๋ณด์ฌ์ฃผ๋ ๊ฒ์ด๋ค.
<๊ฒฐ๊ณผ> name = discountPolicy object = hello.core.discount.RateDiscountPolicy@246f8b8b ์๋ด์์ผ๋ก ์ถ๋ ฅ๋๋ค.๐ป
Q.๊ผญ beanDefinitionName ๋ณ์๋ก ๋ฐ์์ค์ผํ๋๊ฑด๊ฐ?
A. ๊ผญ ๊ทธ๋ดํ์๋ ์๋ค. ๊ฐ๋ ์ฑ์ ์ํด ๋ณ์๋ฅผ ์ฌ์ฉํ๋ ๊ฒ ๋ฟ.
String[] beanDefinitionNames = ac.getBeanDefinitionNames(); for (int i = 0; i < beanDefinitionNames.length; i++) { String beanName = beanDefinitionNames[i]; System.out.println("Bean Name " + (i + 1) + ": " + beanName); }โ
์ด๋ฐ์์ผ๋ก ๊ทธ๋ฅ ๋ณ์์์ด ๋ฐ์๋ ๋๋ค๊ณ ํ๋ค. ๊ทธ๋ฆฌ๊ณ ๋ฐ์์จ ๊ฑธ beanName์ ์ ์ฅํ์ฌ ์ถ๋ ฅํ๋ค.
Q.ac.getBean์์ ac๋ ๊ผญ ๋ถ์ฌํ๋๊ฑธ๊น?
A. getBean์ApplicationContext์์ ์ ๊ณตํ๋ ์ธํฐํ์ด์ค ๋ฉ์๋์ด๋ค.๊ทธ๋ ๊ธฐ์ ๊ด๋ จ ApplicationContext์์ ์ ๊ณตํ๋ ๋ฉ์๋๋ฅผ ์ฐ๋ ค๋ฉด ๋ด๊ฐ ์ง์ ํ ๋ณ์(ac)๋ฅผ ๊ผญ ๋ถ์ฌ์จ์ผ ํ๋ค.
BeanMap (Spring Framework 6.1.11 API)
Limit the properties reflected in the key set of the map to readable properties.
docs.spring.io
2. Java ์๋ฐ [API] - Object ํด๋์ค, Object ํด๋์ค์ ๋ฉ์๋ 1
์๋ฐ์ ํด๋์ค๋ฅผ ์ ์ธํ ๋ extends ํค์๋๋ก ๋ค๋ฅธ ํด๋์ค๋ฅผ ์์ํ์ง ์์ผ๋ฉด ์์์ ์ผ๋ก java.lang.Object ํด๋์ค๋ฅผ ์์ํ๊ฒ ๋๋ค. ๋ฐ๋ผ์ ์๋ฐ์ ๋ชจ๋ ํด๋์ค๋ Object ํด๋์ค์ ์์์ด๊ฑฐ๋ ์์ ํด
kephilab.tistory.com
โบ findApplicationBean
: ๋ด๊ฐ ๊ฐ๋ฐํ bean ์ถ๋ ฅํ๊ธฐ.
for (String beanDefinitionName : beanDefinitionNames) {
BeanDefinition beanDefinition = ac.getBeanDefinition(beanDefinitionName);
๋ชจ๋ ๋น์ getBeanDefinitionNames๋ก ๊ฐ์ ธ์๋ค. ๊ทธ๋ฆฌ๊ณ for์ ํตํด ๋ชจ๋ ๋น์ ๋ฉํ์ ๋ณด๋ฅผ getBeanDefinition์ผ๋ก beanDefinition์ ์ ์ฅํ ๊ฒ์ด๋ค.
getBeanDefinition( );
: ํน์ bean์ ๋ฉํ์ ๋ณด๋ฅผ ๊ฐ์ ธ์จ๋ค.
BeanDefinition
: ์คํ๋ง ํ๋ ์์ํฌ์์ ์ ์ํ ์ธํฐํ์ด์ค๋ก, ์คํ๋ง ์ปจํ ์ด๋์์ ๊ด๋ฆฌํ๋ ๊ฐ bean์ ๋ฉํ์ ๋ณด๋ฅผ ๋ํ๋ด๋ ์ญํ ์ ํ๋ค.
Q. ์์ ์ฝ๋ ์ค getBean์์๋ Object๋ผ๋ ๊ฐ์ฒด ํ์ ์ ์ ์๋๋ฐ ์ฌ๊ธฐ์๋ ์ BeanDefinition์ ์ ์๊ฑธ๊น?
getBean๋ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๋ ์ฝ๋์ธ๊ฑฐ ์๋๊ฐ? ํ์ฌ ์ฌ์ฉ์ค์ธ bean์ ๋ฐํํ๋ ๊ฒ์ด๋. ๊ทธ๋ฐ๋ฐ ์ ๋ค๋ฅด๊ฒ ์ฐ๋๊ฑธ๊น?
A. getBean()๊ณผ getBeanDefinition( )์ ์ปจํ ์ด๋๋ฅผ ๊ฐ์ ธ์ค๋ ๋ฐฉ์์ด ๋ค๋ฅด๋ค. getBean()์ ์คํ๋ง ์ปจํ ์ด๋์์ ๋น์ ์ค์ ์ธ์คํด์ค๋ฅผ ๊ฐ์ ธ์ค๊ณ (์ฝ๊ฒ ๋งํด์ bean๊ณผ ์ฐ๊ฒฐ๋ ํด๋์ค) Object ํ์ ์ผ๋ก ๋ฐํํ๋ค. ๋ฐ๋ฉด์ getBeanDefinition()์ ํน์ ๋น์ ๋ฉํ์ ๋ณด๋ฅผ ๊ฐ์ ธ์จ๋ค. ๊ทธ๋ฆฌ๊ณ ๊ฐ์ ธ์จ ๋ฉํ์ ๋ณด๋ฅผ ์ ์ํด์ฃผ๋ ์ธํฐํ์ด์ค๊ฐ BeanDefinition์ด ๋๊ธฐ์ ๋น์ ์ด๋ฆ์ด ํธ์ถ๋๋ฉด BeanDefinition ๊ฐ์ฒด๋ก ๋ฐํํ๋ ๊ฒ์ด๋ค. ์ฆ Object์ BeanDefinition์ ์จ์ผํ๋ ๊ฒฝ์ฐ๊ฐ ์์ ํ ๋ค๋ฅด๋ฉฐ BeanDefinition์ ํ๋์ ๊ตฌ์ฒด์ ์ธ ํ์ ์ด ์๋๋ค.
Q. getBean( ) ๋ฉ์๋๋ ์ผ๋ฐ์ ์ผ๋ก Object๋ก ๋ฐ๋๋ค๊ณ ํ๋ค. ๊ทธ๋ ๋ค๋ฉด ์ ์ผ๋ฐ์ ์ผ๋ก Object๋ก ๋ฐ๋๊ฐ?
A. ๋น์ ์ค์ ํ์ ์ด ๋ฌด์์ด๋ ์ง ์๊ด์์ด ์ฒ๋ฆฌํ ์ ์๊ฒ ํ๊ธฐ ์ํด Object๋ก ๋ฐ๋๋ค. (์ ์ฐ์ฑ๊ณผ ์ผ๊ด์ฑ์ ๊ฐ๊ฒ ๋๋ค)
ใ คใ ค•••๊ทธ ๋ค์ ์ฝ๋ •••
if (beanDefinition.getRole() == BeanDefinition.ROLE_APPLICATION) {
Object bean = ac.getBean(beanDefinitionName);
System.out.println("name = " + beanDefinitionName
+ "object = " + bean); }
getRole
: ํด๋น ์ญํ ์ ๋ฐํํ๋ค. ์ ์๊ฐ์ผ๋ก ๋ฐํํ๋ค.
ROLE_APPLICATION
: ์ฌ์ฉ์๊ฐ ์ ์ํ(๋ด๊ฐ ๋ง๋ ) ๋น์ด๋ค. ์ ์๊ฐ์ 2
- ROLE_SUPPORT : ์คํ๋ง ๋ด๋ถ์์ ์ง์ํ๋ ์ญํ ์ ๊ฐ์ง ๋น์ด๋ค. ์ ์๊ฐ์ 4
- ROLE_INFRASTRUCTURE : ์คํ๋ง์ ๋ด๋ถ ๊ตฌ์กฐ๋ฅผ ์ง์ํ๋ ๋น์ด๋ค. ์ ์๊ฐ์ 8
for๋ฌธ์ผ๋ก ๋ชจ๋ ๋น์ ๋ฉํ์ ๋ณด๋ฅผ ๊ฐ์ ธ์๊ณ (beanDefinition), ๊ทธ ๋ฉํ์ ๋ณด๋ฅผ if๋ฌธ์ ํตํด ์ฌ์ฉ์๊ฐ ์ ์ํ ๋น์ธ์ง ์๋์ง ๊ตฌ๋ถํ ๊ฒ์ด๋ค. beanDefinition์ ์๋ ์ ๋ณด๋ฅผ getRole์ ํตํด ์ญํ ์ด ๋ฌด์์ธ์ง ํ์ธํ๋ค. ๊ทธ ์ญํ ์ด ROLE_APPLICATION์ธ์ง ํ์ธํ์ฌ ๋ง์ผ๋ฉด ๊ดํธ ์์ ์๋ ์ฝ๋๊ฐ ์คํ๋๋๋ก ํ๋ค. ์ฌ๊ธฐ์BeanDefinition.ROLE_APPLICATION์ผ๋ก ์ด ์ด์ ๋ ROLE_APPLICATION์ด BeanDefinition ํด๋์ค ์์ ์ ์ธ๋์ด์๊ธฐ ๋๋ฌธ์ ์ด๋ฐ ํํ๋ก ์ ์ด์ผ ์ ๋๋ก ์๋ํ๋ค.
beanDefinitionName(๋ชจ๋ ๋น์ ๋ฐํ๋์ด ์ ์ฅํ ๋ณ์)์์ ์กฐ๊ฑด์ ๋ง๋ ๋น์ ๊บผ๋ด Object ํ์ ์ธ bean์ ์ ์ฅํ๋ค.
Q. ๋ฌด์กฐ๊ฑด getBean( )๊ณผ Object๋ฅผ ํตํด์๋ง ๋ฐ์์ผ ๊ฒฐ๊ณผ๊ฐ ์ถ๋ ฅ๋๋๊ฐ?
ใ ค
A. ๊ทธ๋ ๋ค. ์ค์ ๋น์ ์ธ์คํด์ค๋ฅผ ์ป์ด์์ผ๋ง ๊ฒฐ๊ณผ๊ฐ์ผ๋ก ์ถ๋ ฅํ ์ ์๋ค๊ณ ํ๋ค. getRole( )์ ํด๋น ์ญํ ์ ๋ฐํํ๋ ๊ฒ ๋ฟ, ์ธ์คํด์ค๋ฅผ ๋ฐํํ ์ ์๋ค. ๊ทธ๋ ๊ธฐ๋๋ฌธ์ ์ค์ ์ธ์คํด์ค๋ฅผ ๋ฐํํด์ฃผ๋ getBean( )์ ์ฌ์ฉํด์ผ๋ง ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ์ ์๋ค. getBean(ํ์ ) ํน์ getBean(๋น์ด๋ฆ, ํ์ )์ ์ ์ผ๋ฉด ์กฐํ๊ฐ ๋๋ค.
BeanDefinition (Spring Framework 6.1.11 API)
Return the current bean class name of this bean definition. Note that this does not have to be the actual class name used at runtime, in case of a child definition overriding/inheriting the class name from its parent. Also, this may just be the class that
docs.spring.io
BeanDefinition (Spring Framework 6.1.11 API)
Return the current bean class name of this bean definition. Note that this does not have to be the actual class name used at runtime, in case of a child definition overriding/inheriting the class name from its parent. Also, this may just be the class that
docs.spring.io
โ๏ธ beanfind_ApplicationContextBasicFindTest
โบ findBeanByName
: ๋น ์ด๋ฆ์ผ๋ก ์กฐํํ๊ธฐ.
MemberService memberService = ac.getBean("memberService", MemberService.class);
assertThat(memberService).isInstanceOf(MemberServiceImpl.class);
assertThat( ).isInstanceOf( )
: ํด๋น ํ์ ์ ์ธ์คํด์ค์ธ์ง๋ฅผ ๋น๊ตํ๋ ๋ฉ์๋์ด๋ค.
bean ์ด๋ฆ ์ค์์ memberService์ด๋ฉฐ ํด๋น ํ์ ์ MemberService๋ก ์ง์ ํ ๋น์ MemberService(์ธํฐํ์ด์ค ํ์ ์ผ๋ก)๊ฐ์ง๋ฉฐ ๋ณ์ ์ด๋ฆ์ memberService์ผ๋ก ๋ฐ๊ฒ ๋ค๋ ๊ฒ์ด๋ค. ๊ทธ๋ฆฌ๊ณ ๊ทธ MemberServiceimpl(๊ตฌ์ฒดํด๋์ค)์ด memberService์ ์ธ์คํด์ค์ธ์ง ๋น๊ตํ๋ค.
์ข ๋ ์ฝ๊ฒ ์ค๋ช ํด๋ณด์๋ฉด, AppConfig์ ์ธํฐํ์ด์ค์ ๋ฉ์๋ ๋ช ์ ๊ฐ์ด ์ ์๊ณ return ๊ฐ์ผ๋ก ๊ตฌ์ฒดํด๋์ค๋ฅผ ์ ์๋ค. ์ฌ๊ธฐ์ ํ์ธํ๋ ๊ฒ์ AppConfig์ ์ ์ ์ธํฐํ์ด์ค์ ๊ตฌ์ฒดํด๋์ค๊ฐ ๋ง๋์ง ํ์ธํ๋ ๊ฒ์ด๊ณ "memberService"๋ฅผ ์ ์ด ์ด๋ฆ์ผ๋ก ์กฐํํ๋ ๋ฐฉ๋ฒ์ด๋ค.
โบ findBeanByNameX
: ๋น ์ด๋ฆ์ผ๋ก ์กฐํํ์ง ์๊ธฐ.
assertThrows(NoSuchBeanDefinitionException.class,
() -> ac.getBean("XXXX", MemberService.class));
assertThrows
: JUnit ๋๋ AssertJ์ ๊ฐ์ ํ ์คํธ ํ๋ ์์ํฌ์์ ์์ธ๊ฐ ๋ฐ์ํ๋์ง๋ฅผ ํ ์คํธํ ๋ ์ฌ์ฉํ๋ค. ๋ฐ์๋ ์์ธ ํ์ ๊ณผ ์์๋๋ ์์ธ ํ์ ์ด ์ผ์นํ๋ฉด ํ ์คํธ๋ ์ฑ๊ณตํ๋ค.
NoSuchBeanDefinitionException.class
: ์ ์๋ฅผ ์ฐพ์ ์ ์๋ Bean ์ธ์คํด์ค๋ฅผ ์์ฒญํ์ ๋ ์์ธ๊ฐ ๋ฐ์ํ๋ค.
() -> ๋๋ค์
: (๋งค๊ฐ๋ณ์)๊ฐ ๋ค์ด๊ฐ๊ณ -> ์์๋ถ๋ถ์ ๊ฐ๋ฆฌํจ๋ค.
-> (์์๋ถ๋ถ)์ผ๋ก ๊ฐ์ XXXX๊ฐ MemberService.class์ ํด๋น ๋น์ผ๋ก ์กด์ฌํ์ง ์์ผ๋ฉด NoSuchBeanDefinitionException ์์ธ๋ฅผ ๋ฐ์์ํจ๋ค.
[Junit] Junit5 Exception Throw ํ ์คํธ
์ด ๊ธ์ ํผ์ ํ์ตํ ๋ด์ฉ์ ๋ฐํ์ผ๋ก ์์ฑ๋์์ต๋๋ค. ํ๋ฆฌ๊ฑฐ๋ ์๋ชป๋ ์ ๋ณด๊ฐ ์์ ์ ์์ต๋๋ค. ๋๊ธ๋ก ์๋ ค์ฃผ์๋ฉด ์์ ํ๋๋ก ํ๊ฒ ์ต๋๋ค. 1. Junit4 Exception ํ ์คํธ ๋ง์ ๊ฐ๋ฐ์ ๋ถ๋ค์ด ์ฌ์ฉ
small-stap.tistory.com
- 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
[Java/์๋ฐ] ๋๋ค์(Lambda)์ด๋? ๊ทธ๋ฆฌ๊ณ ์ฌ์ฉ๋ฒ
๋๋ค์์ด๋ ? ๋๋ค์์ด๋ ์ฝ๊ฒ ๋งํด ๋ฉ์๋๋ฅผ "ํ๋์ ์"์ผ๋ก ํํํ ๊ฒ์ ๋๋ค. ํ๋์ ์์ผ๋ก ํํํ์ฌ ํจ์ฌ ๊ฐ๋ตํ๊ฒ ํํ์ด ๊ฐ๋ฅํ๊ฒ ๋๋ฉฐ, ๋ฉ์๋์ ์ด๋ฆ๊ณผ ๋ฐํ๊ฐ์ด ์์ด์ง๋ฏ๋ก "์ต๋ช ํจ์
hstory0208.tistory.com
โ๏ธ beanfind_ApplicationContextSameBeanFindTest
@Configuration
static class SameBeanConfig {
@Bean
public MemberRepository memberRepository1() {
return new MemoryMemberRepository();
}
@Bean
public MemberRepository memberRepository2() {
return new MemoryMemberRepository();
}
}
AppConfig๋ฅผ ์์ ํ๊ธฐ ์ซ์ผ๋, ์ด๋ ๊ฒ ๊ฐ์ ํ์ ์ ๊ฐ์ ๊ตฌ์ฒดํด๋์ค๋ฅผ ๋ฐํํ๋ ๋๊ฐ์ bean์ ๋ง๋ค์๋ค.
โบ findAllBeanByType
Map<String, MemberRepository> beansOfType =
ac.getBeansOfType(MemberRepository.class);
getBeansOfType
: ํ์ ์ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ๋๋ค. Bean ์ด๋ฆ์ key๋ก ๊ฐ์ฒด๋ฅผ value๋ก ํ๋ Map์ ๋ฐํํ๋ค. ์ฃผ๋ก ํน์ ํ์ ์ ์ฌ๋ฌ ๊ฐ์ ๋น์ด ์์ ๊ฒฝ์ฐ ์ค๋ฅ๋ฅผ ๋ฐ์์ํค์ง ์๊ธฐ ์ํด ์ฌ์ฉํ๋ค.
MemberRepository ํ์ ์ ์๋ ๋น์ getBeansOfType ๋ฉ์๋๋ฅผ ํตํด key์ value๋ฅผ ๋ฐํํ๋ค. ๊ทธ๋ฌ๋ฉด String์ bean์ ์ด๋ฆ์ด ๋ค์ด๊ฐ๊ณ MemberRepository์ ์ค์ bean์ ๊ฐ์ฒด๊ฐ ๋ค์ด๊ฐ๊ฒ ๋๋ค. ๊ทธ๋ ๊ฒ MemberRepository ํ์ ์ bean์ด ์ฌ๋ฌ ๊ฐ์ธ ๊ฒฝ์ฐ BeansOfType ๋ณ์์ ๋ชจ๋ ๋ด์ ์ ์๊ฒ ๋๋ค.
ใ คใ ค•••๊ทธ ๋ค์ ์ฝ๋ •••
for (String key : beansOfType.keySet()) {
System.out.println("key = " + key + " value = " +
beansOfType.get(key));
}
KeySet
: Map์ ๋ชจ๋ key๋ฅผ ๊ฐ์ ธ์ Set๊ฐ์ฒด๋ก ๋ฐํํ๋ค. for-each ๋ฃจํ๋ฅผ ์ฌ์ฉํ์ฌ ์์ฐจ์ ์ผ๋ก ์ ๊ทผํ๊ณ , ๊ฐ key๋ฅผ ์ฌ์ฉํ์ฌ value์ ์ ๊ทผํ ์ ์๋ค.
beansOfType์ ์๋ ๋ชจ๋ key(๋น์ ์ด๋ฆ)๋ฅผ ๊ฐ์ ธ์ ๋ฐ๋ณตํ์ฌ ๋ณ์ key์ ๋ฃ๋๋ค. ๊ทธ๋ฆฌ๊ณ get(key)๋ฅผ ํตํด ๊ทธ key(๋น์ ์ด๋ฆ)์ ํด๋นํ๋ ๊ฐ์ beansOfType์์ ๊ฐ์ ธ์จ๋ค. ์ฌ๊ธฐ์ beansOfType์ key์ MemberRepository๋ฅผ ์ ์ฅํ๊ณ ์๋ ๋ณ์๋ช ์ผ๋ก, get(key)๋ ๋น์ ์ด๋ฆ์ ๋ง๋ ๊ฐ์ฒด๋ฅผ ๊ฐ์ ธ์ค๋ผ๋ ์๋ฏธ๊ฐ ๋๋ค.
ใ ค•••๊ทธ ๋ค์ ์ฝ๋ •••
System.out.println("beansOfType = " + beansOfType);
assertThat(beansOfType.size()).isEqualTo(2);
beansOfType์ ๋ชจ๋ bean์ ์ด๋ฆ๊ณผ ๊ฐ์ฒด๋ฅผ ์ ์ฅํ๊ณ ์์ผ๋, ๊ทธ๊ฒ์ ๋ชจ๋ ์ถ๋ ฅํ๋ค. assertThat์ ์ด์ฉํ์ฌ beansOfType์ ์๋ bean์ ๊ฐ์๊ฐ 2๊ฐ๊ฐ ๋ง๋์ง ํ์ธํ๋ ์ฝ๋๊ฐ ๋๋ค.
getBean์ NoSuchBeanDefinitionException ํํผํ๊ธฐ
getBean์ NoSuchBeanDefinitionException ํํผํ๊ธฐ
thecodinglog.github.io
[Java] Map ์ ์ฒด ์ถ๋ ฅ(entrySet, keySet, Iterator, Lambda, Stream)
๋ชฉ์ฐจ Map ๊ฐ์ฒด์ ๋ด์ฉ์ ์ถ๋ ฅํ๊ธฐ ์ํ ๋ฐฉ๋ฒ1. entrySet()entrySet() ๋ฉ์๋๋ Map์์ ๋ชจ๋ Entry(Key-Value ์)๋ฅผ ๊ฐ์ ธ์ Set ๊ฐ์ฒด๋ก ๋ฐํํฉ๋๋ค. foreach ๋ฃจํ๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ Entry์ ์์ฐจ์ ์ผ๋ก ์
tychejin.tistory.com
โ๏ธ XML
<bean id="memberService" class="hello.core.member.MemberServiceImpl">
<constructor-arg name="memberRepository" ref="memberRepository" /> ์ฐธ์กฐ
</bean>
์ค์ ๊ตฌํ ๊ฐ์ฒด
<bean id="memberRepository" class="hello.core.member.MemoryMemberRepository" />
GenericXmlApplicatonContext
: ์คํ๋ง ์ค์ ํ์ผ์ ์ฝ์ด์ ๋ก๋ฉ ์ํค๊ณ , ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ฉฐ ์ด๊ธฐํํ๋ ์ญํ ์ ํ๋ค.
- GenericXmlApplicationContext ์ถ์ฒ
[SPRING]GenericXmlApplicationContext๋? ์คํ๋ง ๊ฐ์ฒด ์์ฑ ๋ฐฉ๋ฒ
์ค๋์ ApplicationContext.xml ์คํ๋ง ์ค์ ํ์ผ์ ์ด์ฉํ์ฌ ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ , GenericXmlApplicationContextํด๋์ค๋ฅผ ์ด์ฉํ์ฌ ์์ฑ๋ ๊ฐ์ฒด๋ฅผ ์ ๊ทผํ์ฌ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ํด ์์๋ณด๊ฒ ์ต๋๋ค. ์ดํด๋ฅผ ์ฝ๊ฒ
junghn.tistory.com
.
ํผ์ ์ฒ์ฒํ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์ต๋๋ค.
์๋ชป๋ ๋ด์ฉ์ด ์ ํ์์ ์ ์์ผ๋, ๊ฐ์ํ๊ณ ๋ด์ฃผ์๋ฉด ๊ฐ์ฌํ๊ฒ ์ต๋๋ค.
๐ ๋ชจ๋๋ค ํ์ดํ
ํ์ธ์ ๐