æ¬è¨äºã¯ãSpring Boot 2.7.3ã«ã¤ãã¦æ¸ãã¦ããã¾ãã
Spring Bootã¨ã¯
Spring Bootã¯ãã¬ã¼ã ã¯ã¼ã¯ã§ãªããSpring Frameworkãå©ç¨ãã¦ã¢ããªã±ã¼ã·ã§ã³ãä½ãããããã¦ããä»çµã¿ã®ãã¨ã Spring Bootåä½ã§ã¯ä½ãä½ããã¨ã¯ã§ããªãã®ã§ãSpring WebãSpring Dataãªã©ã®ãã¬ã¼ã ã¯ã¼ã¯ãå©ç¨ãããï¼Spring Bootã§ã¯Starterãå©ç¨ãããã¨ãã»ã¨ãã©ï¼
Spring Bootã¨éSpring Bootã®å¤§ããªéãã¯ãStarterã¨ããä¾åé¢ä¿ä¾¿å©ããã¯ãæä¾ããã¦ãã&ããã©ã«ãã®è¨å®ï¼å¿ è¦ãããªã¯ã©ã¹ã®Beanç»é²ï¼ãããã¦ãããã¨ã
Starters
ã¢ããªã±ã¼ã·ã§ã³ãä½ãã¨ãã«ãã£ããå¿
è¦ã§ãããä¾åé¢ä¿ãå«ãã 便å©ããã¯ãä¾ãã°ãSpringã¨JPAãå©ç¨ãã¦ã¢ããªã±ã¼ã·ã§ã³ãä½ãããã¨ãªã£ãããspring-boot-starter-data-jpa
ãä¾åé¢ä¿ã«è¿½å ããã°ããã
Starterãå©ç¨ããã¨å¾è¿°ããauto-configurationã®ä»çµã¿ãæä¾ããã¦ãããåã«ã©ã¤ãã©ãªãä¾åé¢ä¿ã«è¿½å ãããããè¨å®é
ç®ãæ¸ãã楽ã«ã¢ããªã±ã¼ã·ã§ã³éçºãè¡ããã
â» å
¬å¼ã®ã¹ã¿ã¼ã¿ã¼ã¯spring-boot-starter-*
ã¨ããååã«ãªã£ã¦ããå¤ãã®IDEã§ããã«é©ããè¨å®ãå
¥ã£ã¦ããããããµã¼ããã¼ãã£ã®ã¹ã¿ã¼ã¿ã¼ã¯é ã«spring-boot
ã¨ããååãã¤ãã¦ã¯ãããªãã
Starterã®ä¾
- Spring Boot application starters
- Spring Bootã§ã¢ããªã±ã¼ã·ã§ã³ãä½ãããã«å©ç¨
spring-boot-starter-data-jpa
ãspring-boot-starter-web
ãªã©
- Spring Boot production starters
- production readyãªæ©è½ãæä¾ãã
spring-boot-starter-actuator
- Spring Boot technical starters
- ããã©ã«ãã§å©ç¨ããã¦ããã©ã¤ãã©ãªçãç½®ãæãããé¤å¤ãããããã¨ãã«å©ç¨
spring-boot-starter-logging
ãspring-boot-starter-tomcat
ãªã©
ã³ã¼ãã£ã³ã°
Spring Bootã¢ããªã±ã¼ã·ã§ã³ã«ãããã¨ã³ããªã¼ãã¤ã³ãã¯@SpringBootApplication
ã§ããããã¡ã¤ã³ã¯ã©ã¹ã«ä»ä¸ããã
ãªãã@SpringBootApplication
ã¯ããã±ã¼ã¸æä¸ä½ã«ç½®ãã®ãããã
ä¸è¬çãªã¡ã¤ã³ã¯ã©ã¹ã
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
è¨å®ã¯ã©ã¹
Spring Bootã®è¨å®ã«ã¯Javaãã¼ã¹ã®è¨å®ãæ¨å¥¨ãã¦ãããXMLãå©ç¨ãããã¨ãã§ããã
Auto-configuration
Spring Boot auto-configurationã¯ãããã¸ã§ã¯ãã«è¿½å ããã¦ããä¾åé¢ä¿ã«åºã¥ãã¦ãSpring ã¢ããªã±ã¼ã·ã§ã³ã«èªåçã«è¨å®ãè¡ãä»çµã¿ã®ãã¨ã§ããã
@EnableAutoConfiguration
ã¾ãã¯@SpringBootApplication
ãä»ä¸ããã¨ãauto-configurationãæå¹ã«ãããã¨ãã§ããã
ããã©ã«ãã§å ¨AutoConfigurationãé©ç¨ããããã以ä¸ã®ããã«æ¸ãã¨auto-configurationããé¤å¤ãããã¨ãã§ããã
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }) public class Application { }
@SpringBootApplicationã«ã¤ãã¦
@SpringBootApplication
ã¯ã@SpringBootConfiguration
ã@EnableAutoConfiguration
ã@ComponentScan
ãå«ã¿æã¤ã¢ãã¼ãã¼ã·ã§ã³ã§ãããã¨åãæ©è½ãæä¾ãããã¡ãªã¿ã«ãè¤æ°ã®æ©è½ãæã¤ã¢ããã¼ã·ã§ã³ã®ãã¨ãè¤åã¢ããã¼ã·ã§ã³ã¨å¼ã¶ã
@EnableAutoConfiguration
- å ã«èª¬æããauto-configurationãæå¹ã«ãã
@ComponentScan
- ãããä»ä¸ãããã¯ã©ã¹ã®é
ä¸ã«ããã¯ã©ã¹ã®
@Component
ç³»ã®ã¢ããã¼ã·ã§ã³ï¼@RestController
ã@Service
ãªã©ï¼ãä»ä¸ããã¦ããã¯ã©ã¹ãBeanç»é²ããã - ããã¯Spring Bootã®ã¢ããã¼ã·ã§ã³ã§ãªããSpring Frameworkã®ã¢ããã¼ã·ã§ã³
- ãããä»ä¸ãããã¯ã©ã¹ã®é
ä¸ã«ããã¯ã©ã¹ã®
@SpringBootConfiguration
- ãã®ã¯ã©ã¹èªèº«ãBeanç»é²ããã
@Configuration
ã¨ã»ã¼åãæ©è½ã§ã¯ããããã¤ã³ãã°ã¬ã¼ã·ã§ã³ãã¹ããæ¸ãéã«å½¹ç«ã¤ã
Auto configuration
AutoConfigurationã®ã¯ã©ã¹ã«@AutoConfiguration
ãä»ä¸ããã¨Beanç»é²ãããã
auto-configurationãé©ç¨ãããã¯ã©ã¹ã¯META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
ï¼Spring Boot 2.7ããåã¯META-INF/spring.factoriesã ã£ãï¼ã«åæããã¦ããã
AutoConfigurationImportSelector#getCandidateConfigurationsãåç §ã
Condition Annotations
auto-configurationã¯ã©ã¹ã«æ¸ãã¦ãã@Conditonal
ã«å¾ã£ã¦Beanç»é²ãè¡ãããã
@Conditional
ã®ä»²éã«ã¯æ¬¡ã®ãã®ãããã
Class Condition
ã¯ã©ã¹ãã¹ã«æå®ããã¯ã©ã¹ãããããªããã@ConditionalOnClass
ã@ConditionalOnMissingClass
ã
JacksonAutoConfiguration.javaã ã¨ãObjectMapper.javaãã¯ã©ã¹ãã¹ã«åå¨ãããauto-configurationãé©ç¨ãããã
@AutoConfiguration @ConditionalOnClass(ObjectMapper.class) public class JacksonAutoConfiguration {
Bean Conditions
æå®ããBeanãããããªããã@ConditionalOnBean
ã@ConditionalOnMissingBean
ã
RestTemplateAutoConfiguration#restTemplateBuilderConfigurerã ã¨ãRestTemplateBuilderConfigurerãBeanç»é²ããã¦ãªãã£ãããã®ã¡ã½ããã§RestTemplateBuilderConfigurerã®Beanç»é²ãè¡ãããã
@Bean @Lazy @ConditionalOnMissingBean public RestTemplateBuilderConfigurer restTemplateBuilderConfigurer(
Property Conditions
application.ymlãèµ·åãªãã·ã§ã³ãªã©ã§æå®ããSpring ç°å¢ããããã£ã«ãã£ã¦Beanç»é²ãè¡ãããã
JacksonHttpMessageConvertersConfiguration.MappingJackson2HttpMessageConverterConfiguration.javaã ã¨ãspring.mvc.converters.preferred-json-mapper=jackson
ã®ã¨ãBeanç»é²ãè¡ããã
@Configuration(proxyBeanMethods = false) @ConditionalOnClass(ObjectMapper.class) @ConditionalOnBean(ObjectMapper.class) @ConditionalOnProperty(name = HttpMessageConvertersAutoConfiguration.PREFERRED_MAPPER_PROPERTY, havingValue = "jackson", matchIfMissing = true) static class MappingJackson2HttpMessageConverterConfiguration {
Resource Conditions
æå®ãããªã½ã¼ã¹ãåå¨ãããBeanç»é²ããã@ConditionalOnResource
ã
ãã®ããã«resourcesã«ãã¹ãæå®ãããï¼ProjectInfoAutoConfiguration#buildPropertiesï¼
@ConditionalOnResource(resources = "${spring.info.build.location:classpath:META-INF/build-info.properties}")
Web Application Conditions
Webã¢ããªã±ã¼ã·ã§ã³ãã©ããã@ConditonalOnWebApplication
ã@ConditionalOnNotWebAplication
ã
ãã®ä»ãã¯ã©ã¦ããã©ãããã©ã¼ã ãã©ããã§å¤å®ãã@ConditionalOnCloudPlatform
ãªã©ããããCloudPlatform.javaã«å®ç¾©ããã¦ãããCloudFoundryãHerokuãSAPãKubernetesãAzure App Serviceã対象ã
SpEL Expression Conditions
@ConditionalOnExpression
ã¯SpEL(SPring Expression Language)ã§æ¸ãããããããã£ã«å¿ãã¦Beanç»é²ããã
Spring Boot ãèµ·åããã¾ã§ã«ãã£ã¦ãããã¨
@EnableAutoConfiguraion(@SpringBootApplication)
ãè¡ã£ã¦ãããã¨ãããSpring Bootã®ç¹å¾´ã§ããAutoConfigureã®ä»çµã¿ã説æããã
1. DIã³ã³ããï¼ApplicationContextï¼ã®ä½æ
ApplicationContextã¯ãBeanFactoryãç¶æ¿ããã¢ããªã±ã¼ã·ã§ã³ã®è¨å®ãæä¾ãã主è¦ãªã¤ã³ã¿ã¼ãã§ã¼ã¹ãã¡ã¤ã³æ©è½ã®1ã¤ã«DIã³ã³ãããããã
2. spring.factoriesãAutoConfiguration.importsèªè¾¼
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
ã«auto-configureããã¯ã©ã¹ï¼@AutoConfigure
ãã¤ããã¯ã©ã¹ï¼ãåæããã
以åã¯spring.factoriesã«æ¸ãã¦ãããããã®ãã¡ã¤ã«ã¯auto-configureãå©ç¨ããããã«å¿
è¦ãªã¯ã©ã¹ãæ¸ãããã«ãªã£ãï¼ã½ãï¼
ãããã«æ¸ãããã¯ã©ã¹ãèªã¿è¾¼ãã
3. Beanç»é²ããAutoConfigurationã¯ã©ã¹ãã½ã¼ã
å®ç¾©ãããé åºã§ãBeanç»é²ããAutoConfigurationã¯ã©ã¹ãã½ã¼ããããAutoConfigurationSorter#getInPriorityOrder
ã§è¡ãã
- ã¢ã«ãã¡ãããé
@Order
ã®é @AutoConfigureBefore
ã¨@AutoConfigureAfter
ã®é @AutoConfigureBefore
ã¯æå®ããã¯ã©ã¹ã®å@AutoConfigureAfter
ã¯æå®ããã¯ã©ã¹ã®å¾
4. Beanç»é²
ConfigurationClassParser#processGroupImportsã§DIã³ã³ããã«Beanç»é²ãè¡ã
application.properties(yml)ã«ã¤ãã¦
application.propterties(yml)ã¯ãè¨å®å¤ãæ¸ããã¡ã¤ã«ã Spring Bootãæä¾ãã¦ããè¨å®å¤ã®ä¸è¦§ã¯ããã«æ¸ãã¦ããã
ãã®ãã¡ã¤ã«ã«æ¸ãã¦ã確å®ã«è¨å®ãããããã§ã¯ãªãã@ConfigurationProperties
ãä»ä¸ãããã¯ã©ã¹ã«å¤ããã¤ã³ããããã ãã§ããã
è¨å®ãè¡ãããæ¡ä»¶ã¯ã次ã®2ã¤ãæºãããã¦ãããã¨ã§ãã
- ãã¤ã³ãããã@ConfigurationPropertiesã®ã¯ã©ã¹ãBeanç»é²ããã¦ãããã¨
- ãã®AutoConfigurationãæå¹ã¨ãªã£ã¦ããã¨
大æµã@AutoConfiguration
ãä»ä¸ããã¯ã©ã¹ã«@EnableConfigurationProperties
ãä»ä¸ããã¦ãããã¤ã¾ããAutoConfigurationãæå¹ã«ãªã£ã¦ããã°application.properties(yml)ã«æ¸ããè¨å®ã¯é©ç¨ãããã
ããã¯DataSourceAutoConfiguration.javaã®ä¾ã§ããã
@AutoConfiguration(before = SqlInitializationAutoConfiguration.class) @ConditionalOnClass({ DataSource.class, EmbeddedDatabaseType.class }) @ConditionalOnMissingBean(type = "io.r2dbc.spi.ConnectionFactory") @EnableConfigurationProperties(DataSourceProperties.class) @Import(DataSourcePoolMetadataProvidersConfiguration.class) public class DataSourceAutoConfiguration {
Banner
Bannerã¨ã¯ãSpring Bootãèµ·åããã¨è¦ãããã®ãã¨ã§å°å³ã«å¤æ´ãããç¶ãã¦ããã
æ´å²
ãã¼ã¸ã§ã³ | å 容 |
---|---|
1.0.x | Bannerã表示ãããã©ããã®è¨å®ã®ã¿ |
1.1.x | Text Bannerã表示ã§ããããã«ãªã |
1.2.x | Bannerå¤æ°ãç»å ´ |
1.3.x | Text Bannerãè²ä»ãå¯è½ã« |
1.4.x | Image Bannerã表示å¯è½ã« |
2.0.x | ã¢ãã¡ã¼ã·ã§ã³gifããµãã¼ã |
2.2.x | ASCII ããã¼ã¯256è²å©ç¨å¯è½ã« |
ã«ã¹ã¿ãã¤ãº
ããã¼ã®ã«ã¹ã¿ãã¤ãºæ¹æ³ã¯3éãã§ãããã©ã«ãããã¼ã¯3ã§å®ç¾ãã¦ãããããSpringBootBanner.javaãå®è£ ã
- resources/banner.[ gif | jpg | png ]ãç½®ã
- resources/banner.txtãç½®ã
- org.springframework.boot.Bannerã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£ ãã
åªå é ä½
SpringApplicationBannerPrinter#getBannerã§è¡¨ç¤ºãããã¼ã決å®ãã¦ããã
- Image Bannerã¨Text Bannerãæ¢ã
- 1ããªãã£ããèªä½ã®Bannerã¯ã©ã¹
- 1ã2ããªãã£ããDefault Banner
Image Banner 㨠Text Bannerã¯ä¸ç·ã«è¡¨ç¤ºå¯è½ã
Bannerã¤ã³ã¿ã¼ãã§ã¼ã¹å®è£ ãã¦ã¿ã
public class StoresBanner implements Banner { private static final String[] BANNER = { " ______ _________ ___ _______ ________ ______ \\n" + ".' ____ \\\\ | _ _ | .' `. |_ __ \\\\ |_ __ | .' ____ \\\\ \\n" + "| (___ \\\\_| |_/ | | \\\\_| / .-. \\\\ | |__) | | |_ \\\\_| | (___ \\\\_| \\n" + " _.____`. | | | | | | | __ / | _| _ _.____`. \\n" + "| \\\\____) | _| |_ \\\\ `-' / _| | \\\\ \\\\_ _| |__/ | | \\\\____) | \\n" + " \\\\______.' |_____| `.___.' |____| |___| |________| \\\\______.' \\n" }; @Override public void printBanner(Environment environment, Class<?> sourceClass, PrintStream printStream) { for (String line : BANNER) { printStream.println(line); } } }
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication app = new SpringApplication(Application11.class); app.setBanner(new StoresBanner()); app.run(args); } }
â» ããã¼ã好ãã ã¨æããã人ãä½ã£ãOnline Spring Boot Banner Generatorã¨ãããµã¤ããããã