承接前文springboot情操陶冶-@Configuration注解解析,本文将在前文的基础上阐述@ConfigurationProperties注解的使用

@ConfigurationProperties

此注解多用于对配置文件的加载以及映射对应值至相应的java属性中,样例如下


1.配置属性指定application.properties

# user custom
user.custom.username=demo_jing
user.custom.nickname=nanco
user.custom.email=questionsky1211@gmail.com
user.custom.password=demo1234
user.custom.job=programmer

2.属性映射类(使用@ConfigurationProperties注解)UserProperty.java

package com.example.demo.bootbase;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* @author nanco
* @create 2018/8/13
**/
@ConfigurationProperties(prefix = "user.custom")
public class UserProperty { private String username; private String nickname; private String email; private String password; private String job; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getNickname() {
return nickname;
} public void setNickname(String nickname) {
this.nickname = nickname;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getJob() {
return job;
} public void setJob(String job) {
this.job = job;
} @Override
public String toString() {
return "UserProperty{" +
"username='" + username + '\'' +
", nickname='" + nickname + '\'' +
", email='" + email + '\'' +
", password='" + password + '\'' +
", job='" + job + '\'' +
'}';
}
}

3.属性映射开启(使用@EnableConfigurationProperties注解)UserPropertiesAutoConfiguration.java

package com.example.demo.bootbase;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration; /**
* @author nanco
* @create 2018/8/13
**/
@Configuration
@EnableConfigurationProperties(value = UserProperty.class)
public class UserPropertiesAutoConfiguration {
}

4.上述注解生效入口META-INF\spring.fatories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.demo.bootbase.UserPropertiesAutoConfiguration

5.结果检验

package com.example.demo;

import com.example.demo.bootbase.UserProperty;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext; @SpringBootApplication
public class DemoSpringbootApplication { public static void main(String[] args) {
ApplicationContext demoApplicationContext = SpringApplication.run(DemoSpringbootApplication.class, args); UserProperty userProperty = demoApplicationContext.getBean(UserProperty.class) ; System.out.println(userProperty);
}
}

输出结果如下

...
...
2018-08-13 14:37:05.537 INFO 17384 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 2147483647
2018-08-13 14:37:05.546 INFO 17384 --- [ main] c.e.demo.DemoSpringbootApplication : Started DemoSpringbootApplication in 2.611 seconds (JVM running for 3.756)
UserProperty{username='demo_jing', nickname='nanco', email='questionsky1211@gmail.com', password='demo1234', job='programmer'}

出现的结果与我们预知的一样,属性都进行了相应的赋予。下面笔者开展下源码层的解析以解开其中的绑定小谜团

@EnableConfigurationProperties

要想上述的结果运行成功,必须在相应的启动类使用@EnableConfigurationProperties注解,我们先看下其源码

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(EnableConfigurationPropertiesImportSelector.class)
public @interface EnableConfigurationProperties { /**
* Convenient way to quickly register {@link ConfigurationProperties} annotated beans
* with Spring. Standard Spring Beans will also be scanned regardless of this value.
* @return {@link ConfigurationProperties} annotated beans to register
*/
Class<?>[] value() default {}; }

其中的value属性便是扫描的属性class,并且会将指定的class属性注册至bean工厂。通过前文分析可得,最终的解析是通过@Import引入的EnableConfigurationPropertiesImportSelector.class来实现的

EnableConfigurationPropertiesImportSelector

直接查看其复写的selectImports()方法

	private static final String[] IMPORTS = {
// 属性类注册
ConfigurationPropertiesBeanRegistrar.class.getName(),
// 属性类绑定处理
ConfigurationPropertiesBindingPostProcessorRegistrar.class.getName() }; @Override
public String[] selectImports(AnnotationMetadata metadata) {
return IMPORTS;
}

将导入两个类去处理属性的关系,我们按照顺序来分析


ConfigurationPropertiesBeanRegistrar

直接去看其复写的方法

		@Override
public void registerBeanDefinitions(AnnotationMetadata metadata,
BeanDefinitionRegistry registry) {
// 读取被注解类上的EnableConfigurationProperties上的value属性并进行注入至bean工厂
getTypes(metadata).forEach((type) -> register(registry,
(ConfigurableListableBeanFactory) registry, type));
}

具体的代码就不贴出来了,笔者此处作下小的总结

  1. 优先读取被注解类上@EnableConfigurationPropertiesvalue属性

  2. 在将上述的value属性指定的class集合注入至bean工厂前,优先判断是否被@ConfigurationProperties注解修饰过,没有则会报错

  3. 将指定的class集合注册至bean工厂


ConfigurationPropertiesBindingPostProcessorRegistrar

直接查看复写的方法

	@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
BeanDefinitionRegistry registry) {
if (!registry.containsBeanDefinition(
ConfigurationPropertiesBindingPostProcessor.BEAN_NAME)) {
// ConfigurationPropertiesBindingPostProcessor注册
registerConfigurationPropertiesBindingPostProcessor(registry);
// ConfigurationBeanFactoryMetadata注册
registerConfigurationBeanFactoryMetadata(registry);
}
}

主要注册了两个beanDefinition,分别是ConfigurationPropertiesBindingPostProcessor类和ConfigurationBeanFactoryMetadata类。

看来具体的属性绑定就是这两个类来处理了,笔者继续往下分析

ConfigurationBeanFactoryMetadata

根据spring的bean的生命周期,实现BeanFactoryPostProcessor接口类中的postProcessBeanFactory()方法优先执行

	@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
throws BeansException {
this.beanFactory = beanFactory;
for (String name : beanFactory.getBeanDefinitionNames()) {
BeanDefinition definition = beanFactory.getBeanDefinition(name);
String method = definition.getFactoryMethodName();
String bean = definition.getFactoryBeanName();
if (method != null && bean != null) {
this.beansFactoryMetadata.put(name, new FactoryMetadata(bean, method));
}
}
}

其主要用于获取bean工厂上实现被@Bean注解修饰的方法的bean对象,表明@ConfigurationProperties也可以用于修饰method方法上用于动态注入

ConfigurationPropertiesBindingPostProcessor

根据spring的bean的生命周期,首先我们看下其afterPropertiesSet()实现方法

	@Override
public void afterPropertiesSet() throws Exception {
// 获取ConfigurationBeanFactoryMetadata实体类
this.beanFactoryMetadata = this.applicationContext.getBean(
ConfigurationBeanFactoryMetadata.BEAN_NAME,
ConfigurationBeanFactoryMetadata.class);
// 创建属性绑定类
this.configurationPropertiesBinder = new ConfigurationPropertiesBinder(
this.applicationContext, VALIDATOR_BEAN_NAME);
}

再而我们看下其postProcessBeforeInitialization()方法

	@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
// 查找是否bean上对应的class上含有ConfigurationProperties注解
ConfigurationProperties annotation = getAnnotation(bean, beanName,
ConfigurationProperties.class);
if (annotation != null) {
// 开始绑定操作
bind(bean, beanName, annotation);
}
return bean;
}

Ok,我们继续往下追踪bind()方法

	private void bind(Object bean, String beanName, ConfigurationProperties annotation) {
ResolvableType type = getBeanType(bean, beanName);
// 查看类上是否含有@Validated注解
Validated validated = getAnnotation(bean, beanName, Validated.class);
Annotation[] annotations = (validated != null
? new Annotation[] { annotation, validated }
: new Annotation[] { annotation });
// class/实例/注解 三个绑定在一起供下述调用
Bindable<?> target = Bindable.of(type).withExistingValue(bean)
.withAnnotations(annotations);
try {
this.configurationPropertiesBinder.bind(target);
}
catch (Exception ex) {
throw new ConfigurationPropertiesBindException(beanName, bean, annotation,
ex);
}
}

最终的如何绑定本文就不讲解了,主要会读取Environment的属性然后进行对比绑定

涉及的代码很多,有兴趣的读者可自行分析。

所以基于上述的此加工类的作用,用户也可以直接通过@Bean注解创建Bean

// 直接返回实例,具体的内部属性的绑定则会由ConfigurationPropertiesBindingPostProcessor加工类完成
@Bean
public UserProperty userProperty(){
return new UserProperty();
}

小结

如果想在springboot中使用外部源的属性值有两个方法

  1. @PropertySource注解加载外部源,然后通过@Value注解来进行注入即可

  2. @ConfigurationProperties注解修饰javabean,其prefix属性是必须配置的,javabean的内部属性与配置文件指定的属性须一致,并且setter方法必须配置,这样可确保属性设置能成功

springboot情操陶冶-@ConfigurationProperties注解解析的更多相关文章

  1. springboot情操陶冶-@SpringBootApplication注解解析

    承接前文springboot情操陶冶-@Configuration注解解析,本文将在前文的基础上对@SpringBootApplication注解作下简单的分析 @SpringBootApplicat ...

  2. springboot情操陶冶-@Configuration注解解析

    承接前文springboot情操陶冶-SpringApplication(二),本文将在前文的基础上分析下@Configuration注解是如何一步一步被解析的 @Configuration 如果要了 ...

  3. SpringMVC源码情操陶冶-AnnotationDrivenBeanDefinitionParser注解解析器

    mvc:annotation-driven节点的解析器,是springmvc的核心解析器 官方注释 Open Declaration org.springframework.web.servlet.c ...

  4. springboot情操陶冶-@Conditional和@AutoConfigureAfter注解解析

    承接前文springboot情操陶冶-@Configuration注解解析,本文将在前文的基础上阐述@AutoConfigureAfter和@Conditional注解的作用与解析 1.@Condit ...

  5. springboot情操陶冶-jmx解析

    承接前文springboot情操陶冶-@Configuration注解解析,近期笔者接触的项目中有使用到了jmx的协议框架,遂在前文的基础上讲解下springboot中是如何整合jmx的 知识储备 J ...

  6. springboot情操陶冶-web配置(一)

    承接前文springboot情操陶冶-@SpringBootApplication注解解析,在前文讲解的基础上依次看下web方面的相关配置 环境包依赖 在pom.xml文件中引入web依赖,炒鸡简单, ...

  7. springboot情操陶冶-SpringApplication(二)

    承接前文springboot情操陶冶-SpringApplication(一),本文将对run()方法作下详细的解析 SpringApplication#run() main函数经常调用的run()方 ...

  8. springboot情操陶冶-web配置(七)

    参数校验通常是OpenApi必做的操作,其会对不合法的输入做统一的校验以防止恶意的请求.本文则对参数校验这方面作下简单的分析 spring.factories 读者应该对此文件加以深刻的印象,很多sp ...

  9. springboot情操陶冶-web配置(四)

    承接前文springboot情操陶冶-web配置(三),本文将在DispatcherServlet应用的基础上谈下websocket的使用 websocket websocket的简单了解可见维基百科 ...

随机推荐

  1. 微信跳转,wap网页跳转微信打开指定页面

    最近一朋友的客户有这么一需求,wap网页跳转微信打开指定页面,让用户去关注公众号.想这么干,通过网页跳转到微信并打开带有二维码的网页,用户长按识别二维码就可以关注了.想法很好,于是去搜索了“微信跳转, ...

  2. VB读写进程的内存

    在窗体部分简单测试了ReadProcessMemory和WriteProcessMemory对另一个程序进程的读写. 由于临时项目变动,又不需要了,所以直接封类,删工程.以下代码没有一个函数经过测试, ...

  3. margin相关属性值

    1.图片与文字对齐问题 图片与文字默认是居底对齐.一般img标签打头的小图标与文字对齐的话,通过 img{margin:0 3px -3px 0;} 这个的东西,能实现效果和兼容性俱佳的对齐效果: d ...

  4. 记录一次DataTable排序的问题

    //dt.DefaultView.Sort = "字段名 方式"; 最开始用的上面的没好用,改成底下转换了一下就好用了0.0 DataView dv = new DataView( ...

  5. vue 组件传值

    父组件传值给子组件 <list v-show="listLen" :listdata="list" :tipMsg="tipMsg" ...

  6. Fence Repair POJ - 3253 (贪心)

    Farmer John wants to repair a small length of the fence around the pasture. He measures the fence an ...

  7. 使用ANY和ALL条件

    在比较运算符中,可以出现ALL和ANY,表示“全部”和“任一”,但是ALL和ANY不能单独使用,需要配合单行比较操作符>.>=.<.<=一起使用.其中: > ANY : ...

  8. ucore代码分析

    lab2: 总共分为四个包一个文件,分别为: boot: 操作系统加载程序代码 kern: 操作系统内核代码 libs: 相关的库和数据结构 tools: 相关编译链接调试工具 Makefile: 构 ...

  9. java实现注册的短信验证码

    最近在做只能净化器的后台用户管理系统,需要使用手机号进行注册,找了许久才大致了解了手机验证码实现流程,今天在此和大家分享一下. 我们使用的是榛子云短信平台, 官网地址:http://smsow.zhe ...

  10. 与JMeter的第一次亲密接触

    postman和JMeters是外部接口测试的两个工具,通过界面化的方法,来实现操作http报文携带的请求字段.VK值.json.cookie.header值及文件.本篇主要介绍JMeter.   测 ...