SpringBoot

자동 설정 구현.2

Jungsoomin :) 2020. 10. 2. 18:51

어떻게 덮어쓰기를 방지할까.

  • 덮어쓰기 방지하기 : ComponentScan 우선시 되기

         @ConditionalOnMissingBean : 해당 타입의 빈이 없을때만 해당 빈을 등록해라

 


일일히 빈 등록을 하지 않고 application.properties 에 등록해서 쓰기

 

  • 설정클래스프로퍼티에 대한 설정 클래스 정의
  • @ConfiguationProperties(prefix) 
  • 설정을 사용하기 위해 설정클래스@EnableConfigurationProperties(Property Class)
  • 자동 설정 파일의 Bean 객체에 Properties 클래스 주입
@ConfigurationProperties("holoman")
public class HolomanProperties {

    private String name;
    private int howLong;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getHowLong() {
        return howLong;
    }

    public void setHowLong(int howLong) {
        this.howLong = howLong;
    }

    @Override
    public String toString() {
        return "HolomanProperties{" +
                "name='" + name + '\'' +
                ", howLong=" + howLong +
                '}';
    }
}
@Configuration
@EnableConfigurationProperties(HolomanProperties.class)
public class HolomanConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public Holoman holoman(HolomanProperties properties){// HoloProperties 주입
        Holoman holoman = new Holoman();
        holoman.setHowLong(properties.getHowLong());
        holoman.setName(properties.getName());
        return holoman;
    }
}

 

자동 설정을 위해 필요한 모듈

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-configuration-processor</artifactId>
	<optional>true</optional>
</dependency>

 


흐름

  1. Bean 미등록시 자동 설정 클래스에서 Properties 클래스를 참조하여 빈을 만듬
  2. Properties 클래스프로젝트의 properties 파일에서 값을 읽어옴

'SpringBoot' 카테고리의 다른 글

외부설정  (0) 2020.10.03
SpringApplication 클래스  (0) 2020.10.03
독립적으로 실행가능한 JAR  (0) 2020.10.03
내장 웹 서버  (0) 2020.10.02
자동 설정 구현.1  (0) 2020.10.02