Spring Boot中的自定義start pom


start pom是springboot中提供的簡化企業級開發絕大多數場景的一個工具,利用好strat pom就可以消除相關技術的配置得到自動配置好的Bean。

舉個例子,在一般使用中,我們使用基本的spring-boot-starter配置基本的springboot項目,也使用spring-boot-starter-web去配置web項目,

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
在pom文件中,引入這個start pom,便可以方便我們快速的導包以及配置相關必要配置,能大大的簡化我們的操作。

spring boot官方也給我們提供了很多start pom,當然除了官方的start pom外,我們也可以使用一些第三方為spring boot寫的start pom。

本篇文章,則是介紹一個自己寫start pom的方法,當能熟練使用這個之后,便可以實現自定義快速編程,敏捷開發了。

  • 首先,先新建一個maven工程,在pom文件中引入spring-boot-autoconfigure依賴

pom文件

<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>site.wangxin520</groupId>
    <artifactId>spring-boot-start-hello</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-start-hello</name>
    <description>自定義的一個spring boot的start pom</description>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>1.5.8.RELEASE</version>
        </dependency>
    </dependencies>
</project>

新建的maven工程目錄為

image

其中HelloService.java是我們需要在springboot中引入的實體類。

  • HelloService.java如下
package site.wangxin520.spring_boot_start_hello;

/**
 * 這個是Bean類,用於在spring boot中使用的
 * 
 * @author wangXgnaw
 *
 */
public class HelloService {

    // 私有屬性,與下面的set和get方法一起,用於注入
    private String msg;

    /**
     * 打招呼的類,用於在spring boot中調用
     * 
     * @return
     */
    public String sayHello() {
        return "hello " + msg;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

}
  • HelloServiceProperties.java相當於是讀取到的application.properties中的配置注入到HelloService中,或者說是當沒有配置的話,就注入默認配置
package site.wangxin520.spring_boot_start_hello.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * 這個是類型安全屬性的獲取,用於配置自動注入bean的參數
 * 
 * @author wangXgnaw
 *
 */
@ConfigurationProperties(prefix = "hello")
public class HelloServiceProperties {

    // 默認的配置的值
    private final static String MSG = "wangxin";

    // 由於在configuationProperties中配置了前綴,所以可以在application.properties中使用hello.msg配置該值。
    private String msg = MSG;

    // 這里的get和set方法,是方便外部注入參數值用的,需要注意的是這里和HelloService不同,並沒有讓properties中的參數直接注入到HelloService中
    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

}
  • HelloServiceAutoconfiguration.java是自動注入的一個配置,與spring相關
package site.wangxin520.spring_boot_start_hello.autoconf;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import site.wangxin520.spring_boot_start_hello.HelloService;
import site.wangxin520.spring_boot_start_hello.properties.HelloServiceProperties;

/**
 * 自動配置類,給bean注入參數
 * 
 * @author wangXgnaw
 *
 */
@Configuration // 標記當前類是配置類
@EnableConfigurationProperties(HelloServiceProperties.class) // 使用java類作為配置文件
@ConditionalOnClass(HelloService.class) // 需要被配置的類
@ConditionalOnProperty(prefix = "hello", value = "enable", matchIfMissing = true)
public class HelloServiceAutoconfiguration {

    // 自動注入配置
    @Autowired
    private HelloServiceProperties helloServiceProperties;

    /**
     * 給bean注入參數,同時返回一個bean實例
     * 同時注解表名,返回是一個bean實例
     * 當容器中沒有這個bean實例的時候,就返回一個自動注入好參數的bean實例回去
     * @return HelloService
     */
    @Bean
    @ConditionalOnMissingBean(HelloService.class)
    public HelloService helloService() {
        HelloService helloService = new HelloService();
        helloService.setMsg(helloServiceProperties.getMsg());
        return helloService;
    }

}
  • spring.factories在src/main/resources中,添加一個文件夾,即META-INF,然后添加一個文件spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=site.wangxin520.spring_boot_start_hello.autoconf.HelloServiceAutoconfiguration

這個文件里面,標注出自動注入的配置文件

 

以上就是全部的start pom案例,當寫完后,使用maven的install安裝到本地倉庫后,在創建好springboot之后,添加上依賴,就可以很方便的使用了。

下面是springboot中引用自定義startpom依賴的實例:

  • 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>site.wangxin520</groupId>
    <artifactId>springboot-starter-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot-starter-test</name>
    <description>springboot學習</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.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.7</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>site.wangxin520</groupId>
            <artifactId>spring-boot-start-hello</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

代碼中標黃了的就是我們自定義的一個startpom依賴坐標。

  • 在啟動類中,我們就可以使用自定義的helloservice類了。
package site.wangxin520.springbootstartertest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import site.wangxin520.spring_boot_start_hello.HelloService;

@SpringBootApplication
@RestController
public class SpringbootStarterTestApplication {

    @Autowired
    private HelloService helloService;
    
    @RequestMapping("/")
    public String index(){
        return helloService.sayHello();
    }
    
    public static void main(String[] args) {
        SpringApplication.run(SpringbootStarterTestApplication.class, args);
    }
}
  • 實際操作:

image

可見,已經自動注入進去了。當我們在application.properties中自行配置的時候,如下

image

  • 得到結果為

image

可見,我們自定義的start pom成功了!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM