http://www.cnblogs.com/dragonfei/archive/2016/10/09/5906474.html

********************************************

1.简单示例:

SpringBoot中的的配置简单属性类支持ConfigurationProperties方式,看一个简单的示例。

 1 @ConfigurationProperties(prefix = "org.dragonfei.demo")
2 public class DemoProperties {
3 private String name;
4 private String password;
5 private String test;
6
7 public String getName() {
8 return name;
9 }
10
11 public void setName(String name) {
12 this.name = name;
13 }
14
15 public String getPassword() {
16 return password;
17 }
18
19 public void setPassword(String password) {
20 this.password = password;
21 }
22
23 public String getTest() {
24 return test;
25 }
26
27 public void setTest(String test) {
28 this.test = test;
29 }
30
31 @Override
32 public String toString() {
33 return "DemoProperties{" +
34 "name='" + name + '\'' +
35 ", password='" + password + '\'' +
36 ", test='" + test + '\'' +
37 '}';
38 }
39 }
1 org.dragonfei.demo.name=dragonfei
2 org.dragonfei.demo.password=password
3 org.dragonfei.demo.test=test
1 @Configuration
2 @EnableConfigurationProperties({DemoProperties.class})
3 public class DemoConfiguration {
4 }
 1 @RunWith(SpringJUnit4ClassRunner.class)
2 @SpringApplicationConfiguration(classes = DemoConfiguration.class)
3 @EnableAutoConfiguration
4 public class DemoPropertiesTest {
5
6 @Autowired
7 private DemoProperties properties;
8 @Test
9 public void testProperties(){
10 System.out.println(properties.toString());
11 }
12 }
1 DemoProperties{name='dragonfei', password='password', test='test'}

DemoProperties神奇的注入到Spring容器中了。有没有跟我一样很兴奋,这样的 一大好处,将配置文件的属性以类的形式展现,在需要使用的时候只需要,autowire需要的类就可以了,避免大片重复的的${a.b.c}

 2.Properties属性自动装配实现

DemoProperties这么神奇注入到容器中,天下没有什么是莫名奇妙的,引出了两个关键问题:

  • DemoProperties是怎样注入到容器中?
  • DemoProperties中的各个属性是怎么被赋值的呢?

  要回答上面的问题,必须对@Configuration如何注入bean做一个简单的回顾:

  • 在解析@Congiguraion的时候,会调用@Import中引入的类
  • 如果@Import中是ImportBeanDefinitionegistar的子类,会直接调用registerBeanDefinitions
  • 如果@Import中是ImportSelector类型,会调用selectImports()返回的bean的registerBeanDefinitions方法。
  • registerBeanDefinitions方法会向BeanFactory中添加新的bean。

回到正题。打开EnableConfigurationProperties

 1 @Target(ElementType.TYPE)
2 @Retention(RetentionPolicy.RUNTIME)
3 @Documented
4 @Import(EnableConfigurationPropertiesImportSelector.class)
5 public @interface EnableConfigurationProperties {
6
7 /**
8 * Convenient way to quickly register {@link ConfigurationProperties} annotated beans
9 * with Spring. Standard Spring Beans will also be scanned regardless of this value.
10 * @return {@link ConfigurationProperties} annotated beans to register
11 */
12 Class<?>[] value() default {};
13
14 }

注意@Imoport里面的类

 1     public String[] selectImports(AnnotationMetadata metadata) {
2 MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(
3 EnableConfigurationProperties.class.getName(), false);
4 Object[] type = attributes == null ? null
5 : (Object[]) attributes.getFirst("value");
6 if (type == null || type.length == 0) {
7 return new String[] {
8 ConfigurationPropertiesBindingPostProcessorRegistrar.class
9 .getName() };
10 }
11 return new String[] { ConfigurationPropertiesBeanRegistrar.class.getName(),
12 ConfigurationPropertiesBindingPostProcessorRegistrar.class.getName() };
13 }

然后,会调用ConfigurationPropertiesBeanRegistar和ConfigurationPropertiesBindingPostProcessorRegistar的registerBeanDefinitions方法,前者是为了注入配置properties类,后者为属性绑定值

 1     @Override
2 public void registerBeanDefinitions(AnnotationMetadata metadata,
3 BeanDefinitionRegistry registry) {
4 MultiValueMap<String, Object> attributes = metadata
5 .getAllAnnotationAttributes(
6 EnableConfigurationProperties.class.getName(), false);
7 List<Class<?>> types = collectClasses(attributes.get("value"));
8 for (Class<?> type : types) {
9 String prefix = extractPrefix(type);
10 String name = (StringUtils.hasText(prefix) ? prefix + "-" + type.getName()
11 : type.getName());
12 if (!registry.containsBeanDefinition(name)) {
13 registerBeanDefinition(registry, type, name);
14 }
15 }
16 }
 1     public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
2 BeanDefinitionRegistry registry) {
3 if (!registry.containsBeanDefinition(BINDER_BEAN_NAME)) {
4 BeanDefinitionBuilder meta = BeanDefinitionBuilder
5 .genericBeanDefinition(ConfigurationBeanFactoryMetaData.class);
6 BeanDefinitionBuilder bean = BeanDefinitionBuilder.genericBeanDefinition(
7 ConfigurationPropertiesBindingPostProcessor.class);
8 bean.addPropertyReference("beanMetaDataStore", METADATA_BEAN_NAME);
9 registry.registerBeanDefinition(BINDER_BEAN_NAME, bean.getBeanDefinition());
10 registry.registerBeanDefinition(METADATA_BEAN_NAME, meta.getBeanDefinition());
11 }
12 }

注意这里注入了ConfigurationPropertiesBindingPostProcessor,这才是属性赋值的关键。查看类图

注意到ConfigurationPropertiesBindingPostProcessor继承自BeanPostProcessor,他会在bean初始化前后调用before和after后置处理,这里,在Properties属性初始化完成后,会对绑定属性,

 1 private void postProcessBeforeInitialization(Object bean, String beanName,
2 ConfigurationProperties annotation) {
3 Object target = bean;
4 PropertiesConfigurationFactory<Object> factory = new PropertiesConfigurationFactory<Object>(
5 target);
6 if (annotation != null && annotation.locations().length != 0) {
7 factory.setPropertySources(
8 loadPropertySources(annotation.locations(), annotation.merge()));
9 }
10 else {
11 factory.setPropertySources(this.propertySources);
12 }
13 factory.setValidator(determineValidator(bean));
14 // If no explicit conversion service is provided we add one so that (at least)
15 // comma-separated arrays of convertibles can be bound automatically
16 factory.setConversionService(this.conversionService == null
17 ? getDefaultConversionService() : this.conversionService);
18 if (annotation != null) {
19 factory.setIgnoreInvalidFields(annotation.ignoreInvalidFields());
20 factory.setIgnoreUnknownFields(annotation.ignoreUnknownFields());
21 factory.setExceptionIfInvalid(annotation.exceptionIfInvalid());
22 factory.setIgnoreNestedProperties(annotation.ignoreNestedProperties());
23 if (StringUtils.hasLength(annotation.prefix())) {
24 factory.setTargetName(annotation.prefix());
25 }
26 }
27 try {
28 factory.bindPropertiesToTarget();
29 }
30 catch (Exception ex) {
31 String targetClass = ClassUtils.getShortName(target.getClass());
32 throw new BeanCreationException(beanName, "Could not bind properties to "
33 + targetClass + " (" + getAnnotationDetails(annotation) + ")", ex);
34 }
35 }

至于真实的数据绑定,会从propertySources中获取,敬请期待....Spring 的数据绑定,这简单提一下关键的地方:

1 Set<String> names = getNames(relaxedTargetNames);
2 PropertyValues propertyValues = getPropertyValues(names, relaxedTargetNames);

请注意getNames,是获取prefix+属性构成的key值,prefix_property和prefix.property都会获取到

 1     private Set<String> getNames(Iterable<String> prefixes) {
2 Set<String> names = new LinkedHashSet<String>();
3 if (this.target != null) {
4 PropertyDescriptor[] descriptors = BeanUtils
5 .getPropertyDescriptors(this.target.getClass());
6 for (PropertyDescriptor descriptor : descriptors) {
7 String name = descriptor.getName();
8 if (!name.equals("class")) {
9 RelaxedNames relaxedNames = RelaxedNames.forCamelCase(name);
10 if (prefixes == null) {
11 for (String relaxedName : relaxedNames) {
12 names.add(relaxedName);
13 }
14 }
15 else {
16 for (String prefix : prefixes) {
17 for (String relaxedName : relaxedNames) {
18 names.add(prefix + "." + relaxedName);
19 names.add(prefix + "_" + relaxedName);
20 }
21 }
22 }
23 }
24 }
25 }
26 return names;
27 }

getPropertyValues会获取到满足上述条件的propertyValues,最后调用spring框架提供数据绑定策略进行数据绑定。

没有智能的代码,源码面前了无秘密
 

Spring 配置解析之Properties的更多相关文章

  1. spring配置中,properties文件以及xml文件配置问题

    spring方便我们的项目快速搭建,功能强大,自然也会是体系复杂! 这里说下配置文件properties管理的问题. 一些不涉及到代码逻辑,仅仅只是配置数据,可以放在xxxx.properties文件 ...

  2. spring配置中引入properties

    <context:property-placeholder location="classpath*:db.properties" />

  3. spring 配置多个properties

    复制多份,保证有效的配置文件,属性时true就行 <bean class="org.springframework.beans.factory.config.PropertyPlace ...

  4. Spring源码情操陶冶-PropertyPlaceholderBeanDefinitionParser注解配置解析器

    本文针对spring配置的context:property-placeholder作下简单的分析,承接前文Spring源码情操陶冶-自定义节点的解析 spring配置文件应用 <context: ...

  5. JdbcTemplae使用入门&&Spring三种连接池配置&&Spring配置文件引用外部properties文件

    JdbcTemplate的使用 Spring为了各种支持的持久化技术,都提供了简单操作的模版和回调. JdbcTemplate 简化 JDBC 操作HibernateTemplate 简化 Hiber ...

  6. Spring多资源文件properties的配置

    Spring简化了加载资源文件的配置,可以通过<context:property-placeholder去加载,这个元素的写法如下: <context:property-placehold ...

  7. spring+mybaits xml配置解析----转

    一.项目中spring+mybaits xml配置解析 一般我们会在datasource.xml中进行如下配置,但是其中每个配置项原理和用途是什么,并不是那么清楚,如果不清楚的话,在使用时候就很有可能 ...

  8. 使用import简化spring的配置 spring import 标签的解析 使用import或加载spring配置时,报错误There is no ID/IDREF 多个Spring配置文件import resource路径配置

    spring-import 标签的解析.使用案例: 对于spring配置文件的编写,我想,对于经历过庞大项目的人,都有那种恐惧的心理,太多的配置文件.不过,分模块都是大多数人能想到的方法,但是,怎么分 ...

  9. spring 配置文件中使用properties文件 配置

    配置Bean载入properties文件: <bean id="propertyPlaceholderConfigurer" class="org.springfr ...

随机推荐

  1. python for MSSQLserver

    # -*- coding: utf-8 -*- '''python coded by written in 2016/8/31 Used for get win os log for each win ...

  2. JavaScript语言精粹读书笔记 - JavaScript函数

    JavaScript是披着C族语言外衣的LISP,除了词法上与C族语言相似以外,其他几乎没有相似之处. JavaScript 函数: 函数包含一组语句,他们是JavaScript的基础模块单元,用于代 ...

  3. 读书笔记 --TCP :传输控制协议(一)

    TCP提供一种面向连接的,可靠的字节流服务. TCP 通过如下方式来提供可靠性: 应用数据被分割成TCP认为最适合发送的数据块. 超时重传机制.TCP发出一个段后,启动一个定时器,等待目的端确认收到这 ...

  4. 关于onethink的迁移站点产生数据库错误

      为了支持国产,本人使用了onethink建立了一个自己的站点( 模板世界:www.templatesy.com ),使用至今,虽然碰到了重重困难,还有很多bug,但总算也勉强建了起来. 在近期的一 ...

  5. CountDownLatch和CyclicBarrier 举例详解

    有时候会有这样的需求,多个线程同时工作,然后其中几个可以随意并发执行,但有一个线程需要等其他线程工作结束后,才能开始.举个例子,开启多个线程分块下载一个大文件,每个线程只下载固定的一截,最后由另外一个 ...

  6. python字符串方法的简单使用

    学习python字符串方法的使用,对书中列举的每种方法都做一个试用,将结果记录,方便以后查询. (1) s.capitalize() ;功能:返回字符串的的副本,并将首字母大写.使用如下: >& ...

  7. LabVIEW类方法浏览器-Class Method Browser

    随着LabVIEW的类编程应用增多,当打开较多的VI进行编辑时候,添加该类对应的VI方法到程序后背板上操作显得繁琐(需要在Project浏览器或类浏览器或库浏览器中找到该类的方法VI,然后再拖到程序背 ...

  8. 基于webrtc的资源释放问题(二)

    基于webrtc的资源释放问题(二) ——建立连接的过程中意外中断 应用背景: 我们在打电话的时候会不会遇到这种情况?打电话的时候未接通之前挂掉了电话,或者在接通之后建立的连接的过程中挂掉电话? 特别 ...

  9. mysql优化一 之 优化内容概述及开启慢查日志的相关配置

    1-1数据库优化的目的 首先是为了避免出现页面访问错误(基本有三种) (1)由于数据库连接timeout产生的页面5XX错误 (2)由于慢查询造成页面无法加载 (3)由于阻塞造成的数据无法提交 其次: ...

  10. centos搭建属于自己wordpress网站

    1.在centos7中安装好mysql5.7.16.httpd.php.php-mysql工具 这里的mysql可以用yum一键安装,他可以自己解决依赖问题 [root@localhost ~]# y ...