自定义spring-boot-starter(IDEA开发)

前言


简单粗暴的说
官方提供的默认的spring boot starter中有些许依赖或者配置是我们在开发过程中
所不需要的,对此我们可以自定义一个starter,来满足我们的开发需求

首先,要知道我们并不需要创建一个springboot项目,一个maven项目远远足够了
其次,废话不多说了,直接看代码


pom.xml


<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.0modelVersion>
  <groupId>com.sugroupId>
  <artifactId>su-spring-boot-starterartifactId>
  <version>1.0.0version>
  <packaging>jarpackaging>
  <properties>
   <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
  properties>
  <dependencies>
     
     <dependency>
         <groupId>org.springframework.bootgroupId>
         <artifactId>spring-boot-autoconfigureartifactId>
         <version>1.5.8.RELEASEversion>
     dependency>
   dependencies>
project>

注:关于artifactId
官方命名格式为: spring-boot-starter-{name}
非官方建议命名格式:{name}-spring-boot-starter


首先,我们创建一个配置文件映射实体类
HelloServiceProperties .java

package com.su;

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

/**
 *  配置文件映射实体类
 *  @anchor:su
 *  备注:
 *   1.@ConfigurationProperties:将application.properties配置文件中的 
 *     符合规则的配置参数映射到实体类中
 *   2.preffix,该属性配置了读取参数的前缀
 *     根据实体属性对应配置文件内的配置为:hello.msg
 *     配置文件中不提供时则使用默认值
 */
@ConfigurationProperties(prefix="hello")
public class HelloServiceProperties {
    //设置消息内容的默认值
    private String msg = "World";

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

其次,我们创建一个service 来编写我们的业务逻辑
HelloService.java

package com.su;
/**
 * 业务类
 * @author su
 */
public class HelloService {

    private String msg;

    public String haloHello(){
        return "Hello Starter ===============>>>>"+msg;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    };
}

然后,重要的AutoConfiguration来临了
HelloServiceAutoConfiguration.java

package com.su;

import com.su.HelloService;
import com.su.HelloServiceProperties;
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;

/**
 * 自定义starter自动配置类
 * @author su
 */
/**
 * 注解详解
 * 1. @Configuration:说明该类是配置类,等价于xml中的beans
 * 2. @EnableConfigurationProperties
      (com.su.HelloServiceProperties.class):开启属性注入
 * 3. @ConditionalOnClass(com.su.HelloService.class):条件注解
 *    当类路径下有指定的类的条件(即存在HelloService时初始化该配置类)
 * 4. @ConditionalOnProperty
 *   (prefix=”hello”,value=”enabled”,matchIfMissing=true):条件注解
 *   (存在配置前缀hello,开启,缺失检查)
 *    存在对应配置信息时初始化该配置类
 */
@Configuration
@EnableConfigurationProperties(HelloServiceProperties.class)
@ConditionalOnClass(HelloService.class)
@ConditionalOnProperty(prefix="hello",value="enabled",matchIfMissing=true)
public class HelloServiceAutoConfiguration {
    //映射类
    @Autowired
    private HelloServiceProperties helloServiceProperties;

    /**
     * @ConditionalOnMissingBean(HelloService.class)
     * 当SpringIoc容器内不存在指定Bean的条件
     * (缺失HelloService实体bean时,初始化HelloService)
     * 并添加到SpringIoc中
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(HelloService.class)
    public HelloService helloService(){
        System.out.println("Execute Create New Bean");
        HelloService helloService = new HelloService();
        helloService.setMsg(helloServiceProperties.getMsg());
        return helloService;
    }
}

从starter的自动化运作原理中,我们可以得知
@SpringBootApplication上存在一个开启自动化配置的注解@EnableAutoConfiguration
以下为该注解源码:

package org.springframework.boot.autoconfigure;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Import;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({EnableAutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
    String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
    Class[] exclude() default {};
    String[] excludeName() default {};
}

不难看出,在@EnableAutoConfiguration注解内使用@import 来导入配置
EnableAutoConfigurationImportSelector内部使用
SpringFactoriesLoader.loadFactoryNames方法进行扫描
具有META-INF/spring.factories文件的jar包
以下是spring-boot-autoconfigure包内的spring.factories文件内容:

# Initializers
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,\
org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer

# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.autoconfigure.BackgroundPreinitializer

# Auto Configuration Import Listeners
org.springframework.boot.autoconfigure.AutoConfigurationImportListener=\
org.springframework.boot.autoconfigure.condition.ConditionEvaluationReportAutoConfigurationImportListener
#######等等

照猫画瓢,我们也用相同的key-value形式来定义我们的内容
因为我们这是要实现自定义starter配置,所以我们的key为:
org.springframework.boot.autoconfigure.EnableAutoConfiguration

在resources下自定义 META-INF/spring.factories

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.su.HelloServiceAutoConfiguration

至此,我们的自定义starter就大功告成了


最后一步就是,创建一个Springboot项目来测试我们的starter
pom.xml


<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.0modelVersion>
    <groupId>com.sugroupId>
    <artifactId>test-spring-boot-starterartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>
    <name>test-spring-boot-startername>
    <description>Demo project for Spring Bootdescription>
 
 <parent>
     <groupId>org.springframework.bootgroupId>
     <artifactId>spring-boot-starter-parentartifactId>
     <version>1.5.8.RELEASEversion>
 parent>
 <properties>
   <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
   <java.version>1.8java.version>
 properties>
 <dependencies>
     <dependency>
       <groupId>org.springframework.bootgroupId>
       <artifactId>spring-boot-starterartifactId>
     dependency>                
         
        <dependency>
            <groupId>com.sugroupId>
            <artifactId>su-spring-boot-starterartifactId>
            <version>1.0.0version>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>

编写测试类
MainApplication.java

package com.su;

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;

/**
 * 测试
 * @anchor su
 */
@SpringBootApplication
@RestController
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }

    @Autowired
    public HelloService helloService;

    @RequestMapping("/halo")
    public String index(){
        return helloService.haloHello();
    }
}

在运行项目之前,我们打开application.properties配置文件

#开启debug模式,查看自动化配置的输出日志
debug=true
#定义消息内容
hello.msg = success

启动项目 查看控制台输出日志 看页面输出信息
访问路径 http://localhost:8080/halo
注:在IDEA中因为没有工作空间这个概念
所以我们需要将starter项目 mvn install 到本地仓库中
或者 在test项目右侧 点击maven projects 点击+ 将starter项目的pom添加进去即可
Eclipse开发则无这个问题

再者
若starter和test项目包名不同,则需要在测试类上添加@ComponentScan(basePackages={“com.su”})

你可能感兴趣的:(spring系列)