001/* 002 * Copyright 2012-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.context.properties; 018 019import org.springframework.beans.factory.config.BeanDefinition; 020import org.springframework.beans.factory.support.BeanDefinitionRegistry; 021import org.springframework.beans.factory.support.GenericBeanDefinition; 022import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; 023import org.springframework.core.type.AnnotationMetadata; 024 025/** 026 * {@link ImportBeanDefinitionRegistrar} for binding externalized application properties 027 * to {@link ConfigurationProperties} beans. 028 * 029 * @author Dave Syer 030 * @author Phillip Webb 031 */ 032public class ConfigurationPropertiesBindingPostProcessorRegistrar 033 implements ImportBeanDefinitionRegistrar { 034 035 @Override 036 public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, 037 BeanDefinitionRegistry registry) { 038 if (!registry.containsBeanDefinition( 039 ConfigurationPropertiesBindingPostProcessor.BEAN_NAME)) { 040 registerConfigurationPropertiesBindingPostProcessor(registry); 041 registerConfigurationBeanFactoryMetadata(registry); 042 } 043 } 044 045 private void registerConfigurationPropertiesBindingPostProcessor( 046 BeanDefinitionRegistry registry) { 047 GenericBeanDefinition definition = new GenericBeanDefinition(); 048 definition.setBeanClass(ConfigurationPropertiesBindingPostProcessor.class); 049 definition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); 050 registry.registerBeanDefinition( 051 ConfigurationPropertiesBindingPostProcessor.BEAN_NAME, definition); 052 053 } 054 055 private void registerConfigurationBeanFactoryMetadata( 056 BeanDefinitionRegistry registry) { 057 GenericBeanDefinition definition = new GenericBeanDefinition(); 058 definition.setBeanClass(ConfigurationBeanFactoryMetadata.class); 059 definition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); 060 registry.registerBeanDefinition(ConfigurationBeanFactoryMetadata.BEAN_NAME, 061 definition); 062 } 063 064}