å¨å¦ä¹ Spring Bootçè¿ç¨ä¸ï¼æ¥è§¦æå¤çå°±æ¯starterãå¯ä»¥è®¤ä¸ºstarteræ¯ä¸ç§æå¡ââ使å¾ä½¿ç¨æ个åè½çå¼åè
ä¸éè¦å
³æ³¨åç§ä¾èµåºçå¤çï¼ä¸éè¦å
·ä½çé
置信æ¯ï¼ç±Spring Bootèªå¨éè¿classpathè·¯å¾ä¸çç±»åç°éè¦çBeanï¼å¹¶ç»å
¥beanã举个ä¾åï¼
spring-boot-starter-jdbc
è¿ä¸ªstarterçåå¨ï¼ä½¿å¾æ们åªéè¦å¨BookPubApplicationä¸ç¨
@Autowired
å¼å
¥DataSourceçbeanå°±å¯ä»¥ï¼Spring Bootä¼èªå¨å建DataSourceçå®ä¾ã
è¿éæ们ä¼ç¨ä¸ä¸ªä¸å¤ªè§èçstarterå±ç¤ºSpring Bootçèªå¨é ç½®çè¿è¡åçã Spring Bootçèªå¨é ç½®ãCommand-line Runner ä¸æä¸æ¾å©ç¨StartupRunnerç±»å¨ç¨åºè¿è¡å¯å¨åé¦å æ¥è¯¢æ°æ®åºä¸ä¹¦çæ°ç®ï¼ç°å¨æ¢ä¸ªéæ±ï¼ å¨ç³»ç»å¯å¨åæå°å个å®ä½çæ°é ã
How Do
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<!-- version继æ¿ç¶æ¨¡åç-->
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>1.9.3.RELEASE</version>
</dependency></dependencies>
æ°å»ºå
ç»æcom/test/bookpubstarter/dbcountï¼ç¶åæ°å»ºDbCountRunnerç±»ï¼å®ç°CommandLineRunneræ¥å£ï¼å¨runæ¹æ³ä¸è¾åºæ¯ä¸ªå®ä½çæ°éã
package com.test.bookpubstarter.dbcount;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.repository.CrudRepository;
import java.util.Collection;
public class DbCountRunner implements CommandLineRunner {
protected final Logger logger = LoggerFactory.getLogger(DbCountRunner.class);
private Collection<CrudRepository> repositories;
public DbCountRunner(Collection<CrudRepository> repositories) {
this.repositories = repositories;
@Override
public void run(String... strings) throws Exception {
repositories.forEach(crudRepository -> {
logger.info(String.format("%s has %s entries",
getRepositoryName(crudRepository.getClass()),
crudRepository.count()));
private static String getRepositoryName(Class crudRepositoryClass) {
for (Class repositoryInterface : crudRepositoryClass.getInterfaces()) {
if (repositoryInterface.getName().startsWith("com.test.bookpub.repository")) {
return repositoryInterface.getSimpleName();
return "UnknownRepository";
å¢å èªå¨é
ç½®æ件DbCountAutoConfiguration
package com.test.bookpubstarter.dbcount;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.repository.CrudRepository;
import java.util.Collection;
@Configuration
public class DbCountAutoConfiguration {
@Bean
public DbCountRunner dbCountRunner(Collection<CrudRepository> repositories) {
return new DbCountRunner(repositories);
å¨src/main/resourcesç®å½ä¸æ°å»ºMETA-INFæ件夹ï¼ç¶åæ°å»ºspring.factoriesæ件ï¼è¿ä¸ªæ件ç¨äºåè¯Spring Bootå»æ¾æå®çèªå¨é
ç½®æ件ï¼å æ¤å®çå
容æ¯
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.test.bookpubstarter.dbcount.DbCountAutoConfiguration
å¨ä¹åçç¨åºåºç¡ä¸ï¼å¨é¡¶å±pomæ件ä¸å¢å starterçä¾èµ
<dependency>
<groupId>com.test</groupId>
<artifactId>db-count-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
æStartupRunnerç¸å
³ç注éæï¼ç¶åå¨mainå½æ°ä¸å³é®Run BookPubApplication.main(...)ï¼å¯ä»¥çåºæ们ç¼åçstarter被主ç¨åºä½¿ç¨äºã
èªå¨é
ç½®æ件ï¼æ ¹æ®classpathæ¯å¦åå¨æå®çç±»æ¥å³å®æ¯å¦è¦æ§è¡è¯¥åè½çèªå¨é
ç½®ã
spring.factoriesï¼é常éè¦ï¼æ导Spring Bootæ¾å°æå®çèªå¨é
ç½®æ件ã
endpointï¼å¯ä»¥ç解为ä¸ä¸ªadminï¼å
å«å¯¹æå¡çæè¿°ãçé¢ã交äºï¼ä¸å¡ä¿¡æ¯çæ¥è¯¢ï¼
health indicatorï¼è¯¥starteræä¾çæå¡çå¥åº·ææ
å¨åºç¨ç¨åºå¯å¨è¿ç¨ä¸ï¼Spring Boot使ç¨SpringFactoriesLoaderç±»å è½½å¨æ¥æ¾org.springframework.boot.autoconfigure.EnableAutoConfigurationå
³é®å对åºçJavaé
ç½®æ件ãSpring Bootä¼éåå¨å个jarå
ç§META-INFç®å½ä¸çspring.factoriesæ件ï¼æ建æä¸ä¸ªé
ç½®æ件é¾è¡¨ãé¤äºEnableAutoConfigurationå
³é®å对åºçé
ç½®æ件ï¼è¿æå
¶ä»ç±»åçé
ç½®æ件ï¼
org.springframework.context.ApplicationContextInitializer
org.springframework.context.ApplicationListener
org.springframework.boot.SpringApplicationRunListener
org.springframework.boot.env.PropertySourceLoader
org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider
org.springframework.test.contex.TestExecutionListener
Spring Bootçstarterå¨ç¼è¯æ¶ä¸éè¦ä¾èµSpring Bootçåºãè¿ä¸ªä¾åä¸ä¾èµspring boot并ä¸æ¯å 为èªå¨é
ç½®è¦ç¨spring bootï¼èä»
ä»
æ¯å 为éè¦å®ç°CommandLineRunneræ¥å£ã
两个éè¦æ³¨æçç¹
@ConditionalOnMissingBeançä½ç¨æ¯ï¼åªæ对åºçbanå¨ç³»ç»ä¸é½æ²¡æ被å建ï¼å®ä¿®é¥°çåå§å代ç åæä¼æ§è¡ï¼ç¨æ·èªå·±æå¨å建çbeanä¼å
ï¼
Spring Boot starterå¦ä½æ¾å°èªå¨é
ç½®æ件ï¼xxxxAutoConfigurationä¹ç±»çæ件ï¼ï¼
spring.factoriesï¼ç±Spring Boot触åæ¢æµclasspathç®å½ä¸çç±»ï¼è¿è¡èªå¨é
ç½®ï¼
@Enableï¼ææ¶éè¦ç±starterçç¨æ·è§¦å*æ¥æ¾èªå¨é
ç½®æ件çè¿ç¨ã
æ¬å·ä¸æ³¨äºå端ææ¯ãJVMé®é¢ææ¥åä¼åãJavaé¢è¯é¢ã个人æé¿åèªæ管çç主é¢ï¼ä¸ºè¯»è
æä¾ä¸çº¿å¼åè
çå·¥ä½åæé¿ç»éªï¼æå¾
ä½ è½å¨è¿éæææ¶è·ã