博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot 自定义starter
阅读量:7107 次
发布时间:2019-06-28

本文共 3544 字,大约阅读时间需要 11 分钟。

hot3.png

直接开门见山,要想自己定义一个starter,主要是分下面几步

第一步

首先要创建一个maven项目,这里我提供一个传送门,,这样可以很轻松地创建一个maven项目,并引入到自己的IDE里头

创建maven项目

第二步

既然已经创建好了maven项目,那么就可以开始写自定义的starter了,这里主要分三个点来叙述

1.创建功能业务类

package spring.boot.starter.time.utils;import java.util.Date;import org.joda.time.DateTime;import org.joda.time.Seconds;public class TimeUtils {    private static final String MINUTE_TEXT = "分钟前";    private static final String HOUR_TEXT = "小时前";    private static final String DAY_TEXT = "天前";    private static final int SECONDS_OF_MINUTE = 60;    private static final int SECONDS_OF_HOUR = 60 * 60;    private static final int SECONDS_OF_DAY = 24 * 60 * 60;    public String generateCreateTimeText(Date date) {        DateTime now = new DateTime();        DateTime createTime = new DateTime(date);        int intervalSeconds = Seconds.secondsBetween(createTime, now).getSeconds();        int minutes = (int) Math.ceil((double) intervalSeconds / SECONDS_OF_MINUTE);        if (minutes < 60) {            return String.format("%d%s", minutes, MINUTE_TEXT);        }        int hours = (int) Math.ceil((double) intervalSeconds / SECONDS_OF_HOUR);        if (hours < 24) {            return String.format("%d%s", hours, HOUR_TEXT);        }        return String.format("%d%s", (int) Math.ceil((double) intervalSeconds / SECONDS_OF_DAY), DAY_TEXT);    }}

2.创建自动配置类

package spring.boot.starter.time.utils;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class TimeUtilsAutoConfiguration {    @Bean    public TimeUtils convertChineseTime() {        return new TimeUtils();    }}

3.在src/main/resources目录下新建一个文件夹命名为META-INF,并在这个文件夹下面新建文件,名为spring.factories,这个文件会告诉Spring Boot去找指定的自动配置文件。在应用程序启动过程中,Spring Boot使用springfactoriesloader类加载器查找

# Auto Configureorg.springframework.boot.autoconfigure.EnableAutoConfiguration=\spring.boot.starter.time.utils.TimeUtilsAutoConfiguration#多个类用,\隔开

4.附pom.xml文件

4.0.0
spring.boot.demo
spring-boot-starter-time-utils
0.0.1-SNAPSHOT
jar
spring-boot-starter-time-utils
Demo starter project for Spring Boot
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot
1.4.3.RELEASE
joda-time
joda-time
2.9.7

到这里,第二步就完成了,意味着一个自定义的starter已经写好了

第三步

这里主要是通过别的项目用maven依赖的方式来调用上面已经自定义的starter

1.在需要调starter项目的pom文件中 引用starter

spring.boot.demo
spring-boot-starter-time-utils
0.0.1-SNAPSHOT

2.在controller里面调用自定义的starter

package spring.boot.demo.controller;import org.joda.time.DateTime;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import spring.boot.starter.time.utils.TimeUtils;@RestControllerpublic class TimeUtilsStarterController {    @Autowired    private TimeUtils timeUtils;    @RequestMapping("/time/utils/gain-now-time.htm")    public String gainNowTime() {        return timeUtils.generateCreateTimeText(new DateTime("2017-02-22T15:30:30").toDate());    }}

3.预览效果,这个时候启动该项目,然后在浏览器上输入 ,就可以看到效果了,如图

效果图

转载于:https://my.oschina.net/lsf930709/blog/843776

你可能感兴趣的文章
Numpy中的random模块中的seed方法的作用
查看>>
Tyvj1109|N阶幻方
查看>>
UESTC1263(贪心)
查看>>
phpstorm 8 license key
查看>>
模块和包
查看>>
时间戳计算
查看>>
Excel 开发备忘
查看>>
iOS 轻量级的数据库leveldb
查看>>
osx 10.11.5 El Capitan U盘制作安装
查看>>
[转载]Linux内核list_head学习(二)
查看>>
oracle表空间建立与用户创建删除
查看>>
typedef见解即如何定义一个数组指针
查看>>
Oracle中查询不出数据,为什么在程序中还能显示数据解决方法
查看>>
毕业了,少感慨,多努力
查看>>
C++——指针---指向数组的指针---指向字符串的指针--指向函数的指针--指针的指针--指针的引用...
查看>>
[Head First设计模式]云南米线馆中的设计模式——模版方法模式
查看>>
POI2012 (持续更新中)
查看>>
windows下ftp命令大全
查看>>
用好Word2007中的粘贴、拖动和放置工具
查看>>
数字音频解码国家实验室落户福建泉州
查看>>