自定义SpringBoot的starter,以及如何导入jar包

一、介绍

SpringBoot 最强大的功能就是把我们常用的场景抽取成了一个个starter(场景启动器),我们通过引入springboot 为我提供的这些场景启动器,我们再进行少量的配置就能使用相应的功能。即使是这样,springboot也不能囊括我们所有的使用场景,往往我们需要自定义starter,来简化我们对springboot的使用。启动器starter只是用来做依赖管理,需要专门写一个类似spring-boot-autoconfigure的配置模块,用的时候只需要引入启动器starter,就可以使用自动配置了。

首先需要写一个Configuration(一般还会伴随着写ConditionalOnxxx来判断下导入那个包才生效,不导入就不生效),再配一个Properties, 打包成jar,然后再进行调用引入。自定义starter主要包含两个环节,一个是生成jar包环节,一个是引入jar包。

命名规范

官方命名空间

  • 前缀:spring-boot-starter-
  • 模式:spring-boot-starter-模块名
  • 举例:spring-boot-starter-web、spring-boot-starter-jdbc

自定义命名空间

  • 后缀:-spring-boot-starter
  • 模式:模块-spring-boot-starter
  • 举例:mybatis-spring-boot-starter

一、编写自定义代码,生成jar包

1. 首先创建一个springboot项目,在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.wisdom</groupId>
   <artifactId>spring-boot-starter-hello</artifactId>
   <version>1.0-REALEASE</version>
   <packaging>jar</packaging>

   <dependencies>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-autoconfigure</artifactId>
           <version>2.0.0.RELEASE</version>
       </dependency>
   </dependencies>

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

2. 同时删除 启动类、resources下的文件,test文件。
3. 编写properties属性类,用于读取属性。

@ConfigurationProperties(prefix = "com.wisdom")
public class HelloServiceProperties {

    private String name = "wisdom";

    private String hobby = "basketball";

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }
}

@ConfigurationProperties配置此注解可以自动导入application.properties配置文件中的属性,前提需要指定属性前缀prefix。如果application.properties文件中未指定相应属性,便使用默认的,如上name=“wisdom”,hobby=“basketball”.

4. 创建配置类:

public class HelloServiceConfiguration {

    private String name;

    private String hobby;

    public String getName() {
        return "name is " + name;
    }

    public String getHobby() {
        return "hobby is " + hobby;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }
}

5. 创建自动配置类:

@Configuration
@EnableConfigurationProperties(HelloServiceProperties.class)
@ConditionalOnClass(HelloServiceConfiguration.class)
@ConditionalOnProperty(prefix = "com.wisdom", value = "enabled", matchIfMissing = true)
public class HelloServiceAutoConfiguration {

    @Autowired
    private HelloServiceProperties helloServiceProperties;

    @Bean
    @ConditionalOnMissingBean(HelloServiceConfiguration.class)
    public HelloServiceConfiguration helloServiceConfiguration() {
        HelloServiceConfiguration helloService = new HelloServiceConfiguration();
        helloService.setName(helloServiceProperties.getName());
        helloService.setHobby(helloServiceProperties.getHobby());
        return helloService;
    }
}

  • @Configuration:表明此类是一个配置类,将变为一个bean被spring进行管理。
  • @EnableConfigurationProperties:启用属性配置,将读取HelloServiceProperties里面的属性。
  • @ConditionalOnClass:当类路径下面有HelloServiceConfiguration此类时,自动配置。
  • @ConditionalOnProperty:判断指定的属性是否具备指定的值。
  • @ConditionalOnMissingBean:当容器中没有指定bean是,创建此bean。

6. 在resources文件夹下面新建一个META-INF文件,并在下面创建spring.factories文件,将4中的配置类进行注册。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
 com.wisdom.HelloServiceAutoConfiguration

至此,自定义的spring-boot-starter-hello编写完毕,执行mvn clean install将项目打成一个jar包。

二、引入jar包,进行测试

1. 新建一个springboot项目,在pom文件中添加刚刚打包的jar的坐标。

<dependency>
           <groupId>com.wisdom</groupId>
           <artifactId>spring-boot-starter-hello</artifactId>
           <version>1.0-REALEASE</version>
</dependency>

2. 在启动类上编写访问接口

@SpringBootApplication
@RestController
public class Springboot03Application {

    @Autowired
    private HelloServiceConfiguration helloService;

    public static void main(String[] args) {
        SpringApplication.run(Springboot03Application.class, args);
    }

    @RequestMapping("/name")
    public String getName() {
        return helloService.getName();
    }

    @RequestMapping("/hobby")
    public String getHobby() {
        return helloService.getHobby();
    }
}

3. 在application.properties或者application.yaml添加修改默认属性

#properties文件
com.wisdom.hobby=football
com.wisdom.name=messi
com:
  wisdom:
    name: xxx
    hobby: yyy

注意点:

  1. yaml文件编写的时候,一定要注意属性和字段名之间有个空格,最开始我一直没注意,然后发现yaml中的属性就一直没法修改,找了很久才排查出来的错误~。

  2. 在生成jar包的时候,如果都是在本地,一定要点install,maven的install可以将项目本身编译并打包到本地仓库。

  3. 如果不使用install,还可以通过通过点击maven中的clean, package,打包完成后发给别人,然后别人也可以用自己写的jar包。这样的话,就涉及到如何在springboot中添加外来jar包的问题,详情如下。

IDEA Springboot开发中如何导入外部的jar包

1,先创建一个lib文件夹,设置lib文件夹为resource属性,操作如下
在这里插入图片描述
2、 在pom.xml中添加依赖,来找到相应的jar包

        <dependency>
            <groupId>com.test.util</groupId>
            <artifactId>com.test.util</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${pom.basedir}/src/main/resources/lib/spring-boot-starter-hello-1.0-REALEASE.jar</systemPath>
        </dependency>
        <!--注意这里的resource是添加在build标签对中-->
        <resources>
            <resource>
                <directory>lib</directory>
                <targetPath>BOOT-INF/lib/</targetPath>
                <includes>
                    <include>**/*.jar</include>
                </includes>
            </resource>
        </resources>
  1. 需要将jar包指定到Libraries中的 lib下 如下图:
    在这里插入图片描述

参考博客:
https://blog.csdn.net/qq_21150865/article/details/83504780
https://www.jianshu.com/p/b5794bbd4b54
https://blog.csdn.net/tom_920/article/details/86062829

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值