结果前面的SpringBoot源码级结构分析,我们已经知道了关于SpringBoot Starter的工作原来了,现在我们就来自定义一个Starter。
1、项目结构
首先我们来看下这个项目的结构
上面就是这个项目的整体结构了
2、SpringbootHello
这个项目就是要注入的Service(HelloStarterService )
public class HelloStarterService { private String starterName; private String propertyName; public String helloStarter() { StringBuffer buffer = new StringBuffer(); buffer.append(starterName).append(" Hello ").append(propertyName).append(" SpringBoot Starter"); return buffer.toString(); } public void setStarterName(String starterName) { this.starterName = starterName; } public void setPropertyName(String propertyName) { this.propertyName = propertyName; } }
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring-boot-starter-customize</artifactId> <groupId>com.fev.springboot</groupId> <version>1.0</version> <!--<relativePath>../pom.xml</relativePath>--> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.fev.springboot.hello</groupId> <artifactId>spring-boot-hello</artifactId> <version>1.0.0</version> </project>
3、SpringBootStarterAutoconfigure
这个就是自动配置的逻辑,及读取配置文件(application.properties)的类
@ConfigurationProperties("spring.fev.property") public class HelloStartProperties { private String customizeName; public void setCustomizeName(String customizeName) { this.customizeName = customizeName; } public String getCustomizeName() { return customizeName; } }
@Configuration @ConditionalOnClass({HelloStarterService.class}) @EnableConfigurationProperties(HelloStartProperties.class) public class HelloStarterAutoConfigure { @Autowired private HelloStartProperties helloStartProperties; @Bean @ConditionalOnMissingBean(HelloStarterService.class) @ConditionalOnProperty(name = "spring.fev.customize",havingValue = "default",matchIfMissing = true) public HelloStarterService defaultHelloStarterService() { HelloStarterService helloStarterService = new HelloStarterService(); helloStarterService.setStarterName("Default"); helloStarterService.setPropertyName("Default"); return helloStarterService; } @Bean @ConditionalOnMissingBean(HelloStarterService.class) @ConditionalOnProperty(name = "spring.fev.customize",havingValue = "customize") public HelloStarterService customizeHelloStarterService() { HelloStarterService helloStarterService = new HelloStarterService(); helloStarterService.setPropertyName(helloStartProperties.getCustomizeName()); helloStarterService.setStarterName("customize"); return helloStarterService; } }
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring-boot-starter-customize</artifactId> <groupId>com.fev.springboot</groupId> <version>1.0</version> <!--<relativePath>../pom.xml</relativePath>--> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>spring-boot-starter-autoconfigure</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.0.4.RELEASE</version> </dependency> <dependency> <groupId>com.fev.springboot.hello</groupId> <artifactId>spring-boot-hello</artifactId> <version>1.0.0</version> <optional>true</optional> </dependency> </dependencies> </project>
4、SpringBootStarterHello
这个就是hello-sevice的Starter
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring-boot-starter-customize</artifactId> <groupId>com.fev.springboot</groupId> <version>1.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>spring-boot-starter-hello</artifactId> <dependencies> <dependency> <groupId>com.fev.springboot</groupId> <artifactId>spring-boot-starter-autoconfigure</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>com.fev.springboot.hello</groupId> <artifactId>spring-boot-hello</artifactId> <version>1.0.0</version> </dependency> </dependencies> </project>
spring.factories文件
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.fev.autoconfigure.HelloStarterAutoConfigure
以上就是我们这个项目的内容,下面我们来看其的使用
5、对Starter-Hello的使用
对其的使用,我们只需要随便创建一个SpringBoot项目然后引入
<dependency> <groupId>com.fev.springboot</groupId> <artifactId>spring-boot-starter-hello</artifactId> <version>1.0</version> </dependency>
就能直接进行HelloStarterService的注入使用了,例如创建一个Controller
@RestController public class MyController { @Autowired private HelloStarterService helloStarterService; @GetMapping("/hello") public String get() throws IllegalAccessException { return helloStarterService.helloStarter(); } }
我们先不配置application.yml(properties)文件,直接启动请求:
可以看到其在页面上打印的是默认的HelloStarterService,现在我们来修改下application文件,再启动访问
spring.fev.customize=customize spring.fev.property.customizeName=Feverasa
可以看到其使用的就是我们自定义的信息了。