๐Spring AutoConfiguration
์คํ๋ง์ ์์์ง์
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@SpringBootApplication
- ์ ์ ๋
ธํ
์ด์
์ ํ๊ณ ๋ค์ด๊ฐ๋ณด๋ฉด
@SpringBootConfiguration
,@ComponentScan
,@EnableAutoConfiguration
์ธ ๊ฐ์ ์ ๋ ธํ ์ด์ ์ด ํฉ์ณ์ ธ ์๋ ํํ์ด๋ค.
์คํ๋ง ๋ถํธ ์ดํ๋ฆฌ์ผ์ด์ ์ด
Bean
์ ๋ฑ๋กํ๋ ๊ณผ์ ์ ์๋์ ๊ฐ๋ค.
1๋จ๊ณ:@ComponentScan
์ผ๋ก ์ฐพ์ Bean์ ๋ฑ๋กํ๋ค.
2๋จ๊ณ:@EnableAutoConfiguration
์ผ๋ก ์ถ๊ฐ์ ์ผ๋ก ์ฝ์ด์จ Bean์ ๋ฑ๋กํ๋ค.
@ComponentScan
- ์๊ธฐ ์์ ์ด ์์นํ ํจํค์ง๋ก๋ถํฐ ํ์์ ํจํค์ง๋ฅผ ์ค์บํด์ Bean์ ๋ฑ๋กํ๋ค. ๋ํ์ ์ธ ๊ฒ์๋์์
@Configuration
,@Repository
,@Service
,@Controller
,@RestController
์ด๋ค.
@EnableAutoConfiguration
Configuration
์ด๋ Bean์ ์ฝ์ด์ค๊ธฐ ์ํ ์กฐ๊ฑด์ด ์ ์๋ ์ค์ ํ์ผ์ด๋ค.org.springframework.boot.autoconfigure
ํจํค์ง์์spring.factories
ํ์ผ์ ์ด์ด๋ณด๋ฉด ์๋์ ๊ฐ์ ์๋ง์ ํค๊ฐ์ด ์ ํ์๋ค.
- ์ ํ์ผ์ ์ ์๋ Configuration์ ์ํด
Bean
๋ค์ด ์์ฑ๋๊ฒ ๋๋ค.
Configuration ์์ฑ
- Configuration์ ๋ณ๋์ ํ๋ก์ ํธ๋ก ์์ฑํ์ฌ jar ํํ๋ก ์ฌ์ฉํ ์ ์๋ค.
- ์์ฑ์ด ๋๋ ๋ค์๋
mvn install
๋ช ๋ น์ ํตํด Local Repository์ ์ค์น๋์ด ๋ค๋ฅธ ํ๋ก์ ํธ์์ ํ์ฉํ ์ ์๋ค. - ์๋์ ๊ฐ์ด IntelliJ ๋ฉ๋ด๋ฅผ ํตํด ์ค์นํ ์ ๋ ์๋ค.
์์กด์ฑ ์ค์
Configuration
์ ๋ง๋ค๊ธฐ ์ํด ๋จผ์ pom.xml
๋ถํฐ ์ค์ ํ๋ค.
<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>
Configuration ์์ฑ
@Configuration
์ ๋ถ์ธ ํด๋์ค ๋ด์Bean
์ ์ ์ธํ๋ค.@ConditionalOnMissingBean
์ ์ฃผ์ํ์. ์๋ ๊ฒฝ์ฐ ๋ช ์์ ์ธ Bean๋ณด๋ค Configuration์ด ์ฐ์ ์์๋ฅผ ๊ฐ์ง๋ค.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class PersonConfiguration {
@Bean
@ConditionalOnMissingBean // ์ด Configuration์ ์ฌ์ฉํ๋ ํ๋ก์ ํธ์์ ๊ฐ์ Bean์ด ์์ผ๋ฉด ์์ฑํ์ง ์๋๋ค.
public Person person() {
Person person = new Person();
person.setAge(40);
person.setName("๊นจ๋ฐ์");
return person;
}
}
spring.factories ํ์ผ ์์ฑ
main/resources/META-INF
๋๋ ํ ๋ฆฌ์spring.factories
ํ์ผ์ ์์ฑํ๋ค.- ์๋์ ๊ฐ์ด ๋ง๋ค์ด์ค ์ค์ ํ์ผ์ ๋ช ์์ ์ผ๋ก ์ ์ํ๋ค.
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.develop4.PersonConfiguration
- ๋ชจ๋ ์์ฑ์ด ๋๋ฌ์ผ๋ฉด ์ฒ์์ ์ค๋ช
ํ ๊ฒ์ฒ๋ผ
mvn install
์ ํตํด ์ค์นํ๋ค.
๋ค๋ฅธ ํ๋ก์ ํธ์์ Configuration ์ฌ์ฉ
- ์๋์ ๊ฐ์ด
pom.xml
์ ์ค์นํ ์์กด์ฑ์ ์ถ๊ฐํ๋ค.
<dependency>
<groupId>com.develop4</groupId>
<artifactId>cfg-test-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
- ์๋์ ๊ฐ์ด Bean์ด ์๋ ์์ฑ๋จ์ ํ์ธํ ์ ์๋ค.
@Component
public class PersonRunner implements ApplicationRunner {
@Autowired
Person person; // ์ด ํ๋ก์ ํธ์์๋ ๋ง๋ ์ ์ด ์๋ Bean์ด Configuration์ ํตํด ์ฃผ์
๋๋ค.
public void run(ApplicationArguments args) throws Exception {
System.out.println(person);
}
}
์คํ๋ง ๋ถํธ์์ ์ด๋ป๊ฒ ๋ฐ๋ก ์ปจํธ๋กค๋ฌ๋ฅผ ๋ง๋ค์ด๋ผ ์ ์์๊น?
@RestController
public class UserController {
@GetMapping
public String hello() {
return "hello";
}
}
- ์๊ฐํด๋ณด๋ฉด ์ฐ๋ฆฌ๋ ๊ทธ๋ฅ ํด๋์ค๋ฅผ ํ๋ ๋ง๋ค์์ ๋ฟ์ด๋ค. ๋ฌผ๋ก
@RestController
๋ฅผ ํตํด Bean์ผ๋ก ๋ฑ๋ก์ด ๋ ๊ฑฐ๊ณ ์ด๋ป๊ฒ๋ ๊ฐ์ ธ๊ฐ์ REST API๋ฅผ ํ๋ ๋ง๋ค์ด๋ด๊ฒ ์ง๋ผ๊ณ ์๊ฐํด๋ณผ ์๋ ์๋ค. - ๊ตฌ์ฒด์ ์ธ ๊ตฌํ์
WebMvcAutoConfiguration
์ด๋ผ๋ AutoConfiguration์ ํตํด ์ค์ ๋๋ค. Converter, Interceptor, ResourceHandler, CorsMapping, ViewResolver, ArgumentResolver, Message Converter์ ๊ฐ์ ๋ค์ํ ์ค์ ์ด ์ ๊ณต๋๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก ๋ชจ๋ ์ค์ ๋์ด ์์ผ๋ฏ๋ก ํน๋ณํ ์ผ์ด ์๋ค๋ฉด ์์ฒ๋ผ ๋ฐ๋ก ์น ๊ฐ๋ฐ์ด ๊ฐ๋ฅํ ๊ฒ์ด๋ค.
[1] ๋ฐฑ๊ธฐ์ , ์คํ๋ง ๋ถํธ ๊ฐ๋ ๊ณผ ํ์ฉ, 3๋ถ ์คํ๋ง ๋ถํธ ์๋ฆฌ