自定义Spring Boot Starter,并使用它

 

Spring Boot由众多Starter组成,随着版本的推移Starter家族成员也与日俱增。在传统Maven项目中通常将一些层、组件拆分为模块来管理,以便相互依赖复用,在Spring Boot项目中我们则可以创建自定义Spring Boot Starter来达成该目的。自定义一个Starter步骤如下:

1.使用Maven或者Gradle构建一个项目,Maven配置文件如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.     <modelVersion>4.0.0</modelVersion>
  6.     <groupId>com.example</groupId>
  7.     <artifactId>example-spring-boot-starter</artifactId>
  8.     <version>1.0-SNAPSHOT</version>
  9.     <dependencies>
  10.         <dependency>
  11.             <groupId>org.springframework.boot</groupId>
  12.             <artifactId>spring-boot-autoconfigure</artifactId>
  13.         </dependency>
  14.     </dependencies>
  15.     <dependencyManagement>
  16.         <dependencies>
  17.             <dependency>
  18.               //此处按需加入其他依赖
  19.             </dependency>
  20.         </dependencies>
  21.     </dependencyManagement>
  22. </project>
 
Gradle配置如下:

 

  1. buildscript {
  2. ext {
  3. springBootVersion = '1.5.9.RELEASE'
  4. }
  5. repositories {
  6. mavenCentral()
  7. }
  8. dependencies {
  9. classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
  10. }
  11. }
  12. apply plugin: 'java'
  13. apply plugin: 'eclipse'
  14. apply plugin: 'org.springframework.boot'
  15. apply plugin:  'maven-publish' //添加maven插件
  16. group = 'com.example'
  17. version = '0.0.1-SNAPSHOT'
  18. sourceCompatibility = 1.8
  19. repositories {
  20. mavenCentral()
  21. }
  22. dependencies {
  23. compile('org.springframework.boot:spring-boot-starter')
  24. //主要是添加这个依赖
  25. compile('org.springframework.boot:spring-boot-autoconfigure')
  26. testCompile('org.springframework.boot:spring-boot-starter-test')
  27. }
下面通过一个简单的例子演示创建service,以及调用。例子中配置userproperties,只有name和age2个属性。UserService 有一个sayHello的方法。然后在其他项目中装载UserService,调用sayHello方法。这里说下artifactId的命名问题,Spring 官方 Starter通常命名为spring-boot-starter-{name}如 spring-boot-starter-web, Spring官方建议非官方Starter命名应遵循{name}-spring-boot-starter的格式。

Userproperties如下:省略get,set方法

  1. @ConfigurationProperties(prefix = "user")
  2. public class Userproperties {
  3. private String name;
  4. private Integer age;
  5. }
UserService如下:

 

  1. public class UserService {
  2. private Userproperties userproperties;
  3. public UserService(Userproperties userproperties) {
  4. this.userproperties = userproperties;
  5. }
  6. public void sayHello(){
  7. System.out.println("Hello---"+userproperties.getName()+"---"+userproperties.getAge());
  8. }
最主要的是编写AutoConfigure类,代码如下:
  1. @Configurable
  2. @EnableConfigurationProperties(Userproperties.class)
  3. @ConditionalOnProperty(prefix = "user", name = {"name""age"})
  4. public class UserAutoConfig {
  5. @Autowired
  6. private Userproperties userproperties;
  7. @Bean
  8. public UserService userService() {
  9. UserService userService = new UserService(userproperties);
  10. return userService;
  11. }
  12. }
到这一步基本配置完成,还有一步很重要,就是在resources/META-INF/下创建spring.factories文件,写上自动配置类的路径,比如下面的:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.config.UserAutoConfig
 

到这里就结束了,使用的时候配置user.name=”xxx”,user.age=33即可。

打包命令如下:

mvn:install   或者 gradle build(IDEA右侧build里面jar命令也可以)然后当本地jar引入即可参考:

 

  1. <dependency>
  2. <groupId>com.example</groupId>
  3. <artifactId>example-spring-boot-starter</artifactId>
  4. <version>1.0-SNAPSHOT</version>
  5. <scope>system</scope>
  6. <systemPath>/Users/henshao/Java/example-starter/target/example-spring-boot-starter-1.0-SNAPSHOT.jar</systemPath>
  7. </dependency>Gradle的是创建一个libs目录,在build.gradle加一句compile fileTree(dir: 'libs', include: '*.jar') 即可
如果需要发布到仓库在build.gradle加
  1. // 发布到本地Maven库和远程Maven服务器
  2. publishing {
  3. repositories {
  4. mavenLocal()
  5. maven {
  6. credentials {
  7. username xxxx
  8. password xxxx
  9. }
  10. url "https://xxxx.xx.com/"
  11. }
  12. }
  13. }