"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 ๊ฐ์ ๋ฎ์ด์ผ๋ค.
์ด ๋ฌธ์ ๋ ๋ด๊ฐ ์ด ๋น์ด ์ฐ์ ์ ๋์ง ์๋ ๊ฒ์ด๋ค.
์ด์ ๊ด๋ จ๋ ๊ธ์ ์ด ๋งํฌ์์ ํ์ธํ ์ ์๋ค.
ํด๊ฒฐ ๋ฐฉ๋ฒ
@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ํ๋ค.
๋ฌธ์ ์ 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