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的更多相关文章

  1. 如何优雅地在 Spring Boot 中使用自定义注解,AOP 切面统一打印出入参日志 | 修订版

    欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...

  2. Spring Boot 中关于自定义异常处理的套路!

    在 Spring Boot 项目中 ,异常统一处理,可以使用 Spring 中 @ControllerAdvice 来统一处理,也可以自己来定义异常处理方案.Spring Boot 中,对异常的处理有 ...

  3. Spring Boot中如何自定义starter?

    Spring Boot starter 我们知道Spring Boot大大简化了项目初始搭建以及开发过程,而这些都是通过Spring Boot提供的starter来完成的.品达通用权限系统就是基于Sp ...

  4. 在spring boot中使用自定义的properties

    1 在application.properties中添加 android.name=Tim android.password=123456 新建一个保存该Setting的配置类, @Configura ...

  5. Spring Boot 中使用自定义注解,AOP 切面打印出入参日志及Dubbo链路追踪透传traceId

    一.使用背景 开发排查系统问题用得最多的手段就是查看系统日志,在分布式环境中一般使用 ELK 来统一收集日志,但是在并发大时使用日志定位问题还是比较麻烦,由于大量的其他用户/其他线程的日志也一起输出穿 ...

  6. Spring Boot中自定义注解+AOP实现主备库切换

    摘要: 本篇文章的场景是做调度中心和监控中心时的需求,后端使用TDDL实现分表分库,需求:实现关键业务的查询监控,当用Mybatis查询数据时需要从主库切换到备库或者直接连到备库上查询,从而减小主库的 ...

  7. Spring Boot中使用MongoDB数据库

    前段时间分享了关于Spring Boot中使用Redis的文章,除了Redis之后,我们在互联网产品中还经常会用到另外一款著名的NoSQL数据库MongoDB. 下面就来简单介绍一下MongoDB,并 ...

  8. Spring Boot2 系列教程(六)自定义 Spring Boot 中的 starter

    我们使用 Spring Boot,基本上都是沉醉在它 Stater 的方便之中.Starter 为我们带来了众多的自动化配置,有了这些自动化配置,我们可以不费吹灰之力就能搭建一个生产级开发环境,有的小 ...

  9. Spring Boot2 系列教程(十八)Spring Boot 中自定义 SpringMVC 配置

    用过 Spring Boot 的小伙伴都知道,我们只需要在项目中引入 spring-boot-starter-web 依赖,SpringMVC 的一整套东西就会自动给我们配置好,但是,真实的项目环境比 ...

随机推荐

  1. 【安全开发】C/C++安全编码规范

    C本质上是不安全的编程语言.例如如果不谨慎使用的话,其大多数标准的字符串库函数有可能被用来进行缓冲区攻击或者格式字符串攻击.但是,由于其灵活性.快速和相对容易掌握,它是一个广泛使用的编程语言.下面是针 ...

  2. Linux应急响应(三):挖矿病毒

    0x00 前言 ​ 随着虚拟货币的疯狂炒作,利用挖矿脚本来实现流量变现,使得挖矿病毒成为不法分子利用最为频繁的攻击方式.新的挖矿攻击展现出了类似蠕虫的行为,并结合了高级攻击技术,以增加对目标服务器感染 ...

  3. PHP代码审计笔记--文件包含漏洞

    有限制的本地文件包含: <?php include($_GET['file'].".php"); ?> %00截断: ?file=C://Windows//win.in ...

  4. Jsoup(一)-- HelloWorld

    1.简介 jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQuery的操作方法来取出和操作数据. ...

  5. Android应用程序的结构

    1.src目录 存放该项目的源代码 2.gen目录 该目录文件是ADT自动生成的,并不需要认为地去修改 3.Android2.1 该目录存放的是该项目支持的JAR包,同时还包含项目打包时需要的META ...

  6. Java API方式调用Kafka各种协议

    众所周知,Kafka自己实现了一套二进制协议(binary protocol)用于各种功能的实现,比如发送消息,获取消息,提交位移以及创建topic等.具体协议规范参见:Kafka协议  这套协议的具 ...

  7. iOS - AVAudioSession详解

    音频输出作为硬件资源,对于iOS系统来说是唯一的,那么要如何协调和各个App之间对这个稀缺的硬件持有关系呢? iOS给出的解决方案是"AVAudioSession" ,通过它可以实 ...

  8. 【架构师之路】 LVS+Keepalived实现高可用负载均衡

    一.原理        1.概要介绍        如果将TCP/IP划分为5层,则Keepalived就是一个类似于3~5层交换机制的软件,具有3~5层交换功能,其主要作用是检测web服务器的状态, ...

  9. vue经验 - 那些自己给自己挖的深坑

    深坑场景:vue-异步请求数据,数据还没回来,页面却如饥似渴的准备好了的尴尬场景:问题原因和解决如下: 1.先说vuex中的store,一开始我为了偷懒是这么设置的,如下图: 然后我到了组件中直接这么 ...

  10. C#设计模式--设配器模式

    0.C#设计模式-简单工厂模式 1.C#设计模式--工厂方法模式 2.C#设计模式--抽象工厂模式 3.C#设计模式--单例模式 4.C#设计模式--建造者模式 5.C#设计模式--原型模式 设计模式 ...