DI를 통해서 모듈 간의 결합도가 낮아지고, 유연성이 높아진다.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans ">
<bean id = "conveniencePayService" class="com.zerobase.convpay.service.ConveniencePayService">
<constructor-arg name="paymentInterfaceSet">
<set>
<ref bean="moneyAdapter"/>
<ref bean="cardAdapter"/>
</set>
</constructor-arg>
<constructor-arg name="discountInterface" ref="discountByConvenience"/>
</bean>
<bean id="cardAdapter" class="com.zerobase.convpay.service.CardAdapter"/>
<bean id="moneyAdapter" class="com.zerobase.convpay.service.MoneyAdapter"/>
<bean id="discountByConvenience" class="com.zerobase.convpay.service.DiscountByConvenience" />
<bean id="discountByPayMethod" class="com.zerobase.convpay.service.DiscountByPayMethod" />
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans">
<context:component-scan base-package="com.zerobase.convpay"/>
</beans>
@Configuration
public class ApplicationConfig {
@Bean
public ConveniencePayService conveniencePayService(){
return new ConveniencePayService(
new HashSet<>(Arrays.asList(moneyAdapter(), cardAdapter())),
discountByConvenience()
);
}
@Bean
public CardAdapter cardAdapter() {
return new CardAdapter();
}
@Bean
public MoneyAdapter moneyAdapter() {
return new MoneyAdapter();
}
@Bean
public DiscountByConvenience discountByConvenience() {
return new DiscountByConvenience();
}
}
@Configuration
@ComponentScan(basePackages = "com.zerobase.convpay")
public class ApplicationConfig {
}