Spring Boot 自定义starter的示例代码

 更新时间:2018年11月22日 13:57:58   作者:一花一四季,一梦一世界  
这篇文章主要介绍了Spring Boot 自定义starter的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

SpringBoot 个人感觉特点:

1)众多库的集合(各种Starter),方便快速构建应用系统。

2)自动配置spring(通过AutoConfiguration机制),简化配置,也方便扩展新的Starter。

3)内嵌web容器,无需WAR部署。

创建一个用maven构建的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.xjw.springboot</groupId>
  <artifactId>hellostarter</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>hello-spring-boot-starter</name>
  <description>测试自定义starter</description>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.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</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
      <optional>true</optional>
    </dependency>
  </dependencies>

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


</project>

定义一个pojo用来接收properties中配置的信息

package com.xjw;                                                     
                                                             
import org.springframework.boot.context.properties.ConfigurationProperties;                        
                                                             
@ConfigurationProperties(prefix = "hello")                                        
public class HelloServiceProperteis {                                           
                                                             
  private String msg;                                                  
                                                             
  public String getMsg() {                                               
    return msg;                                                    
  }                                                           
                                                             
  public void setMsg(String msg) {                                           
    this.msg = msg;                                                  
  }                                                           
                                                             
}

@ConfigurationProperties:用来标识这个pojo是一个用来接收指定前缀的资源配置值

prefix:表示在配置文件中配置项前缀[/code]

编写一个Service用来对外提供服务

package com.xjw;

public class HelloService {

  private String msg;

  public String sayHello() {
    return "Hello " + msg;
  }

  public String getMsg() {
    return msg;
  }

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

}

配置一个pojo用来读取上面配置的HelloServiceProperteis

package com.xjw;                                                              
                                                                      
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(value = HelloServiceProperteis.class)                                    
@ConditionalOnClass(HelloService.class)                                                   
@ConditionalOnProperty(prefix = "hello", value = "enable", matchIfMissing = true)                              
public class HelloAutoConfiguration {                                                    
                                                                      
  @Autowired                                                               
  private HelloServiceProperteis helloServiceProperteis;                                         
                                                                      
  @Bean                                                                  
  @ConditionalOnMissingBean(HelloService.class)                                              
  public HelloService helloService() {                                                  
    HelloService helloService = new HelloService();                                           
    helloService.setMsg(helloServiceProperteis.getMsg());                                        
    return helloService;                                                        
  }                                                                    
}

@Configuration:标识此类为一个spring配置类

@EnableConfigurationProperties(value = HelloServiceProperteis.class):启动配置文件,value用来指定我们要启用的配置类,可以有多个,多个时我们可以这么写value={xxProperties1.class,xxProperteis2.class....}

@ConditionalOnClass(HelloService.class):表示当classPath下存在HelloService.class文件时改配置文件类才有效

@ConditionalOnProperty(prefix = "hello", value = "enable", matchIfMissing = true):表示只有我们的配置文件是否配置了以hello为前缀的资源项值,并且在该资源项值为enable,如果没有配置我们默认设置为enable[/code]

最后在src/main/resources 文件夹下新建文件夹 META-INF,在新建的META-INF文件夹下新建spring.factories

在新建的spring.factories文件中配置自动启动类为我们之前编写的HelloAutoConfiguration 类

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.xjw.HelloAutoConfiguration

然后就可以在其他的spring-boot项目中使用我们刚刚新建的starter了,我们来测试一下

在新建一个spring-boot项目,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.xjw.springboot</groupId>
  <artifactId>hellostarter.test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>hello-spring-boot-starter-test</name>
  <description>测试自定义starter</description>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.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>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>com.xjw.springboot</groupId>
      <artifactId>hellostarter</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
    </dependency>
  </dependencies>

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


</project>

然后我们直接在咋们的启动类中中尝试使用以下我们上面定义的starter提供的HelloService:

package com.xjw;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class HelloSpringBootStarterTestApplication {

  @Autowired
  private HelloService helloService;

  @RequestMapping("/")
  public String index() {
    return helloService.sayHello();
  }

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

接着我们修改测试项目中的application.properteis,加入如下配置:

debug=true
server.port=8888

#hello=enable
hello.msg=测试starter

最后启动项目,观察控制台输出的内容中依赖的starter,从Positive matches下我们可以看到有这么一句:

HelloAutoConfiguration matched:
- @ConditionalOnClass found required class 'com.xjw.HelloService'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
- @ConditionalOnProperty (hello.enable) matched (OnPropertyCondition)

或者我们打开项目依赖树也能找到我们的starter ,这说明spring已经自动的启动了我们的starter了,打开浏览器输入地址:http://localhost:8888/将会看到如下结果

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

相关文章

  • spring-data-jpa使用自定义repository来实现原生sql

    spring-data-jpa使用自定义repository来实现原生sql

    这篇文章主要介绍了在spring-data-jpa中使用自定义repository来实现原生sql,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • 解决异常:Invalid keystore format,springboot配置ssl证书格式不合法问题

    解决异常:Invalid keystore format,springboot配置ssl证书格式不合法问题

    这篇文章主要介绍了解决异常:Invalid keystore format,springboot配置ssl证书格式不合法问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-03-03
  • java实现图片反色处理示例

    java实现图片反色处理示例

    这篇文章主要为大家详细介绍了java实现图片反色处理示例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • idea的easyCode的 MybatisPlus模板的配置详解

    idea的easyCode的 MybatisPlus模板的配置详解

    这篇文章主要介绍了idea的easyCode的 MybatisPlus模板的配置详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • 通过volatile验证线程之间的可见性

    通过volatile验证线程之间的可见性

    这篇文章主要介绍了通过volatile验证线程之间的可见性,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • Spring成员对象注入的三种方式详解

    Spring成员对象注入的三种方式详解

    这篇文章主要为大家详细介绍了Spring成员对象注入的三种方式,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-02-02
  • 关于Java Interface接口的简单练习题

    关于Java Interface接口的简单练习题

    这篇文章主要给大家分享的是关于Java Interface接口的简单练习题,难度不算大,但是要有一个清晰的逻辑建立接口和链接Java类。下面来看看文章的详细介绍吧,需要的朋友可以参考一下
    2021-11-11
  • 浅谈Spring注入模型

    浅谈Spring注入模型

    如果不深入到Spring的源码,是很少有机会了解到Spring的注入模型(AutowireMode)。但是为了扫清我们学习Spring源码的障碍,我们有必要了解下Spring的注入模型,感兴趣的同学可以阅读一下
    2023-04-04
  • 分析xxljob登入功能集成OIDC的统一认证

    分析xxljob登入功能集成OIDC的统一认证

    这篇文章主要为大家介绍分析xxljob登入功能集成OIDC的统一认证的详解说明,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2022-02-02
  • JMagick实现基本图像处理的类实例

    JMagick实现基本图像处理的类实例

    这篇文章主要介绍了JMagick实现基本图像处理的类,实例分析了java图像处理的相关技巧,需要的朋友可以参考下
    2015-06-06

最新评论