温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springboot自定义starter实现过程图解

发布时间:2020-08-27 23:13:12 来源:脚本之家 阅读:342 作者:西西嘛呦 栏目:编程语言

这篇文章主要介绍了springboot自定义starter实现过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1、创建一个Empty Project

2、在该工程中点击+,选择new module,新建一个maven工程

springboot自定义starter实现过程图解

springboot自定义starter实现过程图解

springboot自定义starter实现过程图解

点击确定。

3、在该工程中点击+,选择new module,新建一个Spring Initializr工程

springboot自定义starter实现过程图解

后面直接默认next,然后点击finishi。

两个都创建完毕之后点击apply,点击OK。得到如下结构:

springboot自定义starter实现过程图解

4、在gong-spring-boot-starter中引入gong-spring-boot-starter-autoconfigurer,即在gong-spring-boot-starter的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.gong.starter</groupId>
  <artifactId>gong.spring-boot-starter</artifactId>
  <version>1.0-SNAPSHOT</version>

  <dependencies>
    <!--引入自动配置模块-->
    <dependency>
      <groupId>com.gong.starter</groupId>
      <artifactId>gong-spring-boot-starter-autoconfigurer</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>

我们看下gong-spring-boot-starter-autoconfigurer中的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 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.2.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.gong.starter</groupId>
  <artifactId>gong-spring-boot-starter-autoconfigurer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>gong-spring-boot-starter-autoconfigurer</name>
  <description>Demo project for Spring Boot</description>

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

  <dependencies>
    <!--引入spring-boot-starter:所有starter基本配置-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>

  </dependencies>

</project>

删除掉创建项目时自动配置的其它东西,只需要一个标红的依赖即可。

5、在gong-spring-boot-starter-autoconfigurer就可以编写场景启动的相关逻辑啦。

(1)新建如下目录结构及文件

springboot自定义starter实现过程图解

springboot启动入口可以删去,resources下文件删去,test文件夹删去。在com.gong.starter下新建以上三个java文件,在resources下新建META-INF文件夹,再新建spring.factories文件。

HelloService.java

package com.gong.starter;

public class HelloService {

  HelloProperties helloProperties;

  public HelloProperties getHelloProperties() {
    return helloProperties;
  }

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

  public String sayHelloGong(String name){
    return helloProperties.getPrefix()+"-" +name + helloProperties.getSuffix();
  }
}

sayHelloGong方法可以获取HelloProperties中的属性,包括前缀和后缀,然后返回:前缀-name后缀。

HelloProperties.java

package com.gong.starter;

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

//绑定所有以gong.hello开头的配置
@ConfigurationProperties(prefix = "gong.hello")
public class HelloProperties {

  private String prefix;
  private String suffix;

  public String getPrefix() {
    return prefix;
  }

  public void setPrefix(String prefix) {
    this.prefix = prefix;
  }

  public String getSuffix() {
    return suffix;
  }

  public void setSuffix(String suffix) {
    this.suffix = suffix;
  }
}

绑定配置以及定义属性。

HelloServiceAutoConfiguration.java

package com.gong.starter;

import org.springframework.beans.factory.annotation.Autowired;
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 //是一个配置类
@ConditionalOnWebApplication //web应用才生效
@EnableConfigurationProperties(HelloProperties.class) //让属性文件生效
public class HelloServiceAutoConfiguration {

  @Autowired
  HelloProperties helloProperties;
  @Bean
  public HelloService helloService(){
    HelloService service = new HelloService();
    service.setHelloProperties(helloProperties);
    return service;
  }
}

自动配置类。要加入三个注解,并对方法使用@Bean标注。

最后,就是在spring.factories中进行配置自动注册:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.gong.starter.HelloServiceAutoConfiguration

这样,我们自己定义的场景启动器就完成了。

接下来新建一个springboot项目进行测试,首先在pom.xml中导入自己定义的场景启动器:

<!--引入自定义的starter-->
    <dependency>
      <groupId>com.gong.starter</groupId>
      <artifactId>gong.spring-boot-starter</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

然后编写application.properties定义场景启动器的属性:

gong.hello.prefix=GONG
gong.hello.suffix=HELLO WORLD

接着编写一个测试类:

package com.gong.springbootjpa.controller;

import com.gong.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("/hello")
  public String hello(){
    return helloService.sayHelloGong("haha");
  }
}

启动服务器

springboot自定义starter实现过程图解

完美。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI