spring boot 自定义starter的实现教程

所属分类: 软件编程 / java 阅读数: 14
收藏 0 赞 0 分享

spring boot 使用 starter 解决了很多配置问题, 但是, 他是怎么来解决这些问题的呢?

这里通过一个简单的例子, 来看一下, starter是怎么来设置默认配置的.

一. 建 starter 项目

自定义的starter, 项目命名规范是: 自定义名-spring-boot-starter

先来看一下, 我最后的目录结构

1. 修改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.0</modelVersion>
 <groupId>org.elvin</groupId>
 <artifactId>my-spring-boot-starter</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>jar</packaging>
 <name>my-spring-boot-starter</name>
 <url>http://maven.apache.org</url>
 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>
 <dependencies>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-autoconfigure</artifactId>
 <version>1.5.9.RELEASE</version>
 </dependency>
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>3.8.1</version>
 <scope>test</scope>
 </dependency>
 </dependencies>
 <build>
 <plugins>
 <plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-compiler-plugin</artifactId>
 <version>2.3.2</version>
 <configuration>
  <source>1.8</source>
  <target>1.8</target>
 </configuration>
 </plugin>
 </plugins>
 </build>
</project>

其实只是加入了 spring-boot-autoconfigure

App文件中的main方法, 我注释掉了, 这个在这里没有用到

2. 配置属性对应的接收文件

package org.elvin;
import org.springframework.boot.context.properties.ConfigurationProperties;/**
 * author: Elvin
 * Date: 2017/12/12 14:51
 * Description:
 */
@ConfigurationProperties(prefix = "hello")
public class HelloServiceProperties {
 //默认配置内容
 private static final String MSG = "world";
 private String msg = MSG;
 public String getMsg() {
 return msg;
 }
 public void setMsg(String msg) {
 this.msg = msg;
 }
}

3. 对外Service

package org.elvin;
/**
 * author: Elvin
 * Date: 2017/12/12 14:55
 * Description:
 */
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;
 }
}

4. 对外service与配置对应文件关联

package org.elvin;
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;
/**
 * author: Elvin
 * Date: 2017/12/12 14:59
 * Description:
 */
@Configuration
@EnableConfigurationProperties(HelloServiceProperties.class)
@ConditionalOnClass(HelloService.class)
@ConditionalOnProperty(prefix = "hello", value="enabled", matchIfMissing =true )
public class HelloServiceAutoConfiguration {
 @Autowired
 private HelloServiceProperties helloServiceProperties;
 @Bean
 @ConditionalOnMissingBean(HelloService.class)
 public HelloService helloService(){
 HelloService helloService = new HelloService();
 helloService.setMsg(helloServiceProperties.getMsg());
 return helloService;
 }
}

5. starter配置 : spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.elvin.HelloServiceAutoConfiguration

做完这些之后, 通过 mvn clean install , 打包到maven库里面

二. spring boot 项目使用

新建一个spring boot 项目, 选择web即可.

目录结构:

先看一下引用pom.xml

<dependency>
   <groupId>org.elvin</groupId>
   <artifactId>my-spring-boot-starter</artifactId>
   <version>1.0-SNAPSHOT</version>
  </dependency>

再看一下HelloController

package org.elvin.learn.springboot.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.elvin.*;
/**
 * author: Elvin
 * Date: 2017/12/12 15:34
 * Description:
 */
@RestController
@RequestMapping("hello")
public class HelloController {
 @Autowired
 private HelloService helloService;
 @RequestMapping("index")
 public String index(){
  return helloService.sayHello();
 }
}

这里的 HelloService 就是 前面自定义 starter 里面的.

1. 结果: 未配置情况下, 应该是输出 hello world

2. 在配置文件中, 加入 hello.msg=hahahahahah

这个例子很简单, 只是显示一下主要的过程, 别的都是各插件自己的逻辑判断了.

参考资料:

JavaEE开发的颠覆者 Spring Boot实战

以上这篇spring boot 自定义starter的实现教程就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Java数据类型的规则

这篇文章主要介绍了Java数据类型的规则的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Spring整合TimerTask实现定时任务调度

这篇文章主要介绍了Spring整合TimerTask实现定时任务调度的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解SpringMVC使用MultipartFile实现文件的上传

本篇文章主要介绍了SpringMVC使用MultipartFile实现文件的上传,本地的文件上传到资源服务器上,比较好的办法就是通过ftp上传。这里是结合SpringMVC+ftp的形式上传的,有兴趣的可以了解一下。
收藏 0 赞 0 分享

SpringMVC上传文件的三种实现方式

本篇文章主要介绍了SpringMVC上传文件的三种实现方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

微信公众帐号开发-自定义菜单的创建及菜单事件响应的实例

本篇文章主要介绍了微信公众帐号开发-自定义菜单的创建及菜单事件响应的实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
收藏 0 赞 0 分享

浅析Java中的继承与组合

本文将介绍组合和继承的概念及区别,并从多方面分析在写代码时如何进行选择。文中通过示例代码介绍的很详细,有需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

利用反射获取Java类中的静态变量名及变量值的简单实例

下面小编就为大家带来一篇利用反射获取Java类中的静态变量名及变量值的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java启动线程的3种方式对比分析

这篇文章主要为大家对比分析了java启动线程的3种方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

SpringMVC上传和解析Excel方法

这篇文章主要介绍了SpringMVC上传和解析Excel方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

JAVA中String类与StringBuffer类的区别

这篇文章主要为大家详细介绍了JAVA中String类与StringBuffer类的区别,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多