π― λͺ©ν : Beanμ μ΄ν΄
/**
* DIP μλ°
* OrderServiceImpl λ DiscountPolicy μΈν°νμ΄μ€ λΏλ§μλλΌ
* RateDiscountPolicy λ μμ‘΄ νκ³ μλ€.
* OCP μλ°
* RateDiscountPolicy ꡬνν¨μΌλ‘μ κΈ°λ₯μ νμ₯νμλ€.
* κΈ°λ₯ νμ₯ν¨κ³Ό λμμ "OrderServiceImpl ν΄λμ€μ μ½λλ₯Ό λ³κ²½ν΄μΌνλ€."
* private final DiscountPolicy discountPolicy
* = new FixDiscountPolicyImpl();
*
* μ μ¬νλ€μ κ°μ νκΈ° μν΄ AppConfig μμ±νμ¬ κ΄μ¬μ¬ λΆλ¦¬
* μμ±μ μ£Όμ
μΌλ‘ μ ννμ¬ Service ν΄λμ€ λ€μ μΈν°νμ΄μ€μλ§ μμ‘΄νκ² λλ€.
*/
@Configuration
public class AppConfig {
@Bean
public MemberService memberService() {
return new MemberServiceImpl(memberRepository());
}
@Bean
public MemberRepository memberRepository() {
return new MemoryMemberRepository();
}
@Bean
public OrderService orderService() {
return new OrderServiceImpl(
memberRepository(),
discountPolicy()
);
}
@Bean
public DiscountPolicy discountPolicy() {
return new RateDiscountPolicy();
}
}
class ApplicationContextInfoTest {
AnnotationConfigApplicationContext ac =
new AnnotationConfigApplicationContext(AppConfig.class);
@Test
@DisplayName("μ ν리μΌμ΄μ
λΉ μΆλ ₯νκΈ°")
void findApplicationBean() {
String[] beanDefinitionNames = ac.getBeanDefinitionNames();
for (String beanDefinitionName : beanDefinitionNames) {
BeanDefinition beanDefinition
= ac.getBeanDefinition(beanDefinitionName);
//Role ROLE_APPLICATION: μ§μ λ±λ‘ν μ ν리μΌμ΄μ
λΉ
//Role ROLE_INFRASTRUCTURE: μ€νλ§μ΄ λ΄λΆμμ μ¬μ©νλ λΉ
if (beanDefinition.getRole() == beanDefinition.ROLE_APPLICATION) {
Object bean = ac.getBean(beanDefinitionName);
System.out.println(
"name = " + beanDefinitionName +
" Object = "+ bean);
}
}
}
}
// μΆλ ₯κ°
name = appConfig Object = hello.core.AppConfig$$EnhancerBySpringCGLIB$$c9c658a0@2371aaca
name = memberService Object = hello.core.member.MemberServiceImpl@5b529706
name = memberRepository Object = hello.core.member.MemoryMemberRepository@63fdab07
name = orderService Object = hello.core.order.OrderServiceImpl@7b5a12ae
name = discountPolicy Object = hello.core.discount.RateDiscountPolicy@5553d0f5
public class BeanDefinitionTest {
AnnotationConfigApplicationContext ac = new
AnnotationConfigApplicationContext(AppConfig.class);
@Test
@DisplayName("λΉ μ€μ λ©νμ 보 νμΈ")
void findApplicationBean() {
String[] beanDefinitionNames = ac.getBeanDefinitionNames();
for (String beanDefinitionName : beanDefinitionNames) {
BeanDefinition beanDefinition =
ac.getBeanDefinition(beanDefinitionName);
if (beanDefinition.getRole() == BeanDefinition.ROLE_APPLICATION) {
System.out.println("beanDefinitionName" + beanDefinitionName +
" beanDefinition = " + beanDefinition);
}
}
}
}
// μΆλ ₯κ°
beanDefinitionNameappConfig beanDefinition = Generic bean: class [hello.core.AppConfig$$EnhancerBySpringCGLIB$$c9c658a0]; scope=singleton; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null
beanDefinitionNamememberService beanDefinition = Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=appConfig; factoryMethodName=memberService; initMethodName=null; destroyMethodName=(inferred); defined in hello.core.AppConfig
beanDefinitionNamememberRepository beanDefinition = Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=appConfig; factoryMethodName=memberRepository; initMethodName=null; destroyMethodName=(inferred); defined in hello.core.AppConfig
beanDefinitionNameorderService beanDefinition = Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=appConfig; factoryMethodName=orderService; initMethodName=null; destroyMethodName=(inferred); defined in hello.core.AppConfig
beanDefinitionNamediscountPolicy beanDefinition = Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=appConfig; factoryMethodName=discountPolicy; initMethodName=null; destroyMethodName=(inferred); defined in hello.core.AppConfig