一、前言
SpringBoot 通过约定大于配置的做法,内置了很多AutoConfiguration,让我们能够快速搭建Spring的Web应用,大大提升了开发效率。
该系列文章会分为如何编写,测试,集成,原来4块来逐步讲解SpringBoot自动配置相关的知识。
这里就让我们来看看,如何编写属于自己的AutoConfiguration吧。Let`s Go !!!
二、工程结构
三、修改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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-msg</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring.boot.version>2.0.2.RELEASE</spring.boot.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-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
四、编写自动配置类
@Configuration
@EnableConfigurationProperties(MsgProperties.class)
@ConditionalOnClass(MsgService.class)
@ConditionalOnProperty(prefix = "msg" , name = "enable" , matchIfMissing = true)
public class MsgAutoConfiguration {
@Bean
@ConditionalOnMissingBean(MsgService.class)
public MsgService msgBean(){
return new MsgService();
}
}
五、编写注入的Property类
package com.faker.framework.configure;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @Auther:
* @Date: 18/11/26 15:21
* @Description:
*/
@ConfigurationProperties(prefix = "msg")
public class MsgProperties {
/**
* 用户名
*/
private String username;
private String pwd;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
六、编写自动配置注入的Serivce
package com.faker.framework.configure;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @Auther:
* @Date: 18/6/13 15:12
* @Description:
*/
public class MsgService implements InitializingBean {
@Autowired
private MsgProperties msgProperties;
public void afterPropertiesSet() throws Exception {
System.out.println("username: "+msgProperties.getUsername());
System.out.println("pwd: "+msgProperties.getPwd());
}
}
七、在resource目录下添加打包配置
创建META-INF文件夹,在文件夹下创建spring.factories配置文件
并在改配置文件中添加内容与自己的自动配置类对应,这样在打包被引入后,SpringBoot能
识别你自定义的自动配置类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.faker.framework.configure.MsgAutoConfiguration