自定义starter的创建

一.自定义启动器的命名规则

启动器命名规则:

springboot官方的命名规则是:spring-boot-starter-xxx

自定义的启动器的命名规则是:xxx-spring-boot-starter

二.构建启动器的步骤

1.首先创建一个空项目

这里写图片描述

2.创建两个子工程

1.点击加号添加一个maven工程,命名为mystarter-spring-boot-starter

2.再点击加号创建一个springboot项目,命名为mystarter-spring-boot-starter-autoconfigurater

让该maven项目依赖于这个springboot项目

maven项目的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.cb.starter</groupId>
    <artifactId>mystarter-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

  <!-- 添加对启动器自动配置的依赖-->
    <dependencies>
        <dependency>
            <groupId>com.cb.starter</groupId>
            <artifactId>mystarter-spring-boot-starter-autoconfigurater</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

springboot项目的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.cb.starter</groupId>
    <artifactId>mystarter-spring-boot-starter-autoconfigurater</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>mystarter-spring-boot-starter-autoconfigurater</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>


</project>
3.在springboot中编写配置

1.新建配置文件类HelloProperties.java

package com.cb.starter;

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


@ConfigurationProperties(prefix = "hello")
public class HelloProperties {

    private String msg ;

    public String getMsg() {
        return msg;
    }

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

2.创建一个service

package com.cb.starter;

public class HelloService {

    HelloProperties helloProperties;

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }

    public String sayhello(String name){
        return "hello "+ name + helloProperties.getMsg();
    }
}

3.创建一个配置类

package com.cb.starter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(HelloProperties.class) //导入配置文件
@ConditionalOnWebApplication    //web环境下才起作用
public class HelloConfiguration {
    @Autowired
    HelloProperties helloProperties;

    @Bean   //返回给容器一个name=helloService的bean
    public HelloService helloService(){
        HelloService helloService = new HelloService();
        helloService.setHelloProperties(helloProperties);
        return helloService;
    }
}
4. 配置启动器的类路径

springboot自动配置的原理就是在启动类上加@springbootapplication注解,这个注解包含

@EnableAutoConfiguration,配置类要想在启动时自动加载,必须要配置在EnableAutoConfiguration下,

这个东西默认是去类路径下的META-INF下的spring.factories下去找的。我们在resources下新建META-INF文件夹。

之后打成jar包后,就和其他的maven仓库的jar一样的结构了。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.cb.starter.HelloConfiguration

5.点击idea右侧的maven project

先对springboot项目进行install,安装到本地仓库。在对maven项目进行安装 ,此时仓库里已有这两个jar包了。

我们可以直接来使用,就像使用基本的jar一样。

三. 测试启动器

新建一个springboot项目,pom.xml添加启动器的id

<!--引入自己配置的starter-->
<dependency>
    <groupId>com.cb.starter</groupId>
    <artifactId>mystarter-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

此时可以看到,项目的external libraries下已经有了我们刚刚构建的启动器的jar包了。

新建一个控制器:

package com.cb.starterdemo;

import com.cb.starter.HelloService;  //我们自定义的启动器可以使用
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @Autowired
    HelloService helloService;
    @GetMapping(value = "/hello")
    public String sayhello(String name){
        return helloService.sayhello(name);
    }
}

配置文件中配置一个属性:

hello.msg = 小萝莉

运行起来访问:http://localhost:8080/hello?name=123

显示:hello 123小萝莉

ok!大功告成

猜你喜欢

转载自blog.csdn.net/KKALL1314/article/details/82704593