93
KSUG 이수홍 Deep dive into Spring Boot Autoconfiguration

스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

  • Upload
    -

  • View
    2.441

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

KSUG�이수홍

Deep�dive�into�Spring�Boot��

Autoconfiguration

Page 2: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

Why�Autoconfiguration?

Page 3: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

스프링부트�이전의�설정

Page 4: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

기존�MVC�설정

@Configuration @EnableWebMvc public class WebAppConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(“/resource/") .addResourceLocations("/resource/**"); } @Bean public ViewResolver internalResourceViewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/views/"); viewResolver.setSuffix(".jsp"); return viewResolver; }…

Page 5: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

DB�설정

@EnableTransactionManagement @Configuration public class DatabaseConfig { @Bean public DataSource dataSource( @Value("${db.driver}") Class<Driver> driverClass, @Value("${db.url}") String url, @Value("${db.user}") String user, @Value("${db.password}") String password) { return new SimpleDriverDataSource( BeanUtils.instantiateClass(driverClass), url, user, password); } }…

Page 6: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

JPA�설정

@Configuration public class JpaConfig { @Autowired private DataSource dataSource; @Bean public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean() { LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource); em.setPackagesToScan("com.springcamp.test"); em.setPersistenceProvider(new HibernatePersistenceProvider()); em.setMappingResources("META-INF/orm.xml"); em.setJpaProperties(hibernateProperties()); return em; } @Bean public Properties hibernateProperties() { Properties properties = new Properties(); properties.put(DIALECT, MySQLDialect.class.getName()); properties.put(HBM2DDL_AUTO, ddlAutoProp); return properties; } ….

Page 7: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

새로운�프로젝트�마다�반복적인�설정

Page 8: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

스프링부트에서�어떻게�설정하는가?

Page 9: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

기존�MVC�설정

@Configuration @EnableWebMvc public class WebAppConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(“/resource/") .addResourceLocations("/resource/**"); } @Bean public ViewResolver internalResourceViewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/views/"); viewResolver.setSuffix(".jsp"); return viewResolver; }}

Page 10: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

기존�MVC�설정

@Configuration @EnableWebMvc public class WebAppConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(“/resource/") .addResourceLocations("/resource/**"); } @Bean public ViewResolver internalResourceViewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/views/"); viewResolver.setSuffix(".jsp"); return viewResolver; }}

Page 11: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

기존�MVC�설정

@Configuration @EnableWebMvc public class WebAppConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(“/resource/") .addResourceLocations("/resource/**"); } @Bean public ViewResolver internalResourceViewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/views/"); viewResolver.setSuffix(".jsp"); return viewResolver; }}

“/resource/""/resource/**"

ResourceHandler

Page 12: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

기존�MVC�설정

@Configuration @EnableWebMvc public class WebAppConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(“/resource/") .addResourceLocations("/resource/**"); } @Bean public ViewResolver internalResourceViewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/views/"); viewResolver.setSuffix(".jsp"); return viewResolver; }}

“/resource/""/resource/**"

ResourceHandler

Prefix("/WEB-INF/views/")Suffix(".jsp")

Page 13: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

스프링�부트�MVC�설정

1. 의존성�추가 spring-boot-starter-web�

2. 환경변수�추가� spring.mvc.view.prefix:�/WEB-INF/viewsspring.mvc.view.suffix:�.jsp

Page 14: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

기존�DB�설정

@EnableTransactionManagement @Configuration public class DatabaseConfig { @Bean public DataSource dataSource( @Value("${db.driver}") Class<Driver> driverClass, @Value("${db.url}") String url, @Value("${db.user}") String user, @Value("${db.password}") String password) { return new SimpleDriverDataSource( BeanUtils.instantiateClass(driverClass), url, user, password); } }}

Page 15: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

기존�DB�설정

@EnableTransactionManagement @Configuration public class DatabaseConfig { @Bean public DataSource dataSource( @Value("${db.driver}") Class<Driver> driverClass, @Value("${db.url}") String url, @Value("${db.user}") String user, @Value("${db.password}") String password) { return new SimpleDriverDataSource( BeanUtils.instantiateClass(driverClass), url, user, password); } }}

Page 16: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

기존�DB�설정

@EnableTransactionManagement @Configuration public class DatabaseConfig { @Bean public DataSource dataSource( @Value("${db.driver}") Class<Driver> driverClass, @Value("${db.url}") String url, @Value("${db.user}") String user, @Value("${db.password}") String password) { return new SimpleDriverDataSource( BeanUtils.instantiateClass(driverClass), url, user, password); } }}

"${db.driver}" "${db.url}" "${db.user}" "${db.password}"

Page 17: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

기존�DB�설정

@EnableTransactionManagement @Configuration public class DatabaseConfig { @Bean public DataSource dataSource( @Value("${db.driver}") Class<Driver> driverClass, @Value("${db.url}") String url, @Value("${db.user}") String user, @Value("${db.password}") String password) { return new SimpleDriverDataSource( BeanUtils.instantiateClass(driverClass), url, user, password); } }}

"${db.driver}" "${db.url}" "${db.user}" "${db.password}"SimpleDriverDataSource

Page 18: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

스프링�부트�DB�설정

1. 의존성�추가 spring-boot-starter-jdbc�

2. 환경변수�추가spring.datasource.url:�접속�url��� spring.datasource.driverClassName:�드라이브spring.datasource.username:�saspring.datasource.password:

Page 19: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

JPA�설정

@Configuration public class JpaConfig { @Autowired private DataSource dataSource; @Bean public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean() { LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource); em.setPackagesToScan("com.springcamp.test"); em.setPersistenceProvider(new HibernatePersistenceProvider()); em.setMappingResources("META-INF/orm.xml"); em.setJpaProperties(hibernateProperties()); return em; } @Bean public Properties hibernateProperties() { Properties properties = new Properties(); properties.put(DIALECT, MySQLDialect.class.getName()); properties.put(HBM2DDL_AUTO, ddlAutoProp); return properties; } //..}

Page 20: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

JPA�설정

@Configuration public class JpaConfig { @Autowired private DataSource dataSource; @Bean public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean() { LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource); em.setPackagesToScan("com.springcamp.test"); em.setPersistenceProvider(new HibernatePersistenceProvider()); em.setMappingResources("META-INF/orm.xml"); em.setJpaProperties(hibernateProperties()); return em; } @Bean public Properties hibernateProperties() { Properties properties = new Properties(); properties.put(DIALECT, MySQLDialect.class.getName()); properties.put(HBM2DDL_AUTO, ddlAutoProp); return properties; } //..}

Page 21: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

JPA�설정

@Configuration public class JpaConfig { @Autowired private DataSource dataSource; @Bean public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean() { LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource); em.setPackagesToScan("com.springcamp.test"); em.setPersistenceProvider(new HibernatePersistenceProvider()); em.setMappingResources("META-INF/orm.xml"); em.setJpaProperties(hibernateProperties()); return em; } @Bean public Properties hibernateProperties() { Properties properties = new Properties(); properties.put(DIALECT, MySQLDialect.class.getName()); properties.put(HBM2DDL_AUTO, ddlAutoProp); return properties; } //..}

properties.put(DIALECT, MySQLDialect.class.getName()); properties.put(HBM2DDL_AUTO, ddlAutoProp);

Page 22: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

스프링�부트�JPA�설정

1. 의존성�추가 spring-boot-starter-data-jpa�

2. 환경변수�추가spring.jpa.show-sql:�truespring.jpa.hibernate.ddl-auto:�create-drop

Page 23: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

반복적인�설정�부분�제거��의존성과�환경변수�추가

Page 24: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

그리고�소스

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

Page 25: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

설정�부분�해결?!

Page 26: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

설정�클래스는�다�어디�갔을까?

Page 27: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

그�내부를�들여다�보자

Page 28: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

Into�the�스프링�부트

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

Page 29: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

Into�the�스프링�부트

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

Page 30: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

Into�the�스프링�부트

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

스프링�컨테이너를�실행

Page 31: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

Into�the�스프링�부트

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

스프링�컨테이너를�실행

Page 32: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

Into�the�스프링�부트

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

스프링�컨테이너를�실행부트�단순함을�위한�어노테이션�

Page 33: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

Into�the�@SpringBootApplication

@Configuration@EnableAutoConfiguration@ComponentScanpublic @interface SpringBootApplication { … }

Page 34: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

Into�the�@SpringBootApplication

@Configuration@EnableAutoConfiguration@ComponentScanpublic @interface SpringBootApplication { … }

Page 35: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

Into�the�@SpringBootApplication

@Configuration@EnableAutoConfiguration@ComponentScanpublic @interface SpringBootApplication { … } @EnableAutoConfiguration�

스프링부트�마법의�실마리

Page 36: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

About�@EnableAutoConfiguration

•특정�기준의�설정클래스를�로드�

•@Enable*�모듈화된�설정의�일종

Page 37: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

• JavaConfig에서�모듈화된�설정시�사용�(�스프링�3.1�부터�지원�)��

• 기존�XML�커스텀태그(<mvc:*/>,<context:*/>�…)와�대응�

• 쉽게�자신의�설정�모듈을�작성�가능 (�@Import�어노테이션�사용�)

@Enable*�어노테이션이란?

@EnableWebMvc @EnableAspectJAutoProxy … @Configuration public class ApplicationConfiguration { … }

Page 38: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

Into�the�@EnableAutoConfiguration�

@AutoConfigurationPackage @Import(EnableAutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration { … }

Page 39: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

Into�the�@EnableAutoConfiguration�

@AutoConfigurationPackage @Import(EnableAutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration { … }

Page 40: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

Into�the�@EnableAutoConfiguration�

@AutoConfigurationPackage @Import(EnableAutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration { … }

스프링부트�자동�설정�기능담당

Page 41: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

Into�the�EnableAutoConfigurationImportSelector�1

public class EnableAutoConfigurationImportSelector implements DeferredImportSelector, … { @Override public String[] selectImports(AnnotationMetadata metadata) { … }}

Page 42: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

Into�the�EnableAutoConfigurationImportSelector�1

public class EnableAutoConfigurationImportSelector implements DeferredImportSelector, … { @Override public String[] selectImports(AnnotationMetadata metadata) { … }}

Page 43: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

Into�the�EnableAutoConfigurationImportSelector�1

public class EnableAutoConfigurationImportSelector implements DeferredImportSelector, … { @Override public String[] selectImports(AnnotationMetadata metadata) { … }}

설정�클래스(@Configuration)�리스트�받아서�활성화

Page 44: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

Into�the�EnableAutoConfigurationImportSelector�1

public class EnableAutoConfigurationImportSelector implements DeferredImportSelector, … { @Override public String[] selectImports(AnnotationMetadata metadata) { … }}

설정�클래스(@Configuration)�리스트�받아서�활성화

Page 45: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

Into�the�EnableAutoConfigurationImportSelector�1

public class EnableAutoConfigurationImportSelector implements DeferredImportSelector, … { @Override public String[] selectImports(AnnotationMetadata metadata) { … }}

설정�클래스(@Configuration)�리스트�받아서�활성화

설정�클래스�리스트�(패키지명�포함�클래스명)

Page 46: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

Into�the�EnableAutoConfigurationImportSelector�2

Page 47: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

…protected List<String> getCandidateConfigurations(…) {

return SpringFactoriesLoader.loadFactoryNames( getSpringFactoriesLoaderFactoryClass(),

getBeanClassLoader()); }

…}

Into�the�EnableAutoConfigurationImportSelector�2

Page 48: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

…protected List<String> getCandidateConfigurations(…) {

return SpringFactoriesLoader.loadFactoryNames( getSpringFactoriesLoaderFactoryClass(),

getBeanClassLoader()); }

…}

Into�the�EnableAutoConfigurationImportSelector�2

Page 49: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

…protected List<String> getCandidateConfigurations(…) {

return SpringFactoriesLoader.loadFactoryNames( getSpringFactoriesLoaderFactoryClass(),

getBeanClassLoader()); }

…}Classpath의�모든�라이브러리의�“META-INF/spring.factories“�위치의�파일에서�설정파일�리스트�읽어온다.

Into�the�EnableAutoConfigurationImportSelector�2

Page 50: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

Into�the�META-INF/spring.factories�

•Key=Value�형태로�설정클래스�리스트�기록�

•스프링부트의�기본�설정�정보는�아래의�라이브러리�안에�포함o.s.boot:spring-boot-autoconfigure:x.x.x.jar

# Auto Configureorg.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\ … org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\ … org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration,\ org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\ …

Page 51: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

중간�정리

• 스프링부트의�설정의�시작@EnableAutoConfiguration�

• 설정리스트의�보관소�“META-INF/spring.factories”�

• 모든�설정클래스를�로딩!?

Page 52: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

스프링부트는�지정된�모든�설정클래스가��

스프링컨테이너에�올라가는가?

Page 53: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

당연히�NO!�

Page 54: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

@Conditional

Page 55: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

@Conditional은��어노테이션통한�조건적으로�Bean을�등록

Page 56: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

@Conditional�

•스프링�4에서�도입된�어노테이션�

•조건부로�Bean을�(스프링컨테이너)에�등록

Page 57: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

@Conditional�

@Bean @Conditional(PropertiesCondition.class) public CommandLineRunner propertiesConditional() { return (args) -> { System.out.println("test.hasValue 환경변수가 있으면 보입니다"); }; }

Page 58: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

@Conditional�

@Bean @Conditional(PropertiesCondition.class) public CommandLineRunner propertiesConditional() { return (args) -> { System.out.println("test.hasValue 환경변수가 있으면 보입니다"); }; }

Page 59: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

@Conditional�

@Bean @Conditional(PropertiesCondition.class) public CommandLineRunner propertiesConditional() { return (args) -> { System.out.println("test.hasValue 환경변수가 있으면 보입니다"); }; }

Page 60: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

@Conditional�

@Bean @Conditional(PropertiesCondition.class) public CommandLineRunner propertiesConditional() { return (args) -> { System.out.println("test.hasValue 환경변수가 있으면 보입니다"); }; }

class PropertiesCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { return context.getEnvironment() .containsProperty("test.hasValue"); } }

Page 61: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

@Conditional�

@Bean @Conditional(PropertiesCondition.class) public CommandLineRunner propertiesConditional() { return (args) -> { System.out.println("test.hasValue 환경변수가 있으면 보입니다"); }; }

class PropertiesCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { return context.getEnvironment() .containsProperty("test.hasValue"); } }

Page 62: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

@Conditional�

@Bean @Conditional(PropertiesCondition.class) public CommandLineRunner propertiesConditional() { return (args) -> { System.out.println("test.hasValue 환경변수가 있으면 보입니다"); }; }

class PropertiesCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { return context.getEnvironment() .containsProperty("test.hasValue"); } }

true이면�Bean이�등록

Page 63: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

@Profile�

Page 64: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

@Profile

@Profile("dev") @Configuration class HelloProfileDevConfig { @Bean public HelloWorld helloDevWorld() { return new HelloWorld("dev"); } } @Profile("test") @Configuration class HelloProfileTestConfig { @Bean public HelloWorld helloTestWorld() { return new HelloWorld("test"); } }

Page 65: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

@Profile

@Profile("dev") @Configuration class HelloProfileDevConfig { @Bean public HelloWorld helloDevWorld() { return new HelloWorld("dev"); } } @Profile("test") @Configuration class HelloProfileTestConfig { @Bean public HelloWorld helloTestWorld() { return new HelloWorld("test"); } }

Page 66: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

@Profile

@Profile("dev") @Configuration class HelloProfileDevConfig { @Bean public HelloWorld helloDevWorld() { return new HelloWorld("dev"); } } @Profile("test") @Configuration class HelloProfileTestConfig { @Bean public HelloWorld helloTestWorld() { return new HelloWorld("test"); } }

@Profile("dev")

Page 67: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

@Profile

@Profile("dev") @Configuration class HelloProfileDevConfig { @Bean public HelloWorld helloDevWorld() { return new HelloWorld("dev"); } } @Profile("test") @Configuration class HelloProfileTestConfig { @Bean public HelloWorld helloTestWorld() { return new HelloWorld("test"); } }

@Profile("test")

@Profile("dev")

Page 68: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

Into�@Profile

Page 69: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

Into�@Profile

@Conditional(ProfileCondition.class) public @interface Profile { String[] value();

}

Page 70: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

Into�@Profile

@Conditional(ProfileCondition.class) public @interface Profile { String[] value();

}

Page 71: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

Into�@Profile

@Conditional(ProfileCondition.class) public @interface Profile { String[] value();

}@Conditional�를�통해�Profile이�구현

Page 72: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

@Conditional�예제

Page 73: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

@Conditional�확장�1

@ConditionalOnClass

@ConditionalOnProperties

@ConditionalOnBean @ConditionalOnMissingBean

@ConditionalOnMissingClass

@ConditionalOnResources

Page 74: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

@Conditional�확장�1

@ConditionalOnClass

@ConditionalOnProperties

@ConditionalOnBean @ConditionalOnMissingBean

@ConditionalOnMissingClass

@ConditionalOnResources

스프링�컨테이너의�Bean�존재유무

Page 75: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

@Conditional�확장�1

@ConditionalOnClass

@ConditionalOnProperties

@ConditionalOnBean @ConditionalOnMissingBean

@ConditionalOnMissingClass

@ConditionalOnResources

스프링�컨테이너의�Bean�존재유무

classpath에서�클래스의�존재유무

Page 76: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

@Conditional�확장�1

@ConditionalOnClass

@ConditionalOnProperties

@ConditionalOnBean @ConditionalOnMissingBean

@ConditionalOnMissingClass

@ConditionalOnResources

스프링�컨테이너의�Bean�존재유무

classpath에서�클래스의�존재유무

환경�변수의�유무

Page 77: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

@Conditional�확장�1

@ConditionalOnClass

@ConditionalOnProperties

@ConditionalOnBean @ConditionalOnMissingBean

@ConditionalOnMissingClass

@ConditionalOnResources

스프링�컨테이너의�Bean�존재유무

classpath에서�클래스의�존재유무

환경�변수의�유무 Resources�존재�

Page 78: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

@Conditional�확장�2

@ConditionalOnJava

@OnJndiCondition

@ConditionalOnWebApplication

@ConditionalOnExpression

@ConditionalOnNotWebApplication

Page 79: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

@Conditional�확장�2

@ConditionalOnJava

@OnJndiCondition

@ConditionalOnWebApplication

@ConditionalOnExpression

현재�프로젝트가�웹애플리케이션인지�여부

@ConditionalOnNotWebApplication

Page 80: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

@Conditional�확장�2

@ConditionalOnJava

@OnJndiCondition

@ConditionalOnWebApplication

@ConditionalOnExpression

현재�프로젝트가�웹애플리케이션인지�여부

JAVA�버전�종류�선택

@ConditionalOnNotWebApplication

Page 81: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

@Conditional�확장�2

@ConditionalOnJava

@OnJndiCondition

@ConditionalOnWebApplication

@ConditionalOnExpression

현재�프로젝트가�웹애플리케이션인지�여부

JAVA�버전�종류�선택

@ConditionalOnNotWebApplication

SPEL의�true�false�여부

Page 82: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

@Conditional�확장�2

@ConditionalOnJava

@OnJndiCondition

@ConditionalOnWebApplication

@ConditionalOnExpression

현재�프로젝트가�웹애플리케이션인지�여부

JAVA�버전�종류�선택

JNDI�lookup�가능�여부

@ConditionalOnNotWebApplication

SPEL의�true�false�여부

Page 83: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

스프링부트는��@Conditional*을�통한�Autoconfiguration�

구현

Page 84: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

스프링부트�설정

MVC�설정

Security�설정

JDBC�설정

JPA�설정

AOP�설정

Page 85: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

스프링부트�설정

MVC�설정

Security�설정

JDBC�설정

JPA�설정

AOP�설정

서블릿�인터페이스�있는가?�이미�MVC�설정하는게�있는가?

Page 86: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

스프링부트�설정

MVC�설정

Security�설정

JDBC�설정

JPA�설정

AOP�설정

서블릿�인터페이스�있는가?�이미�MVC�설정하는게�있는가?

스프링�시큐리티�라이브러리가�있는가?�등등

Page 87: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

스프링부트�설정

MVC�설정

Security�설정

JDBC�설정

JPA�설정

AOP�설정

서블릿�인터페이스�있는가?�이미�MVC�설정하는게�있는가?

스프링�시큐리티�라이브러리가�있는가?�등등

DataSource�객체가�스프링�컨테이너에�존재하는가?�등

Page 88: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

스프링부트�설정

MVC�설정

Security�설정

JDBC�설정

JPA�설정

AOP�설정

서블릿�인터페이스�있는가?�이미�MVC�설정하는게�있는가?

스프링�시큐리티�라이브러리가�있는가?�등등

DataSource�객체가�스프링�컨테이너에�존재하는가?�등

JPA,�하이버네이트�라이브리가�있는가?�DataSource�설정이�이미되었

는가?

Page 89: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

스프링부트�설정

MVC�설정

Security�설정

JDBC�설정

JPA�설정

AOP�설정

서블릿�인터페이스�있는가?�이미�MVC�설정하는게�있는가?

스프링�시큐리티�라이브러리가�있는가?�등등

DataSource�객체가�스프링�컨테이너에�존재하는가?�등

JPA,�하이버네이트�라이브리가�있는가?�DataSource�설정이�이미되었

는가?

스프링�AOP,�AspectJ�라이브러리가�존재하는가?�

Page 90: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

즉�spring.factories�의��설정�클래스는��

조건(@Conditional)에�따라�컨테이너에�등록

Page 91: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

Autoconfiguration�지원�기술

• CoreSpring�(Security,�AOP,�Session,�Cache,�DevTools�…)�Atomikos,�…�

• WebSpring�(MVC,�Websocket,�Rest�Docs),�WS,�Jersey,�…�

• Template�Engines Freemarker,�Velocity,�Thymeleaf,�Mustache,�…�

• SQL�JPA,�JOOQ,�JDBC,�H2,�HSQLDB,�Derby,�MySQL,�PostreSQL�

• NoSQL,�Cloud,�Social,�I/O,�Ops,�…

Page 92: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

커스텀�모듈�만들기�(소스)

Page 93: 스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration

감사합니다!