Spring Boot Config 파일

Codren·2021년 9월 9일
1

Section 1. Spring Boot Configuration

1. Spring 기본 Configuration.java 파일 설정




2. Spring Boot Configuration

  • 기본적으로 Spring Boot Web App 프로젝트 폴더내에서 @ComponentScan

  • Setter DI 를 사용할 경우 Config
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();
    }
}



  • Constructor DI 를 사용할 경우 Config
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();
    }
}

0개의 댓글