본문 바로가기

Spring/Spring Boot

(SpringBoot) Spring Boot의 자동설정 구현해보기(2)

https://jwdeveloper.tistory.com/109

 

(SpringBoot) Spring Boot의 자동설정 구현해보기(1)

spring-boot-starter 구현 1. 의존성 추가 org.springframework.boot spring-boot-autoconfigure <groupi..< p=""> </groupi..<>

jwdeveloper.tistory.com

이전에 생겼던 문제 (@Component로 등록하였던 Bean이 사라지는 현상)을 해결해보자!

 

@ConditionalOnMissingBean

: 해당 Type의 Bean이 없으면 Bean으로 등록하게끔 설정

 

(junwoo-spring-boot-starter project)

@Configuration
public class HolomanConfiguration {
    @Bean
    @ConditionalOnMissingBean
    public Holoman holoman() {
        Holoman holoman = new Holoman();
        holoman.setHowLong(5);
        holoman.setName("junwoo");

        return holoman;
    }
}

 

다시 maven을 install 하고 springbootgettingstarted로 돌아와서 maven을 refresh 후 실행한다.

 

실행결과 이제 원하는 결과가 출력된다.


또 한 가지 문제점 발생

"나는 그냥 값만 살짝 변경하고 싶을 뿐인데... 이렇게 장황하게 Bean으로 등록해야 하는가?"

 

Applicataion.properties의 활용

 

해당 구조로 설정 파일을 생성

 

 

https://jwdeveloper.tistory.com/86

 

(SpringBoot) 외부설정(1)

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-c%20onfig Spring Boot Reference Documentation This section goes into more detail about how you should us..

jwdeveloper.tistory.com

 

Properties 설정 클래스 추가 (junwoo-spring-boot-starter)

@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;
    }
}

Configuration의 파라미터를 설정한 Properties로 받아오게끔 설정  (junwoo-spring-boot-starter)

@Configuration
@EnableConfigurationProperties(HolomanProperties.class)
public class HolomanConfiguration {
    @Bean
    @ConditionalOnMissingBean
    public Holoman holoman(HolomanProperties properties) {
        Holoman holoman = new Holoman();
        holoman.setHowLong(properties.getHowLong());
        holoman.setName(properties.getName());

        return holoman;
    }
}

mvn install 재실행!


application.properties로 값을 설정한다. (springbootgettingstarted)

spring.main.allow-bean-definition-overriding=true
holoman.name = funny coding
holoman.howLong = 30

생성하였던 Bean 제거 (springbootgettingstarted)

@SpringBootApplication
public class SpringbootgettingstartedApplication {

    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(SpringbootgettingstartedApplication.class);
        springApplication.setWebApplicationType(WebApplicationType.NONE);
        springApplication.run(args);
    }


}

 

실행결과


코드 참조

 

https://github.com/mike6321/Spring/tree/master/SpringBoot/springbootgettingstarted

 

mike6321/Spring

Contribute to mike6321/Spring development by creating an account on GitHub.

github.com

https://github.com/mike6321/Spring/tree/master/SpringBoot/junwoo-spring-boot-starter

 

mike6321/Spring

Contribute to mike6321/Spring development by creating an account on GitHub.

github.com