๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Springboot

์Šคํ”„๋ง ๋ถ€ํŠธ์˜ Auto Configuration ์ง์ ‘ ๊ตฌํ˜„ํ•ด๋ณด๊ธฐ

 

"Xxx-Spring-Boot-Starter" ์ž๋™ ์„ค์ •์„ ์ง์ ‘ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

jueun-spring-boot-starter ๊ตฌํ˜„ํ•˜๊ธฐ

1. maven ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ, ์˜์กด์„ฑ ์ถ”๊ฐ€

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.jueun</groupId>
    <artifactId>jueun-spring-boot-starter</artifactId>
    <version>1.0-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>
    </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>

2. DTO(VO) ์ž‘์„ฑ

package com.jueun;

public class Holoman {
    String name;
    int howLong;
    
    // getter(), setter(), toString() ์ƒ๋žต
}

3. @Configuration ํŒŒ์ผ ์ž‘์„ฑ

package com.jueun;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HolomanConfiguration {

    @Bean
    public Holoman holoman(){
        Holoman holoman = new Holoman();
        holoman.setHowLong(5);
        holoman.setName("jueun");
        return holoman;
    }

}

4. spring.factories ํŒŒ์ผ ์ž‘์„ฑ

์„œ๋น„์Šค ํ”„๋กœ๋ฐ”์ด๋” ๊ฐ™์€ ํŒจํ„ด์ด๋ผ๊ณ  ํ•œ๋‹ค.

src/main/resource/META-INF์— spring.factories์„ ์ƒ์„ฑํ•˜๊ณ  spring.factories ์•ˆ์— ์ž๋™ ์„ค์ • ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.jueun.HolomanConfiguration

5. mvn install

์ง€๊ธˆ๊นŒ์ง€ ์ž‘์„ฑํ•œ ํŒŒ์ผ ๊ตฌ์กฐ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

Maven > install ๋”๋ธ”ํด๋ฆญ

ํ”„๋กœ์ ํŠธ๋ฅผ build๋ฅผ ํ•ด์„œ jar ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ ๊ฒƒ์„ ๋‹ค๋ฅธ maven ํ”„๋กœ์ ํŠธ์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก local maven ์ €์žฅ์†Œ์—๋‹ค๊ฐ€ ์„ค์น˜๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์ž๋™ ์„ค์ • ์‹คํ–‰ํ•˜๊ธฐ

์ด์ œ ์ž๋™ ์„ค์ •์„ ๋‹ค๋ฅธ ํ”„๋กœ์ ํŠธ์—์„œ ๋ถˆ๋Ÿฌ์™€์„œ ์‹คํ–‰๋˜๋Š”์ง€ ํ™•์ธํ•ด๋ณด์ž!

1. spring boot ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑ, ์˜์กด์„ฑ ์ถ”๊ฐ€

์˜์กด์„ฑ์ด ์ž˜ ๋“ค์–ด์™”๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜์žˆ๋‹ค.

<dependency>
            <groupId>com.jueun</groupId>
            <artifactId>jueun-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
</dependency>

2. Runner ํด๋ž˜์Šค ์ž‘์„ฑ

package com.jueun.mainproject;

import com.jueun.Holoman;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class HolomanRunner implements ApplicationRunner {

    @Autowired
    Holoman holoman;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println(holoman);
    }

}

3. ์‹คํ–‰

Application ํด๋ž˜์Šค์—์„œ๋Š” Holoman์— ๋Œ€ํ•œ ๊ทธ ์–ด๋–ค ๋‚ด์šฉ๋„ ์ ํ˜€์žˆ์ง€ ์•Š์œผ๋‚˜, ์‹คํ–‰ํ•ด๋ณด๋ฉด ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

package com.jueun.mainproject;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

์ถœ๋ ฅ ํ™•์ธ


๋ฌธ์ œ์  1

๋” ๋‚˜์•„๊ฐ€์„œ ๋ฉ”์ธ ํ”„๋กœ์ ํŠธ์—์„œ ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ๋นˆ์˜ ๊ฐ’์„ ์ง์ ‘ ๋“ฑ๋กํ•ด๋ณด์ž

package com.jueun.mainproject;

import com.jueun.Holoman;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public Holoman holoman(){
        Holoman holoman = new Holoman();
        holoman.setHowLong(50);
        holoman.setName("jueun2");
        return holoman;
    }

}

ํ•˜์ง€๋งŒ ์—ฌ์ „ํžˆ ์‹คํ–‰๊ฐ’์€ jueun, 5 ๊ฐ’์ด ์ถœ๋ ฅ๋œ๋‹ค.

๊ทธ ์ด์œ ๋Š” ComponentScan ์ดํ›„์— EnableAutoConfiguration์œผ๋กœ ๋นˆ์„ ๋“ฑ๋กํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

@ComponentScan ์‹œ jueun2, 50 ๊ฐ’์ด ์จ์กŒ์ง€๋งŒ, @EnableAutoConfiguration ์‹œ, jueun, 5 ๊ฐ’์„ ๋ฎ์–ด์ผ๋‹ค.

์ด ๋ฌธ์ œ๋Š” ๋‚ด๊ฐ€ ์“ด ๋นˆ์ด ์šฐ์„ ์‹œ ๋˜์ง€ ์•Š๋Š” ๊ฒƒ์ด๋‹ค.

์ด์™€ ๊ด€๋ จ๋  ๊ธ€์€ ์ด ๋งํฌ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์Šคํ”„๋ง ๋ถ€ํŠธ ์ž๋™์„ค์ • ๊ฐ„๋‹จํ•˜๊ฒŒ ์ดํ•ดํ•˜๊ธฐ

bean์€ 2๋‹จ๊ณ„๋กœ ๋‚˜๋ˆ ์„œ ์ฝํžŒ๋‹ค. @SpringBootApplication ์•ˆ์— @ComponentScan์™€ @EnableAutoConfiguration์ด ์ˆจ์–ด์žˆ๋‹ค. 1๋‹จ๊ณ„: @ComponentScan @ComponentScan์€ ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ์ด๋‹ค. ํŠน์ • ํŒจ..

hoit1302.tistory.com

ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

@ConditionalOnMissingBean

์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” @ConditionalOnMissingBean์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

ComponentScan์œผ๋กœ jueun2, 50 ๊ฐ’์˜ Holoman์ด ์žˆ์œผ๋‹ˆ AutoConfiguration ์‹œ์— jueun, 5 ๊ฐ’์˜ ๋นˆ์€ ๋“ฑ๋กํ•˜์ง€ ์•Š๋„๋ก ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

package com.jueun;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HolomanConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public Holoman holoman(){
        Holoman holoman = new Holoman();
        holoman.setHowLong(5);
        holoman.setName("jueun");
        return holoman;
    }

}

๋‹ค์‹œ mvn installํ•˜๊ณ  ๋ฉ”์ด๋ธ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” maven์— ๋Œ€ํ•ด์„œ refreshํ•œ๋‹ค.

maven refresh


๋ฌธ์ œ์  2

public Holoman holoman(){
        Holoman holoman = new Holoman();
        holoman.setHowLong("jueun");
        holoman.setName(5);
        return holoman;
}

์œ„์™€ ๊ฐ™์ด ๋นˆ์— ๊ฐ’์„ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•ด ํ•จ์ˆ˜๋Š” ์“ฐ๋Š” ๊ฒƒ์€ ๋น„ํšจ์œจ์ ์ด๋‹ค.

properties ํŒŒ์ผ์—์„œ ๊ฐ’์„ ๋ฐ”๊พธ๋Š” ๊ฒƒ ๋งŒ์œผ๋กœ ๋นˆ์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ๋‹ค.

ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

1. @ConfigurationProperties("")

package com.jueun;


import org.springframework.boot.context.properties.ConfigurationProperties;

// ๋ฉ”์ธํ”„๋กœ์ ํŠธ์—์„œ holoman๋กœ ์‹œ์ž‘ํ•˜์—ฌ ํ”„๋กœํผํ‹ฐ๋กœ ์ ‘๊ทผํ•œ๋‹ค.
@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;
    }
}

2. HolomanConfiguration ์ž‘์„ฑ

@EnableConfigurationProperties(HolomanProperties.class)

package com.jueun;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(HolomanProperties.class)
public class HolomanConfiguration {

    // @Bean์œผ๋กœ ์ •์˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์ž๋™์œผ๋กœ ์Šคํ”„๋ง์ด ์ฃผ์ž…
    @Bean
    @ConditionalOnMissingBean
    public Holoman holoman(HolomanProperties properties){
        Holoman holoman = new Holoman();
        holoman.setHowLong(properties.getHowLong());
        holoman.setName(properties.getName());
        return holoman;
    }

}

3, ํ”„๋กœํผํ‹ฐ ํ‚ค๊ฐ’ ์ž๋™ ์™„์„ฑ ์˜์กด์„ฑ ์ถ”๊ฐ€

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

4. mvn install / ๋ฉ”์ธ์€ Maven refresh

5. application.properties ํŒŒ์ผ ์ž‘์„ฑ

holoman.name=jueun3
holoman.how-long=30

์ถœ๋ ฅ ํ™•์ธ