用springboot開發的過程當中,咱們會用到@ConfigurationProperties註解,主要是用來把properties或者yml配置文件轉化爲bean來使用的,而@EnableConfigurationProperties註解的做用是@ConfigurationProperties註解生效。
若是隻配置@ConfigurationProperties註解,在IOC容器中是獲取不到properties配置文件轉化的bean的,固然在@ConfigurationProperties加入註解的類上加@Component也可使交於springboot管理。java
第一步:建立一個類TestConfigurationPropertiesspring
@ConfigurationProperties(prefix = "properties") public class TestConfigurationProperties { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
注意:得加上set和get方法
第二步:建立TestAutoConfiguration類api
@Configuration @EnableConfigurationProperties(TestConfigurationProperties.class) public class TestAutoConfiguration { private TestConfigurationProperties testConfigurationProperties; public TestAutoConfiguration(TestConfigurationProperties testConfigurationProperties) { this.testConfigurationProperties = testConfigurationProperties; } @Bean public User user(){ User user = new User(); user.setName(testConfigurationProperties.getName()); return user; } }
注意:得建立一個有參構造方法
第三步:配置文件加入屬性springboot
properties.name=test
第四步:跑一下,打印出User這個類app
@RestController @RequestMapping("/api/test") @Slf4j public class TestController { @Autowired TestConfigurationProperties testConfigurationProperties; @Autowired User user; @RequestMapping(value = "/testConfigurationProperties") public String testConfigurationProperties() { log.info("test testConfigurationProperties.............{}", testConfigurationProperties.getName()); log.info("user:{}", user); return "SUCCESS"; } }
控制檯輸出:less
2019-04-21/16:11:36.638||||||||^_^|[http-nio-8088-exec-1] INFO com.stone.zplxjj.controller.TestController 37 - test testConfigurationProperties.............test 2019-04-21/16:11:36.639||||||||^_^|[http-nio-8088-exec-1] INFO com.stone.zplxjj.controller.TestController 38 - user:User(id=null, name=test)
經過查看@EnableConfigurationProperties的註解:ide
@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 {}; }
經過分析自動配置能夠知道,確定是這個類EnableConfigurationPropertiesImportSelector起的做用:ui
private static final String[] IMPORTS = { ConfigurationPropertiesBeanRegistrar.class.getName(), ConfigurationPropertiesBindingPostProcessorRegistrar.class.getName() }; @Override public String[] selectImports(AnnotationMetadata metadata) { return IMPORTS; }
selectImports方法返回了這兩個類:ConfigurationPropertiesBeanRegistrar和ConfigurationPropertiesBindingPostProcessorRegistrar,是什麼時候加載的,咱們只須要看這個類ConfigurationPropertiesBeanRegistrar便可:this
public static class ConfigurationPropertiesBeanRegistrar implements ImportBeanDefinitionRegistrar { @Override public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) { getTypes(metadata).forEach((type) -> register(registry, (ConfigurableListableBeanFactory) registry, type)); } //找到加入這個註解@EnableConfigurationProperties裏面的value值,其實就是類class private List<Class<?>> getTypes(AnnotationMetadata metadata) { MultiValueMap<String, Object> attributes = metadata .getAllAnnotationAttributes( EnableConfigurationProperties.class.getName(), false); return collectClasses((attributes != null) ? attributes.get("value") : Collections.emptyList()); } private List<Class<?>> collectClasses(List<?> values) { return values.stream().flatMap((value) -> Arrays.stream((Object[]) value)) .map((o) -> (Class<?>) o).filter((type) -> void.class != type) .collect(Collectors.toList()); } //註冊方法:根據找到的類名name和type,將加入註解@ConfigurationProperties的類加入spring容器裏面 private void register(BeanDefinitionRegistry registry, ConfigurableListableBeanFactory beanFactory, Class<?> type) { String name = getName(type); if (!containsBeanDefinition(beanFactory, name)) { registerBeanDefinition(registry, name, type); } } //找到加入註解@ConfigurationProperties的類的名稱,加入必定格式的拼接 private String getName(Class<?> type) { ConfigurationProperties annotation = AnnotationUtils.findAnnotation(type, ConfigurationProperties.class); String prefix = (annotation != null) ? annotation.prefix() : ""; return (StringUtils.hasText(prefix) ? prefix + "-" + type.getName() : type.getName()); } private boolean containsBeanDefinition( ConfigurableListableBeanFactory beanFactory, String name) { if (beanFactory.containsBeanDefinition(name)) { return true; } BeanFactory parent = beanFactory.getParentBeanFactory(); if (parent instanceof ConfigurableListableBeanFactory) { return containsBeanDefinition((ConfigurableListableBeanFactory) parent, name); } return false; } private void registerBeanDefinition(BeanDefinitionRegistry registry, String name, Class<?> type) { assertHasAnnotation(type); GenericBeanDefinition definition = new GenericBeanDefinition(); definition.setBeanClass(type); registry.registerBeanDefinition(name, definition); } private void assertHasAnnotation(Class<?> type) { Assert.notNull( AnnotationUtils.findAnnotation(type, ConfigurationProperties.class), () -> "No " + ConfigurationProperties.class.getSimpleName() + " annotation found on '" + type.getName() + "'."); } }
另外還有這個類:ConfigurationPropertiesBindingPostProcessorRegistrar,剛剛沒有分析,看了下源碼,其實他作的事情就是將配置文件當中的屬性值賦予到加了@ConfigurationProperties的註解的類的屬性上,具體就不分析了,有興趣本身能夠閱讀,入口知道了,就簡單了spa
更多文章能夠關注公衆號:stonezplxjj和我的博客:http://www.zplxjj.com