为了更好的理解 Spring Boot 的 自动配置和工作原理,我们自己来实现一个简单的自动配置模块。
假设,现在项目需要一个功能,需要自动记录项目发布者的相关信息,我们如何通过 Spring Boot 的自动配置,更好的实现功能呢?
实战的开端 – Maven搭建
先创建一个Maven项目,我来手动配置下 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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<groupId>com.lianggzone.demo</groupId>
<artifactId>springboot-action-autoconfig</artifactId>
<version>0.1</version>
<packaging>jar</packaging>
<name>springboot-action-autoconfig</name>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
参数的配置 - 属性参数类
首先,我们定义一个自定义前缀,叫做 custom 吧。之前说到,这里的配置参数,可以通过 application.properties 中直接设置。那么,我们创建一个作者的字段,设置默认值为 LiangGzone。
@ConfigurationProperties(prefix = "custom")
public class AuthorProperties {
public static final String DEFAULT_AUTHOR = "LiangGzone";
public String author = DEFAULT_AUTHOR;
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
那么,聪明的你,应该想到了,我们在 application.properties 中配置的时候,就要这样配置了。
#custom custom.author = 梁桂钊
真的很简单 - 简单的服务类
public class AuthorServer {
public String author;
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
你没有看错,真的是太简单了,没有高大上的复杂业务。它的主要用途就是赋值。
自动配置的核心 - 自动配置类
@ConditionalOnClass,参数中对应的类在 classpath 目录下存在时,才会去解析对应的配置类。因此,我们需要配置 AuthorServer 。
@EnableConfigurationProperties, 用来加载配置参数,所以它应该就是属性参数类 AuthorProperties。
@Configuration
@ConditionalOnClass({ AuthorServer.class })
@EnableConfigurationProperties(AuthorProperties.class)
public class AuthorAutoConfiguration {
@Resource
private AuthorProperties authorProperties;
@Bean
@ConditionalOnMissingBean(AuthorServer.class)
@ConditionalOnProperty(name = "custom.author.enabled", matchIfMissing = true)
public AuthorServer authorResolver() {
AuthorServer authorServer = new AuthorServer();
authorServer.setAuthor(authorProperties.getAuthor());
return authorServer;
}
}
authorResolver方法的作用,即 AuthorProperties 的参数赋值到AuthorServer 中。
spring.factories 不要遗漏
我们需要实现自定义自动装配,就需要自定义 spring.factories 参数。所以,我们需要在 src/main/resources/ META-INF/spring.factories 中配置信息,值得注意的是,这个文件要自己创建。
# CUSTOM
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.lianggzone.springboot.autoconfig.author.AuthorAutoConfiguration
功能打包与配置依赖
好了,我们已经实现了一个简单的自动配置功能。那么,我们需要将这个项目打成 jar 包部署在我们的本地或者私服上。然后,就可以用了。
我们在另外一个项目中,配置 Maven 依赖。
<dependency>
<groupId>com.lianggzone.demo</groupId>
<artifactId>springboot-action-autoconfig</artifactId>
<version>0.1</version>
</dependency>
测试,测试
@RestController
@EnableAutoConfiguration
public class AuthorAutoConfigDemo {
@Resource
private AuthorServer authorServer;
@RequestMapping("/custom/author")
String home() {
return "发布者:"+ authorServer.getAuthor();
}
}
运行起来,我们看下打印的发布者信息是什么?
我们在 application.properties 中配置一个信息。
#custom custom.author = 梁桂钊
运行起来,我们看下打印的发布者信息是什么?
原文链接:http://www.open-open.com/lib/view/open1483317440426.html