Spring Boot 揭秘与实战 自己实现一个简单的自动配置模块
2017-07-28 17:19
1096 查看
文章目录
1. 实战的开端 – Maven搭建
2. 参数的配置 - 属性参数类
3. 真的很简单 - 简单的服务类
4. 自动配置的核心 - 自动配置类
5. spring.factories 不要遗漏
6. 功能打包与配置依赖
7. 测试,测试
8. 源代码
书接上回,《Spring Boot 揭秘与实战 源码分析 - 工作原理剖析》。为了更好的理解 Spring Boot 的 自动配置和工作原理,我们自己来实现一个简单的自动配置模块。
假设,现在项目需要一个功能,需要自动记录项目发布者的相关信息,我们如何通过 Spring Boot 的自动配置,更好的实现功能呢?
<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>
@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 String author;
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
你没有看错,真的是太简单了,没有高大上的复杂业务。它的主要用途就是赋值。
@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 中。
# CUSTOM
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.lianggzone.springboot.autoconfig.author.AuthorAutoConfiguration
我们在另外一个项目中,配置 Maven 依赖。
<dependency>
<groupId>com.lianggzone.demo</groupId>
<artifactId>springboot-action-autoconfig</artifactId>
<version>0.1</version>
</dependency>
@EnableAutoConfiguration
public class AuthorAutoConfigDemo {
@Resource
private AuthorServer authorServer;
@RequestMapping("/custom/author")
String home() {
return "发布者:"+ authorServer.getAuthor();
}
}
运行起来,我们看下打印的发布者信息是什么?
我们在 application.properties 中配置一个信息。
#custom
custom.author = 梁桂钊
运行起来,我们看下打印的发布者信息是什么?
(完)
如果觉得我的文章对你有帮助,请随意打赏。
版权声明:本文由 梁桂钊 发表于 梁桂钊的博客
转载声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证),非商业转载请注明作者及出处,商业转载请联系作者本人。
文章标题:Spring Boot 揭秘与实战 自己实现一个简单的自动配置模块
文章链接:http://blog.720ui.com/2017/springboot_source_autoconfigure_custom/
1. 实战的开端 – Maven搭建
2. 参数的配置 - 属性参数类
3. 真的很简单 - 简单的服务类
4. 自动配置的核心 - 自动配置类
5. spring.factories 不要遗漏
6. 功能打包与配置依赖
7. 测试,测试
8. 源代码
书接上回,《Spring Boot 揭秘与实战 源码分析 - 工作原理剖析》。为了更好的理解 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 = 梁桂钊
运行起来,我们看下打印的发布者信息是什么?
源代码
(完)
如果觉得我的文章对你有帮助,请随意打赏。
版权声明:本文由 梁桂钊 发表于 梁桂钊的博客
转载声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证),非商业转载请注明作者及出处,商业转载请联系作者本人。
文章标题:Spring Boot 揭秘与实战 自己实现一个简单的自动配置模块
文章链接:http://blog.720ui.com/2017/springboot_source_autoconfigure_custom/
相关文章推荐
- springboot源码解析:自己实现一个springboot自动配置
- 初识Spring Boot框架(二)之DIY一个Spring Boot的自动配置
- 初识Spring Boot框架(二)之DIY一个Spring Boot的自动配置
- Spring Boot实战之netty-socketio实现简单聊天室(给指定用户推送消息)
- Spring Boot 揭秘与实战 附录 - Spring Boot 公共配置
- 一个简单的springboot的案例实现一个请求控制层返回一个JSON
- 初识Spring Boot框架(二)之DIY一个Spring Boot的自动配置
- Spring boot实现一个简单的ioc(1)
- [Spring Boot] 4. Spring Boot实现自动配置的原理
- Spring boot实现一个简单的ioc(2)
- 关于S5PV210的启动问题和u-boot分析并自己实现一个简单的boot(一)
- Spring Boot实战教程之自动配置详解
- 学记:为spring boot写一个自动配置
- SpringBoot自动配置的实现原理
- Spring Boot实战之netty-socketio实现简单聊天室(给指定用户推送消息)
- 初识Spring Boot框架(二)之DIY一个Spring Boot的自动配置
- SpringBoot+Maven项目实战(6):整合Log4j和Aop,实现简单的日志记录
- SpringBoot学习笔记(5) Spring Boot集成Redis实现自动配置
- [Spring Boot] 3. Spring Boot实现自动配置的基础
- Spring Boot 自动配置理解 以及实现自定义Starter