Spring Boot中的自定义start pom
start pom是springboot中提供的简化企业级开发绝大多数场景的一个工具,利用好strat pom就可以消除相关技术的配置得到自动配置好的Bean。
举个例子,在一般使用中,我们使用基本的spring-boot-starter配置基本的springboot项目,也使用spring-boot-starter-web去配置web项目,
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
在pom文件中,引入这个start pom,便可以方便我们快速的导包以及配置相关必要配置,能大大的简化我们的操作。
spring boot官方也给我们提供了很多start pom,当然除了官方的start pom外,我们也可以使用一些第三方为spring boot写的start pom。
本篇文章,则是介绍一个自己写start pom的方法,当能熟练使用这个之后,便可以实现自定义快速编程,敏捷开发了。
- 首先,先新建一个maven工程,在pom文件中引入spring-boot-autoconfigure依赖
pom文件
<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>site.wangxin520</groupId>
<artifactId>spring-boot-start-hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-start-hello</name>
<description>自定义的一个spring boot的start pom</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>1.5.8.RELEASE</version>
</dependency>
</dependencies>
</project>
新建的maven工程目录为

其中HelloService.java是我们需要在springboot中引入的实体类。
- HelloService.java如下
package site.wangxin520.spring_boot_start_hello; /**
* 这个是Bean类,用于在spring boot中使用的
*
* @author wangXgnaw
*
*/
public class HelloService { // 私有属性,与下面的set和get方法一起,用于注入
private String msg; /**
* 打招呼的类,用于在spring boot中调用
*
* @return
*/
public String sayHello() {
return "hello " + msg;
} public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
} }
- HelloServiceProperties.java相当于是读取到的application.properties中的配置注入到HelloService中,或者说是当没有配置的话,就注入默认配置
package site.wangxin520.spring_boot_start_hello.properties; import org.springframework.boot.context.properties.ConfigurationProperties; /**
* 这个是类型安全属性的获取,用于配置自动注入bean的参数
*
* @author wangXgnaw
*
*/
@ConfigurationProperties(prefix = "hello")
public class HelloServiceProperties { // 默认的配置的值
private final static String MSG = "wangxin"; // 由于在configuationProperties中配置了前缀,所以可以在application.properties中使用hello.msg配置该值。
private String msg = MSG; // 这里的get和set方法,是方便外部注入参数值用的,需要注意的是这里和HelloService不同,并没有让properties中的参数直接注入到HelloService中
public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
} }
- HelloServiceAutoconfiguration.java是自动注入的一个配置,与spring相关
package site.wangxin520.spring_boot_start_hello.autoconf; 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; import site.wangxin520.spring_boot_start_hello.HelloService;
import site.wangxin520.spring_boot_start_hello.properties.HelloServiceProperties; /**
* 自动配置类,给bean注入参数
*
* @author wangXgnaw
*
*/
@Configuration // 标记当前类是配置类
@EnableConfigurationProperties(HelloServiceProperties.class) // 使用java类作为配置文件
@ConditionalOnClass(HelloService.class) // 需要被配置的类
@ConditionalOnProperty(prefix = "hello", value = "enable", matchIfMissing = true)
public class HelloServiceAutoconfiguration { // 自动注入配置
@Autowired
private HelloServiceProperties helloServiceProperties; /**
* 给bean注入参数,同时返回一个bean实例
* 同时注解表名,返回是一个bean实例
* 当容器中没有这个bean实例的时候,就返回一个自动注入好参数的bean实例回去
* @return HelloService
*/
@Bean
@ConditionalOnMissingBean(HelloService.class)
public HelloService helloService() {
HelloService helloService = new HelloService();
helloService.setMsg(helloServiceProperties.getMsg());
return helloService;
} }
- spring.factories在src/main/resources中,添加一个文件夹,即META-INF,然后添加一个文件spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=site.wangxin520.spring_boot_start_hello.autoconf.HelloServiceAutoconfiguration
这个文件里面,标注出自动注入的配置文件
以上就是全部的start pom案例,当写完后,使用maven的install安装到本地仓库后,在创建好springboot之后,添加上依赖,就可以很方便的使用了。
下面是springboot中引用自定义startpom依赖的实例:
- 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>site.wangxin520</groupId>
<artifactId>springboot-starter-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>springboot-starter-test</name>
<description>springboot学习</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.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.7</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-starter-web</artifactId>
</dependency>
<dependency>
<groupId>site.wangxin520</groupId>
<artifactId>spring-boot-start-hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
代码中标黄了的就是我们自定义的一个startpom依赖坐标。
- 在启动类中,我们就可以使用自定义的helloservice类了。
package site.wangxin520.springbootstartertest; 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; import site.wangxin520.spring_boot_start_hello.HelloService; @SpringBootApplication
@RestController
public class SpringbootStarterTestApplication { @Autowired
private HelloService helloService; @RequestMapping("/")
public String index(){
return helloService.sayHello();
} public static void main(String[] args) {
SpringApplication.run(SpringbootStarterTestApplication.class, args);
}
}
- 实际操作:

可见,已经自动注入进去了。当我们在application.properties中自行配置的时候,如下

- 得到结果为

可见,我们自定义的start pom成功了!
Spring Boot中的自定义start pom的更多相关文章
- 如何优雅地在 Spring Boot 中使用自定义注解,AOP 切面统一打印出入参日志 | 修订版
欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...
- Spring Boot 中关于自定义异常处理的套路!
在 Spring Boot 项目中 ,异常统一处理,可以使用 Spring 中 @ControllerAdvice 来统一处理,也可以自己来定义异常处理方案.Spring Boot 中,对异常的处理有 ...
- Spring Boot中如何自定义starter?
Spring Boot starter 我们知道Spring Boot大大简化了项目初始搭建以及开发过程,而这些都是通过Spring Boot提供的starter来完成的.品达通用权限系统就是基于Sp ...
- 在spring boot中使用自定义的properties
1 在application.properties中添加 android.name=Tim android.password=123456 新建一个保存该Setting的配置类, @Configura ...
- Spring Boot 中使用自定义注解,AOP 切面打印出入参日志及Dubbo链路追踪透传traceId
一.使用背景 开发排查系统问题用得最多的手段就是查看系统日志,在分布式环境中一般使用 ELK 来统一收集日志,但是在并发大时使用日志定位问题还是比较麻烦,由于大量的其他用户/其他线程的日志也一起输出穿 ...
- Spring Boot中自定义注解+AOP实现主备库切换
摘要: 本篇文章的场景是做调度中心和监控中心时的需求,后端使用TDDL实现分表分库,需求:实现关键业务的查询监控,当用Mybatis查询数据时需要从主库切换到备库或者直接连到备库上查询,从而减小主库的 ...
- Spring Boot中使用MongoDB数据库
前段时间分享了关于Spring Boot中使用Redis的文章,除了Redis之后,我们在互联网产品中还经常会用到另外一款著名的NoSQL数据库MongoDB. 下面就来简单介绍一下MongoDB,并 ...
- Spring Boot2 系列教程(六)自定义 Spring Boot 中的 starter
我们使用 Spring Boot,基本上都是沉醉在它 Stater 的方便之中.Starter 为我们带来了众多的自动化配置,有了这些自动化配置,我们可以不费吹灰之力就能搭建一个生产级开发环境,有的小 ...
- Spring Boot2 系列教程(十八)Spring Boot 中自定义 SpringMVC 配置
用过 Spring Boot 的小伙伴都知道,我们只需要在项目中引入 spring-boot-starter-web 依赖,SpringMVC 的一整套东西就会自动给我们配置好,但是,真实的项目环境比 ...
随机推荐
- Windows 2012 R2 安装Nessus
1.nessus官网注册 注册地址:https://www.tenable.com/products/nessus-home Name字段随意,邮箱需要填写自己的,方便接受注册码 2.注册后,登录邮箱 ...
- 执行RF测试只生成output.xml文件,不生成log和report文件
命令格式: -l --log file HTML log file. Can be disabled by giving a special value `NONE`. Default: log.ht ...
- 【Java并发编程一】线程安全和共享对象
一.什么是线程安全 当多个线程访问一个类时,如果不用考虑这些线程在运行时环境下的调度和交替执行,并且不需要额外的同步及在调用代码代码不必作其他的协调,这个类的行为仍然是正确的,那么称这个类是线程安全的 ...
- Android textview及其子类
属性: android:autoLink设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接.可选值(none/web/email/phone/map/all) andr ...
- Delphi应用程序的调试(二)使用断点
Delphi应用程序的调试(二)使用断点 使用断点(Using Breakpoints) 当用户从Delphi IDE 运行程序时,程序全速运行,只会在设置了断点的地方停住. New Term 断点( ...
- java基础---->git的使用(一)
这里面记录一下git的使用,只是平时工作中遇到的一些问题的解决方案,不会涉及到git的一些基础概念及说明.人的天性便是这般凉薄,只要拿更好的来换,一定舍得. Git的一些使用 一.在码云建立好仓库之后 ...
- 【基础】java类的各种成员初始化顺序
父子类继承时的静态代码块,普通代码块,静态方法,构造方法,等先后顺序 前言: 普通代码块:在方法或语句中出现的{}就称为普通代码块.普通代码块和一般的语句执行顺序由他们在代码中出现的次序决定--“先出 ...
- Eclipse中如何在指定工程中搜索指定的字符串
1.点击Search: 2.在下拉框中先择Search. 3. 4. 5.选择Java 6.
- git上传的文件夹为空的时候
1,先删除空的文件夹 参考:https://www.cnblogs.com/wang715100018066/p/9694532.html 2,这个只能说是技巧不能说是方法,原理是在每个空文件夹新建一 ...
- LeetCode 38 Count and Say(字符串规律输出)
题目链接:https://leetcode.com/problems/count-and-say/?tab=Description 1—>11—>21—>1211—>111 ...