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 的一整套东西就会自动给我们配置好,但是,真实的项目环境比 ...
随机推荐
- Linux curl 命令下载文件
引用自http://blog.csdn.net/wh211212/article/details/54285921 命令:curl 在Linux中curl是一个利用URL规则在命令行下工作的文件传输工 ...
- 【MacOS】brew-python3
mkdir homebrew && curl -L https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 ...
- 【GIS】ArcGIS JS 4.X
require(["esri/Map", "esri/views/SceneView", "esri/TileLayer/TdtMapLayer/Td ...
- informix中的时间计算
今天看SUN服务器是的mail(vi /var/mail/xxxuser),发现定时任务上的一些存储过程执行有错误,其中有一个错误是long transaction,长事务错误,到数据库一查,天哪 ...
- [OSX] 使用 MacPorts 安装 Python 和 pip 指南
Mac OS 未预装任何在 Unix/Linux 中常见的命令行包管理工具,Mac OS 中的 App Store 和自身的软件升级功能可以下载更新许多比较好的应用,但这些应用多数是满足普通消费者需求 ...
- 《shiro框架》
20170929 shiro授权流程学习 shiro-filter执行流程 CacheManager(shiro缓存管理) JEESITE登录流程简单梳理 shiro与springMVC整合 shir ...
- <转>13个实用的Linux find命令示例
注:本文摘自青崖白鹿,翻译的妈咪,我找到了! -- 15个实用的Linux find命令示例, 感谢翻译的好文. 除了在一个目录结构下查找文件这种基本的操作,你还可以用find命令实现一些实用的操作, ...
- 分布式实时日志系统(二) 环境搭建之 flume 集群搭建/flume ng资料
最近公司业务数据量越来越大,以前的基于消息队列的日志系统越来越难以满足目前的业务量,表现为消息积压,日志延迟,日志存储日期过短,所以,我们开始着手要重新设计这块,业界已经有了比较成熟的流程,即基于流式 ...
- 关于 ArrayList.toArray() 和 Arrays.asList().toArray()方法
1.ArrayList.toArray() 理解 * 通过源码我们可以看到返回的是Object类型的数组,失去了原有的实际类型,虽然底层存储是具体类型的对象,这也正体现了文档中说的:该方法起到了 ...
- 查看JVM使用的默认的垃圾收集器
一.查看步骤 cmd执行命令: java -XX:+PrintCommandLineFlags -version 输出如下(举例): 针对上述的-XX:UseParallelGC,这边我们引用< ...