작성
·
380
0
아래는 AutoAppConfigTest랑 AutoAppConfig 코드입니다..
public class AutoAppConfigTest {
@Test
void basicScan(){
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AutoAppConfig.class);
MemberService memberService = ac.getBean(MemberService.class);
assertThat(memberService).isInstanceOf(MemberService.class);
OrderServiceImpl bean = ac.getBean(OrderServiceImpl.class);
MemberRepository memberRepository = bean.getMemberRepository();
System.out.println("memberRepository = " + memberRepository);
}
}
package hello.core;
import hello.core.discount.DiscountPolicy;
import hello.core.discount.RateDiscountPolicy;
import hello.core.member.MemberRepository;
import hello.core.member.MemoryMemberRepository;
import hello.core.order.OrderService;
import hello.core.order.OrderServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
@Configuration
@ComponentScan (
basePackages = "hello.core", //여기 패키지부터 스캔
//basePackageClasses =
//컴포넌트 스캔중 뺄거 지정 (지정하지 않을시 default 현재 @ComponentScan이 붙은 설정정보 클래스의 package)
excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Configuration.class)
)
public class AutoAppConfig {
@Bean(name = "memoryMemberRepository")
MemberRepository memberRepository(){
return new MemoryMemberRepository();
}
@Bean(name = "orderService")
public OrderService orderService(){
System.out.println("call AppConfig.orderService");
return new OrderServiceImpl(memberRepository(), discountPolicy());
//return null; ////필드주입
}
@Bean(name = "discountPolicy")
public DiscountPolicy discountPolicy(){
return new RateDiscountPolicy();
}
}
답변 2
0
넵
OrderService 인터페이스
package hello.core.order;
public interface OrderService {
Order createOrder(Long memberId, String itemName, int itemPrice);
}
OrderServiceImpl 클래스
package hello.core.order;
import hello.core.discount.DiscountPolicy;
import hello.core.discount.FixDiscountPolicy;
import hello.core.discount.RateDiscountPolicy;
import hello.core.member.Member;
import hello.core.member.MemberRepository;
import hello.core.member.MemoryMemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
//@RequiredArgsConstructor //lombok 생성자주입 (final 붙은 필드를 가지고 생성자를 자동으로 만들어준다)
@Component
public class OrderServiceImpl implements OrderService{
//필드주입
// @Autowired private MemberRepository memberRepository;
//
// @Autowired private DiscountPolicy discountPolicy;
private final MemberRepository memberRepository;
private final DiscountPolicy discountPolicy;
//생성자 주입
@Autowired // 생성자 1개일시 쓰지않아도 됨
public OrderServiceImpl(MemberRepository memberRepository, DiscountPolicy rateDiscountPolicy) {
System.out.println("생성자호출");
this.memberRepository = memberRepository;
this.discountPolicy = rateDiscountPolicy;
}
//수정자 주입
// @Autowired
// public void setMemberRepository(MemberRepository memberRepository){
// this.memberRepository = memberRepository;
// }
//
// @Autowired
// public void setDiscountPolicy(DiscountPolicy discountPolicy){
// this.discountPolicy = discountPolicy;
// }
//메소드 주입입
// @Autowired
// public void init(MemberRepository memberRepository, DiscountPolicy discountPolicy){
// this.memberRepository = memberRepository;
// this.discountPolicy = discountPolicy;
// }
@Override
public Order createOrder(Long memberId, String itemName, int itemPrice) {
Member member = memberRepository.findById(memberId);
int discountPrice = discountPolicy.discount(member, itemPrice);
return new Order(memberId,itemName, itemPrice, discountPrice);
}
public MemberRepository getMemberRepository() {
return memberRepository;
}
}
AppConfig의 oderService() 의 반환타입을 OrderService가 아니라 OrderServiceImpl로 변경해보시겠어요?
아래 답변을 참고하시면 좋을 것 같습니다.
https://www.inflearn.com/questions/69229