package hello.hellospring;
import hello.hellospring.repository.MemberRepository;
import hello.hellospring.repository.MemoryMemberRepository;
import hello.hellospring.service.MemberService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SpringConfig {
@Bean
public MemberService memberService() {
return new MemberService(memberRepository());
}
@Bean
public MemberRepository memberRepository() {
return new MemoryMemberRepository();
}
}
Q. 만약, @Autowired붙이고, SpringConfig에 아무것도 없다면 작동이 될까?
DI 의존관계 주입방법
@Autowired private MemberRepository memberRepository;
누구나 호출 가능/개발은 호출되지않아야하는 것들이 호출되면 안됨 )-> 만약에 바꾸면 문제가 생길 수 있음
private final MemberRepository memberRepository;
@Autowired
public void setMemberRepository(MemberRepository memberRepository) {
this.memberRepository = memberRepository
}
private final MemberRepository memberRepository;
/*
* Q. 만약, @Autowired붙이고, SpringConfig에 아무것도 없다면 작동이 될까?
* A. No. why? 스프링이 MemberService를 관리하고있지 않기 때문 */
public MemberService(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
}
직접 운영할 때 장점