Spring Boot ã§ã¡ã¼ã«éä¿¡ãã Web ã¢ããªã±ã¼ã·ã§ã³ãä½ã ( çªå¤ç·¨ )( DataSource Bean ã« application.properties ã®è¨å®ãåæ ãããä»çµã¿ã¨ã¯ï¼ )
ã¯ããã«
DataSource Bean 㧠DataSourceBuilder.create().build()
ãå¼ã³åºãã¦ã¤ã³ã¹ã¿ã³ã¹ãçæããæã« application.properties ã®è¨å®ãåæ ããã¦ãããã®ã¨æã£ã¦ããã®ã§ããããã®æç¹ã§ã¯ã¾ã åæ ããã¦ãã¾ããã§ãããã©ã®ãããªä»çµã¿ã§åæ ããã¦ããã®ãèå³ã湧ããã®ã§èª¿ã¹ã¦ã¿ã¾ãã
ç¾å¨ç¢ºèªã§ãã¦ãããã¨ã¯ï¼
DataSource Bean å
㧠DataSourceBuilder.create().build()
㧠DataSource ã®ã¤ã³ã¹ã¿ã³ã¹ãçæããæã«ã¯ application.properties ã®è¨å®ã¯åæ ããã¦ããªãã®ã§ãããController ã¯ã©ã¹ç㧠@Autowired ã¢ããã¼ã·ã§ã³ã§ DI ããæã«ã¯ dataSource ã« application.properties ã®è¨å®ãåæ ããã¦ãã¾ãã
src/main/java/ksbysample/webapp/email/config ã®ä¸ã® ApplicationConfig.java ã以ä¸ã®ããã«å¤æ´ããå¾ã
@Configuration public class ApplicationConfig { @Bean @ConfigurationProperties("spring.datasource") public DataSource dataSource() { org.apache.tomcat.jdbc.pool.DataSource dataSource = (org.apache.tomcat.jdbc.pool.DataSource)DataSourceBuilder.create().build(); System.out.println("â â â " + dataSource.getDriverClassName()); System.out.println("â â â " + dataSource.getUrl()); return dataSource; } }
bootRun ã¿ã¹ã¯ãå®è¡ã㦠Tomcat ãèµ·åããã¨ããã®æç¹ã§ã¯ dataSource ã« application.properties ã®è¨å®ãåæ ããã¦ããã null ã¨åºåããããã¨ã確èªã§ãã¾ãã
次ã«ãå¤æ´ããå 容ãä¸æ¦å ã«æ»ãã¦ãã src/main/java/ksbysample/webapp/email/web/mailsend ã®ä¸ã® MailsendController.java ã以ä¸ã®ããã«å¤æ´ããå¾ã
@Autowired private DataSource dataSource; @RequestMapping public String index(MailsendForm mailsendForm , Model model) { org.apache.tomcat.jdbc.pool.DataSource dataSource = (org.apache.tomcat.jdbc.pool.DataSource)this.dataSource; System.out.println("â â â " + dataSource.getDriverClassName()); System.out.println("â â â " + dataSource.getUrl()); return "mailsend/mailsend"; }
Tomcat ãåèµ·åãã¦ãã©ã¦ã¶ã§ http://localhost:8080/mailsend ã¸ã¢ã¯ã»ã¹ãã㨠dataSource ã« application.properties ã®è¨å®ãåæ ããã¦ãããã¨ã確èªã§ãã¾ãã
å¤æ´ããå 容ãå ã«æ»ãã¦ããã¾ãã
調æ»ãã®ï¼ ( DataSource Bean ã®èªåã³ã³ãã£ã®ã¥ã¬ã¼ã·ã§ã³ãè¡ãã¯ã©ã¹ãããã®ãï¼ )
ã¾ã㯠Spring Boot ã® GitHub 㧠DataSource Bean ã®èªåã³ã³ãã£ã®ã¥ã¬ã¼ã·ã§ã³ãè¡ãã¯ã©ã¹ããããæ¤ç´¢ãã¦ã¿ã¾ããhttps://github.com/spring-projects/spring-boot ã«ã¢ã¯ã»ã¹ããå¾ã"dataSource autoconfigure" ã§æ¤ç´¢ãã¦ãããããã½ã¼ã¹ä¸è¦§ãçºãã¦ãã㨠DataSourceAutoConfiguration.java ã¨ããããã«ããããããã¯ã©ã¹ãããã¾ããã
DataSourceAutoConfiguration.java ãã²ã¨éãè¦ã¦ã¿ãææ³ã¨ãã¦ã¯ã
- SpringBootCondition ãç¶æ¿ããã¤ã³ãã¼ã¯ã©ã¹ã EmbeddedDataSourceCondition, NonEmbeddedDataSourceCondition, DataSourceAvailableCondition ã®ï¼ã¤åå¨ãã¾ãã
- SpringBootCondition ãç¶æ¿ããã¯ã©ã¹ã® getMatchOutcome ã¡ã½ããã§
return ConditionOutcome.match(...);
ãè¿ãã¨ãå¥ã®ã¯ã©ã¹ã§ @Conditional ã¢ããã¼ã·ã§ã³ãä»å ãã¦ãããã¨ã§ã¯ã©ã¹ã使ç¨ãããå¦ãã®å¤ææ¡ä»¶ã¨ãã¦ä½¿ç¨ã§ããããã§ããä¾ãã° DataSourceAvailableCondition ã¤ã³ãã¼ã¯ã©ã¹ã® getMatchOutcome ã¡ã½ãããreturn ConditionOutcome.match(...);
ãè¿ãå ´åã«ã¯ã@Conditional(DataSourceAutoConfiguration.DataSourceAvailableCondition.class)
ãä»å ããã JdbcTemplateConfiguration ã¯ã©ã¹ã使ç¨å¯è½ã«ãªããã¨ããæãã§ãã - EmbeddedDataSourceCondition ã¯ã¯ã©ã¹ãã¼ãã¼ã« H2 Database, Java DB(Derby), HSQLDB ã®ããããã®ãã©ã¤ããããã¨
return ConditionOutcome.match(...);
ãè¿ãã¾ãã - NonEmbeddedDataSourceCondition ã¯ã¯ã©ã¹ãã¼ãã¼ã« org.apache.tomcat.jdbc.pool.DataSource, com.zaxxer.hikari.HikariDataSource, org.apache.commons.dbcp.BasicDataSource, org.apache.commons.dbcp2.BasicDataSource ã®ãããããåå¨ããã¨
return ConditionOutcome.match(...);
ãè¿ãã¾ããDataSourceBuilder.java ãåç §ãã¾ããã - NonEmbeddedDataSourceCondition ãæå¹ã§ãDataSource ã XADataSource ãè¿ã Bean ãåå¨ããªãå ´åãNonEmbeddedConfiguration ã¯ã©ã¹ã§ DataSource Bean ãçæãã¾ãããã® DataSource Bean ã®å®è£ ãè¦ãæãã ã¨ãã¢ããªã±ã¼ã·ã§ã³ã§ DataSource Bean ãå®ç¾©ããå¿ è¦ã¯ãªãæ°ããã¾ãããããããã¨ã§è©¦ãã¦ã¿ã¾ãããã
- DataSourceAvailableCondition 㯠DataSource ã XADataSource ãè¿ã Bean ãåå¨ããã㤠EmbeddedDataSourceCondition ã NonEmbeddedDataSourceCondition ã®ãããããæå¹ãªå ´åã«ã¯
return ConditionOutcome.match(...);
ãè¿ãã¾ããDataSource ãè¿ã Bean ã¯ã¢ããªã±ã¼ã·ã§ã³ã§å®ç¾©ãã¦ããªãã¦ã NonEmbeddedConfiguration ã¯ã©ã¹ã§ DataSource Bean ãèªåçæãããå ´åã«ã¯return ConditionOutcome.match(...);
ãè¿ãã¦ãã¾ãããApplicationConfig ã¯ã©ã¹ã® DataSource Bean ãã³ã¡ã³ãã¢ã¦ããã¦ç¢ºèªãã¾ããã - DataSourceAutoConfiguration.java ã®ä¸ã¯ @Conditional ã¢ããã¼ã·ã§ã³ã @ConditionalOnMissingBean ã¢ããã¼ã·ã§ã³ã§ã©ã®ã¤ã³ãã¼ã¯ã©ã¹ã使ç¨ããã®ããå¤æãã¦ãããã§ãããã¾ãããããªã«ã¢ããã¼ã·ã§ã³ã§ããããæ¡ä»¶å¤æããã¦ããã¨ã¯é©ãã§ããã
- ãã¨ã¯ DataSourceInitializer Bean ãå®ç¾©ããã¦ããªããã°ããã®ã¯ã©ã¹ã®ä¸ã§ DataSourceInitializer Bean ãçæãã¦ãã¾ããã
bootRun ã¿ã¹ã¯ã Debug å®è¡ã㦠NonEmbeddedDataSourceCondition, EmbeddedDataSourceCondition, DataSourceAvailableCondition ãããããä½ãè¿ãã¦ããã確èªãã¦ã¿ã¾ãã
ããããã®ã¤ã³ãã¼ã¯ã©ã¹ã§ return ConditionOutcome.match(...);
ãreturn ConditionOutcome.noMatch(...);
ãè¿ãã¨ããã«ãã¬ã¼ã¯ãã¤ã³ããæå®ããå¾ãDebug å®è¡ãã¾ãã
- NonEmbeddedDataSourceCondition ã¯
return ConditionOutcome.match(...);
ãè¿ãã¦ãã¾ããã - EmbeddedDataSourceCondition ã¯
return ConditionOutcome.noMatch(...);
ãè¿ãã¦ãã¾ããã - DataSourceAvailableCondition ã¯
return ConditionOutcome.match(...);
ãè¿ãã¦ãã¾ããã
ãã®çµæãã使ç¨å¯è½ã¨å¤æããã DataSourceAutoConfiguration.java å ã®ã¤ã³ãã¼ã¯ã©ã¹ã¯ JdbcTemplateConfiguration ã ãã§ãããJdbcTemplateConfigurationãã¤ã³ãã¼ã¯ã©ã¹ã¯ JdbcTemplate BeanãNamedParameterJdbcTemplate Bean ãå®ç¾©ããã ãã§ãDataSource Bean ã«ä½ãå¦çã¯ãã¦ãã¾ããã§ããã
DataSourceAutoConfiguration.java ã§ã¯ DataSource Bean ã« application.properties ã®è¨å®ã¯åæ ãã¦ãã¾ããã§ããã
調æ»ãã®ï¼ ( DataSource Bean ã«ãã¬ã¼ã¯ãã¤ã³ããã»ãããã¦å¦çã追ã£ã¦ã¿ã )
DataSource Bean ã«ãã¬ã¼ã¯ãã¤ã³ããã»ããã㦠Debug ã¢ã¼ã㧠Tomcat ãèµ·åããå¾ããããã¬ãã Step å®è¡ãã¦ã¿ã¾ãã
調æ»ããçµæã以ä¸ã®ãã¨ãåããã¾ããã
- Bean ã®çæ㯠AbstractAutowireCapableBeanFactory ã¯ã©ã¹ã® createBean ã¡ã½ããããè¡ããã¦ãã¾ãã
- createBean ã¡ã½ããããåãã¯ã©ã¹å ã® doCreateBean ã¡ã½ãããå¼ã³åºããã¾ãã
- doCreateBean ã¡ã½ããããåãã¯ã©ã¹å ã® initializeBean ã¡ã½ãããå¼ã³åºããã¾ãã
- initializeBean ã¡ã½ããã®ä¸ã§ã
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
ãå¼ã³åºãããæã«è¨å®ãåæ ããã¦ãã¾ããã - applyBeanPostProcessorsBeforeInitialization ã¡ã½ããã®ä¸ã§
for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) { ... }
ã¨ããå¦çãæ¸ããã¦ããããã®ã«ã¼ãã®ä¸ã§ ConfigurationPropertiesBindingProcessor ã¯ã©ã¹ã® postProcessBeforeInitialization ã¡ã½ãããå¼ã³åºããã¦è¨å®ãåæ ããã¦ãã¾ããã
調æ»çµæ
@Bean ã« @ConfigurationProperties ãåããã¦ä»å ãã¦ãã㨠ConfigurationPropertiesBindingProcessor ã¯ã©ã¹ã® postProcessBeforeInitialization ã¡ã½ãããå¼ã³åºããã¦è¨å®ãåæ ããããã¨ããä»çµã¿ã§ããã