承接前文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. 如何使用yql实现跨域访问

    应用场景 调用百度的某个API, 例如:https://openapi.baidu.com/api 返回结果是:{"id":123,"name":"t ...

  2. 匿名函数 javascript

    匿名函数: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  3. java拦截处理System.exit(0)

    在使用TestNG做单元测试时,需要测试的代码中出现System.exit(0),导致单元测试还未结束程序就停止了.解决方法如下: public class TestMain { public sta ...

  4. python多线程和多进程使用

    # 多线程 from concurrent.futures import ThreadPoolExecutor # 多进程 from concurrent.futures import Process ...

  5. Recycle移动端界面设计成果图

    经过功能分析,我最终设计出来了该App界面图: (1)主页面图 (2)消息界面图 (3)我的界面图 (4)垃圾页面图 由于时间原因,此次设计仅为初稿.以后会继续抽出时间,与团队成员一起完善该项目App ...

  6. 二、油泼面(Oil spill noodle)

    油泼面 油泼面是陕西传统的特色面食之一,起源于周代,并以咸阳油泼面最为著名,有鲜香味.酸辣味.香辣味. 油泼面是一种很普通的面食制作方法,将手工制作的面条在开水中煮熟后捞在碗里,将葱花碎.花椒粉.盐等 ...

  7. easyui-combotree选中指定的值

    选中根节点: //station_id为combotree控件id var station = $('#station_id').combotree('tree').tree('getRoots'); ...

  8. 格式化数据保留两位小数,输入格式为 :xxx,xx,,,,x,,(x为浮点数)

    /** * 格式化字符串 */ static String dataFormat(String data){ String formatedData = ""; // 浮点数正则表 ...

  9. Anveshak: Placing Edge Servers In The Wild

    Anveshak:在野外放置边缘服务器 本文为SIGCOMM 2018 Workshop (Mobile Edge Communications, MECOMM)论文. 笔者翻译了该论文.由于时间仓促 ...

  10. C++之几个最常

    1.同类对象间的数据共享——静态成员 静态数据成员声明静态数据成员要采用关键字static:类静态数据成员的定义和初始化定义:static 数据类型 成员名:初始化:数据类型 类名::静态数据成员名= ...