๐ŸƒSpring AutoConfiguration

1 minute read

์Šคํ”„๋ง์˜ ์‹œ์ž‘์ง€์ 

@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๋ถ€ ์Šคํ”„๋ง ๋ถ€ํŠธ ์›๋ฆฌ