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

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

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

pom.xml

<?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.su</groupId>
  <artifactId>su-spring-boot-starter</artifactId>
  <version>1.0.0</version>
  <packaging>jar</packaging>
  <properties>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
     <!-- spring boot 自动配置需要的包 -->
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-autoconfigure</artifactId>
         <version>1.5.8.RELEASE</version>
     </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

<?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.su</groupId>
    <artifactId>test-spring-boot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>test-spring-boot-starter</name>
    <description>Demo project for Spring Boot</description>
 <!-- 引用依赖的父包 -->
 <parent>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version>1.5.8.RELEASE</version>
 </parent>
 <properties>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   <java.version>1.8</java.version>
 </properties>
 <dependencies>
     <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter</artifactId>
     </dependency>                
         <!--自定义starter依赖-->
        <dependency>
            <groupId>com.su</groupId>
            <artifactId>su-spring-boot-starter</artifactId>
            <version>1.0.0</version>
        </dependency>
        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--test-->
        <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>

编写测试类
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”})
————————————————
版权声明:本文为CSDN博主「我不管我最可爱」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_37934687/article/details/78616079

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值