自定义starter的创建

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

启动器命名规则:

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

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

二.构建启动器的步骤

1.首先创建一个空项目

自定义starter的创建_第1张图片

2.创建两个子工程

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

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

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

maven项目的pom文件:


<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.0modelVersion>

    <groupId>com.cb.startergroupId>
    <artifactId>mystarter-spring-boot-starterartifactId>
    <version>1.0-SNAPSHOTversion>

  
    <dependencies>
        <dependency>
            <groupId>com.cb.startergroupId>
            <artifactId>mystarter-spring-boot-starter-autoconfiguraterartifactId>
            <version>0.0.1-SNAPSHOTversion>
        dependency>
    dependencies>

project>

springboot项目的pom.xml:


<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.0modelVersion>

    <groupId>com.cb.startergroupId>
    <artifactId>mystarter-spring-boot-starter-autoconfiguraterartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>mystarter-spring-boot-starter-autoconfiguratername>
    <description>Demo project for Spring Bootdescription>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.3.RELEASEversion>
        <relativePath/> 
    parent>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        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


<dependency>
    <groupId>com.cb.startergroupId>
    <artifactId>mystarter-spring-boot-starterartifactId>
    <version>1.0-SNAPSHOTversion>
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!大功告成

你可能感兴趣的:(Java后台,maven,编程,idea,springboot)