【Spring Boot 系列 自定義啓動器Starter】

對應經常使用的功能組件,咱們能夠將其製做成一個Starter.這樣在整個系統中都能達到只須要導入依賴便可使用功能,無需其餘配置。java

一、Starter命名規則

1.一、官方命名方式

  • 前綴:spring-boot-starter-
  • 規範:spring-boot-starter-模塊名
  • 示例:spring-boot-starter-web

1.二、第三方命名方式

  • 後綴:-spring-boot-starter
  • 規範:模塊名-spring-boot-starter
  • 示例:mybatis-spring-boot-starter

二、自動裝配Bean

經過使用@Configuration以及Spring4中@Conditional條件註解等來完成Bean的自動裝配web

三、配置自動裝配Bean

在類路徑下新建文件夾META-INF,在該文件夾下新建spring.factories文件。spring

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=自動裝配Bean的全限定路徑

原理戳Spring Boot自動配置分析shell

四、示例程序

4.一、利用IDEA建立一個SpringBoot工程

  • Maven依賴以下
<?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;
    }
}
  • 新增spring.factories文件 在類路徑下建立META-INF文件夾,在該文件下建立一個spring.factories文件。其內容以下
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.hanson.example.spring.boot.starter.HansonAutoConfiguration
  • 打包安裝

4.二、測試程序

  • 利用IDEA 建立一個Spring Boot工程,依賴以下
<?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
相關文章
相關標籤/搜索