创建自定义springboot的starter

夙愿已清 提交于 2020-10-03 05:19:45

springboot自定义starter

完整的springboot starter包含两个组件

  • autoconfigure
    这里包含自动配置的代码

  • starter
    依赖,或者依赖的其他springboot starter组件

自定义starter的命名

一般来说,自动装配的配置文件都是以xxxx-spring-boot-autoconfigur
starter模块的命名为xxxx-spring-boot-starter

开始自定义starter

创建工程


这是工程的目录结构


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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.gitee.randomobject</groupId>
    <artifactId>sorm-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sorm-starter</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

</project>

HelloProperties

package com.gitee.randomobject.sormstarter.autoconfigure;

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

/**
 * Sorm的自动配置类
 */
@ConfigurationProperties(prefix = "my.sorm")
@Data
public class HelloProperties {

    /**
     * 姓名
     */
    private String name;

    /***
     * 年龄
     */
    private int age;

}


HelloService

package com.gitee.randomobject.sormstarter.autoconfigure;

import lombok.Data;

@Data
public class HelloService {


   private String name;

   private int age;

    public HelloService(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

SormStarterAutoConfiguration

package com.gitee.randomobject.sormstarter.autoconfigure;

import com.gitee.randomobject.SormFactory;
import com.gitee.randomobject.dao.DAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class SormStarterAutoConfiguration {

    private final HelloProperties helloProperties;


    public SormStarterAutoConfiguration(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }

    @Bean
    @ConditionalOnMissingBean
    public HelloService sormStarter(){
        return new HelloService(helloProperties.getName(),helloProperties.getAge());
    }
}
构建

mvn clean install

测试

只需引入

<dependency>
  <groupId>com.gitee.randomobject</groupId>
  <artifactId>sorm-starter</artifactId>
  <version>0.0.1-SNAPSHOT</version>
</dependency>

然后注入即可

 @Autowired
 private HelloService helloService;

@Test
public void testMyCon(){
   System.out.printf("姓名:%s,年龄:%d",helloService.getName(),helloService.getAge());
   System.out.println("");
}

application.yml中配置


结果

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!