對應經常使用的功能組件,咱們能夠將其製做成一個Starter.這樣在整個系統中都能達到只須要導入依賴便可使用功能,無需其餘配置。java
spring-boot-starter-
spring-boot-starter-模塊名
spring-boot-starter-web
-spring-boot-starter
模塊名-spring-boot-starter
mybatis-spring-boot-starter
經過使用@Configuration
以及Spring4中@Conditional
條件註解等來完成Bean的自動裝配web
在類路徑下新建文件夾META-INF
,在該文件夾下新建spring.factories
文件。spring
# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=自動裝配Bean的全限定路徑
原理戳Spring Boot自動配置分析shell
<?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"> <modelVersion>4.0.0</modelVersion> <groupId>com.hanson</groupId> <artifactId>example-spring-boot-starter</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>example-spring-boot-starter</name> <description>自定義Starter</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.15.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies> </project>
package com.hanson.example.spring.boot.starter.ext; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; @Data @ConfigurationProperties(prefix = "hanson") public class HansonProperties { private String name; private Integer age; }
package com.hanson.example.spring.boot.starter.ext; import lombok.Data; @Data public class HansonService { private HansonProperties hansonProperties; public String introduce() { return "hello,i am " + hansonProperties.getName() + " and i am " + hansonProperties.getAge() + "years old!"; } }
package com.hanson.example.spring.boot.starter; import com.hanson.example.spring.boot.starter.ext.HansonProperties; import com.hanson.example.spring.boot.starter.ext.HansonService; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @EnableConfigurationProperties(HansonProperties.class) public class HansonAutoConfiguration { @Bean public HansonService hansonService(HansonProperties hansonProperties) { HansonService hansonService = new HansonService(); hansonService.setHansonProperties(hansonProperties); return hansonService; } }
# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.hanson.example.spring.boot.starter.HansonAutoConfiguration
<?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"> <modelVersion>4.0.0</modelVersion> <groupId>com.hanson</groupId> <artifactId>example-spring-boot-starter-test</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>example-spring-boot-starter-test</name> <description>自定義Starter測試</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.16.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!--導入咱們自定義的依賴--> <dependency> <groupId>com.hanson</groupId> <artifactId>example-spring-boot-starter</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
在application.yml或者application.properties添加一下配置apache
hanson.name=Hanson hanson.age=25
package com.hanson.example.spring.boot.starter.test; import com.hanson.example.spring.boot.starter.ext.HansonService; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class ExampleSpringBootStarterTestApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(ExampleSpringBootStarterTestApplication.class, args); HansonService bean = context.getBean(HansonService.class); System.out.println(bean.introduce()); } }
2018-09-14 19:46:29.157 INFO 2184 --- [ main] .ExampleSpringBootStarterTestApplication : Started ExampleSpringBootStarterTestApplication in 0.966 seconds (JVM running for 2.281) hello,i am Hanson and i am 25years old! 2018-09-14 19:46:29.167 INFO 2184 --- [ Thread-4] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@120d6fe6: startup date [Fri Sep 14 19:46:28 CST 2018]; root of context hierarchy 2018-09-14 19:46:29.167 INFO 2184 --- [ Thread-4] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown