Spring Boot(六):自定義starter

在springboot中,使用最多的就是starter。starter能夠理解爲一個可拔插式的插件,例如,你想使用jdbc插件,那麼能夠使用spring-boot-starter-jdbc。隨着版本的推移Starter家族成員也與日俱增。在傳統Maven項目中一般將一些層、組件拆分爲模塊來管理,以便相互依賴複用,在Spring Boot項目中咱們則能夠建立自定義Spring Boot Starter來達成該目的。java

1、starter的工做原理web

一、springboot在啓動時掃描項目所依賴的jar包,尋找包含搜spring.factories文件的jar包spring

二、根據spring.factories配置加載AutoConfiure類apache

三、根據@Conditional註解的條件,進行自動配置並將Bean注入spring context瀏覽器

2、自定義starterspringboot

一、IDEA建立一個empty projectapp

二、添加兩個module,一個是自動配置(maven工程),一個是啓動器(springboot工程),啓動器依賴自動配置。maven

三、項目結構spring-boot

四、內部代碼測試

(1)spring-boot-starter-autoconfigurer module

package com.springboot.starter;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
/**
 * @author:素小暖
 * @date:2020/2/9
 * @desc:
 */
@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class HelloAuto {
    @Autowired
    HelloProperties helloProperties;
 
    @Bean
    public HelloService helloService() {
        HelloService helloService = new HelloService();
        helloService.setHelloProperties(helloProperties);
        return helloService;
    }
}
package com.springboot.starter;
 
import org.springframework.boot.context.properties.ConfigurationProperties;
 
/**
 * @author:素小暖
 * @date:2020/2/9
 * @desc:
 */
@ConfigurationProperties(prefix = "springboot.hello")
public class HelloProperties {
    private String name;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
}
package com.springboot.starter;
 
/**
 * @author:素小暖
 * @date:2020/2/9
 * @desc:
 */
public class HelloService {
 
    HelloProperties helloProperties;
 
    public String hello() {
        return "hello" + helloProperties.getName();
    }
 
    public HelloProperties getHelloProperties() {
        return helloProperties;
    }
 
    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }
}

spring.factories文件: 

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.springboot.starter.HelloAuto

POM文件

<?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.springboot</groupId>
	<artifactId>spring-boot-starter-autoconfigurer</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
 
	<name>spring-boot-starter-autoconfigurer</name>
	<description>Demo project for Spring Boot</description>
 
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.4.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>
 
	</dependencies>
 
</project>

(2) springbootstarter:

啓動器比較簡單,只有pom文件,引入自動配置便可。

<?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>wms</groupId>
    <artifactId>springboot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>com.springboot</groupId>
            <artifactId>spring-boot-starter-autoconfigurer</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
 
</project>

五、 測試,新建springboot項目,pom文件中引入啓動器

<dependency>
    <groupId>springboot</groupId>
    <artifactId>springboot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

application.properties添加配置項

springboot.hello.name=chenzz

  測試controller:

package com.springboot.teststarter;
 
import com.wms.starter.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * @author:素小暖
 * @date:2020/2/9
 * @desc:
 */
@RestController
public class controller {
 
    @Autowired
    HelloService helloService;
 
    @GetMapping("hello")
    public String hello(){
        return helloService.hello();
    }
}

五、瀏覽器訪問

相關文章
相關標籤/搜索