changju / study-springboot

0 stars 0 forks source link

[3부: 스프링 부트 원리] 11. 자동 설정 만들기 2부: @ConfigurationProperties #5

Open changju opened 2 years ago

changju commented 2 years ago

3부: 스프링 부트 원리

11. 자동 설정 만들기 2부: @ ConfigurationProperties

changju commented 2 years ago

AutoConfiguration

<project xmlns="...">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.study.springboot</groupId>
    <artifactId>spring-boot-chap3-10-autoconfiguration-2-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>

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

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.0.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>
@Configuration
@EnableConfigurationProperties(PrettyFatherProperties.class)
public class PrettyFatherConfiguration {
    @Bean
    @ConditionalOnMissingBean // 등록이 되어진 bean 이 없으면 등록 하여
    public PrettyFather prettyFather(PrettyFatherProperties properties) {
        PrettyFather pFather = new PrettyFather();
        pFather.setName(properties.getName());
        pFather.setOld(properties.getOld());
        return pFather;
    }
}
@Getter
@Setter
@ToString
@ConfigurationProperties("prettyfather")
public class PrettyFatherProperties {
    String name;
    int old;

}
changju commented 2 years ago

UseConfiguration