您的位置:首页 > 编程语言 > Java开发

自己写一个spring boot starter

2020-10-28 21:41 127 查看

(1) 新建项目
项目名:formatter-spring-boot-starter

(2) 定义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">
<parent>
<artifactId>first-app-by-gui</artifactId>
<groupId>thinking-in-spring-boot</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>formatter-spring-boot-starter</artifactId>

<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.4.RELEASE</version>
<!--            细节,防止依赖传递,避免其他项目引用该项目之后版本冲突-->
<optional>true</optional>
</dependency>

</dependencies>
</project>

(3) 写一个接口和实现

public interface Formatter {

String format(Object object);
}
public class DefaultFormatter implements Formatter{

@Override
public String format(Object object) {
return String.valueOf(object);
}
}

(4) 配置类

@Configuration
public class FormatterAutoConfiguration {

@Bean
public Formatter defaultFormatter(){
return new DefaultFormatter();
}
}

(5) 配置文件
写一个META-INF/spring.factories文件,内容如下:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=thinking.in.spring.boot.config.FormatterAutoConfiguration

完成之后如果需要使用就引入这个项目,就可以直接使用里面的接口了.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: