1. Spring 기본 Configuration.java 파일 설정
2. Spring Boot Configuration
public class MemberService {
public void SetMemberRepository(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
}
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() {
MemberService memberService = new MemberService();
memberService.SetMemberRepository(memberRepository());
return memberService;
}
@Bean
public MemberRepository memberRepository() {
return new MemoryMemberRepository();
}
}
public class MemberService {
// @Autowired 없어도 됨 (직접 매개변수로 받기 때문)
public MemberService(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
}
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();
}
}