用过springboot的自动配置会觉得非常方便,我们完全可以自己写一个starter pom,这样不仅可以有自动配置功能,而且具有更通用的的耦合度低的配置,

  • 新建一个starter的maven项目,以idea为例。

然后next-填好项目名字和路径finish。建好项目。

在pom.xml中加入springboot的自动配置依赖,代码如下

<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>com.demo</groupId>
<artifactId>spring-boot-starter-hello</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging> <name>spring-boot-starter-hello</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
  • 属性配置类,代码如下:
@ConfigurationProperties(prefix = "hello")
public class HelloServiceProperties {
private static final String MSG = "world"; private String msg = MSG; public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
}
}
这里的配置类是类型安全的属性获取。在application.properties 中通过hello.msg=world。
  • 判断依据类,代码如下:
public class HelloService {
private String msg; public String sayHello() {
return "hello " + msg;
} public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
}
}

要根据这个类类判断是否创建这个类的bean,这个类可以是第三方类库的类。

  • 自动配置类
@Configuration
@EnableConfigurationProperties(HelloServiceProperties.class)
@ConditionalOnClass(HelloService.class)
@ConditionalOnProperty(prefix = "hello",value = "enabled",matchIfMissing = true)
public class HelloServiceAutoConfiguration {
@Autowired
private HelloServiceProperties helloServiceProperties;
@Bean
@ConditionalOnMissingBean(HelloService.class)
public HelloService helloService(){
HelloService helloService = new HelloService();
helloService.setMsg(helloServiceProperties.getMsg());
return helloService;
}
}

代码解释:

根据HelloServiceProperties提供的参数,并通过@ConditionalOnClass判断HelloService这个类在类路径是否存在,且当容器中没有这个bean的情况下自动配置这个bean。

  • 注册配置,若想自动配置生效,需要注册自动配置类,在src/main/resources下创建META-INF/spring.factories,结构如下图所示:

spring.factories中填写如下内容注册

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\

com.demo.HelloServiceAutoConfiguration

若有多个配置用“,”隔开,“\”是为了换行后能读到属性。

  • 使用stater。我们可以将spring-boot-starter-hello 通过“mvn install”安装到本地库或者发布的maven私服使用。

mvn install:install-file -Dfile=jar包的位置 -DgroupId=groupId -DartifactId=artifactId -Dversion=version -Dpackaging=jar

新建springboot项目,并将我们的starter作为依赖,代码如下:

测试代码如下:

@Controller
public class IndexController { @Autowired
HelloService helloService; @RequestMapping("/starter")
public @ResponseBody String sayHello(){
return helloService.sayHello();
}
}

在代码中直接注入HelloService的bean,但在项目中,我们没有配置这个bean。这个是通过自动配置完成的。

访问后效果如下:

然后在application.properties中配置

hello.msg= spring boot

再次访问效果如下:

在application.properties中配置debug=true,可以看到自动配置的报告。

Spring boot 自定义一个starter pom的更多相关文章

  1. Spring Boot自定义starter必知必会条件

    前言 在目前的Spring Boot框架中,不管是Spring Boot官方还是非官方,都提供了非常多的starter系列组件,助力开发者在企业应用中的开发,提升研发人员的工作效率,Spring Bo ...

  2. spring cloud教程之使用spring boot创建一个应用

    <7天学会spring cloud>第一天,熟悉spring boot,并使用spring boot创建一个应用. Spring Boot是Spring团队推出的新框架,它所使用的核心技术 ...

  3. 如何基于Spring Boot搭建一个完整的项目

    前言 使用Spring Boot做后台项目开发也快半年了,由于之前有过基于Spring开发的项目经验,相比之下觉得Spring Boot就是天堂,开箱即用来形容是绝不为过的.在没有接触Spring B ...

  4. 玩转Spring Boot 自定义配置、导入XML配置与外部化配置

    玩转Spring Boot 自定义配置.导入XML配置与外部化配置       在这里我会全面介绍在Spring Boot里面如何自定义配置,更改Spring Boot默认的配置,以及介绍各配置的优先 ...

  5. 峰哥说技术:06-手撸Spring Boot自定义启动器,解密Spring Boot自动化配置原理

    Spring Boot深度课程系列 峰哥说技术—2020庚子年重磅推出.战胜病毒.我们在行动 06  峰哥说技术:手撸Spring Boot自定义启动器,解密Spring Boot自动化配置原理 Sp ...

  6. Using Spring Boot without the parent POM

    Using Spring Boot without the parent POM: 问题 spring boot项目一般情况下的parent如下: <parent> <groupId ...

  7. spring boot自定义线程池以及异步处理

    spring boot自定义线程池以及异步处理@Async:什么是线程池?线程池是一种多线程处理形式,处理过程中将任务添加到队列,然后在创建线程后自动启动这些任务.线程池线程都是后台线程.每个线程都使 ...

  8. Spring Boot 2.X(四):Spring Boot 自定义 Web MVC 配置

    0.准备 Spring Boot 不仅提供了相当简单使用的自动配置功能,而且开放了非常自由灵活的配置类.Spring MVC 为我们提供了 WebMvcConfigurationSupport 类和一 ...

  9. Spring Boot 自定义kafka 消费者配置 ContainerFactory最佳实践

    Spring Boot 自定义kafka 消费者配置 ContainerFactory最佳实践 本篇博文主要提供一个在 SpringBoot 中自定义 kafka配置的实践,想象这样一个场景:你的系统 ...

随机推荐

  1. CF528E Triangles 3000

    cf luogu 既然要求三角形面积,不如考虑三角形的面积公式.因为是三条直线,所以可以考虑利用三个交点来算面积,如果这个三角形按照逆时针方向有\(ABC\)三点,那么他的面积为\(\frac{\ve ...

  2. 运维dig语法

    dig命令是常用的域名查询工具,可以用来测试域名系统工作是否正常 语法 1 dig(选项)(参数) 选项 1 @<服务器地址>:指定进行域名解析的域名服务器: 2 -b<ip地址&g ...

  3. java中的集合类详情解析以及集合和数组的区别

    数组和链表 数组:所谓数组就是相同数据类型的元素按照一定顺序排列的集合. 它的存储区间是连续的,占用内存严重,所以空间复杂度很大,为o(n),但是数组的二分查找时间复杂度很小为o(1). 特点是大小固 ...

  4. ORA-01846: 周中的日无效

    参考这篇博客:https://blog.csdn.net/yabingshi_tech/article/details/8678218

  5. 60. Permutation Sequence (JAVA)

    The set [1,2,3,...,n] contains a total of n! unique permutations. By listing and labeling all of the ...

  6. C++ ->error LNK1123

    终极解决方案:VS2010在经历一些更新后,建立Win32 Console Project时会出“error LNK1123” 错误,解决方案为将 项目|项目属性|配置属性|清单工具|输入和输出|嵌入 ...

  7. java高并发核心要点|系列文章

    java高并发核心要点|系列1|开篇 java高并发核心要点|系列2|锁的底层实现原理 java高并发核心要点|系列3|锁的底层实现原理|ABA问题 java高并发核心要点|系列4|CPU内存指令重排 ...

  8. u-boot include目录 gd_t结构体 如何关联芯片指定的目录

    1 u-boot /u-boot-2018.07-fmxx/include/config.h /* Automatically generated - do not edit */#define CO ...

  9. solaris11 format zpool

    # format AVAILABLE DISK SELECTIONS:0. c1t0d0 <LSI-MR9261-8i-2.12-557.86GB>/pci@0,0/pci8086,3c0 ...

  10. python数据类型基础与解压缩

    ''' python数据类型基础与解压缩 ''' # a = 10 # b = 10 # c = 10 # 定义变量就是拿来用的, # 链式赋值 a = b = c = 10 print(a, b, ...