自己编写一个SpringBoot的Starter插件项目

用过SpringBoot的同学相信都不会陌生POM里面引用的很多第三方的Starter插件,只要引用进项目,按照配置一下properties属性就可以自然使用了,比如Mybatis等等,觉得很酷,现在自己动手也编写一个这样的插件,以提供给别的项目,方便使用,更重要的是把相同功能和性质的代码封装起来,即插即用。

1.pom的引用

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-autoconfigure</artifactId>
  <version>2.0.0.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-configuration-processor</artifactId>
  <version>2.0.0.RELEASE</version>
  <optional>true</optional>
</dependency>
2.resources文件夹下面建立文件夹META-INF,里面建立一个文件,如:spring.factories,内容如下,路径为自己的配置类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=USplus.configuration.UsServiceAutoConfiguration

3.工程结构目录,这个因人而异

4.UsServiceAutoConfiguration是加载时候的配置类

package USplus.configuration;

import USplus.UsProperties.UsProperties;
import USplus.UsService.UsService;
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;

@Configuration
@EnableConfigurationProperties(UsProperties.class)
@ConditionalOnClass(UsProperties.class)
@ConditionalOnProperty(prefix = "spring.person", value = "enabled", matchIfMissing = true)
public class UsServiceAutoConfiguration {

    @Autowired
    private UsProperties usProperties;

    @Bean
    @ConditionalOnMissingBean(UsService.class)  // 当容器中没有指定Bean的情况下,自动配置UsService类
    public UsService usService(){
        UsService usService = new UsService(usProperties);
        return usService;
    }


}

5.自定义属性参数类,UsProperties

package USplus.UsProperties;

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

@ConfigurationProperties(prefix = "spring.person")
public class UsProperties {

    private String where;

    private String when;

    private String who;

    private String what;

    private String why;

    public String getWhere() {
        return where;
    }

    public void setWhere(String where) {
        this.where = where;
    }

    public String getWhen() {
        return when;
    }

    public void setWhen(String when) {
        this.when = when;
    }

    public String getWho() {
        return who;
    }

    public void setWho(String who) {
        this.who = who;
    }

    public String getWhat() {
        return what;
    }

    public void setWhat(String what) {
        this.what = what;
    }

    public String getWhy() {
        return why;
    }

    public void setWhy(String why) {
        this.why = why;
    }

    @Override
    public String toString() {
        return "UsProperties{" +
                "where='" + where + '\'' +
                ", when='" + when + '\'' +
                ", who='" + who + '\'' +
                ", what='" + what + '\'' +
                ", why='" + why + '\'' +
                '}';
    }
}

6.自定义想要做什么事的service,UsService

package USplus.UsService;

import USplus.UsProperties.UsProperties;

public class UsService {

    private UsProperties usProperties;


    public UsService(UsProperties usProperties){
        this.usProperties = usProperties;
    }


    public String wh(){
        return usProperties.toString();
    }
}

7.打包即可引用了,我的groupId如下

<groupId>USplus</groupId>
<artifactId>us</artifactId>
<version>1.0-SNAPSHOT</version>

启动后,springboot自动加载配置的属性,并且返回,我配置的是when,who,where,what,why,简单的一个小例子,初学乍练,欢迎批评指正!

 
 
 

猜你喜欢

转载自blog.csdn.net/airyearth/article/details/105917126