zoukankan      html  css  js  c++  java
  • spring学习笔记(一)@ConfigurationProperties注解

    结论: 这个注解主要是为了将配置文件中的属性映射到实体类上,并且支持嵌套映射。

    代码说明:

    @ConfigurationProperties(prefix = "person")
    @Data
    public class Person {
        String name;
        Integer age;
        //Dog dog;
    }
    @SpringBootApplication
    @EnableConfigurationProperties({Person.class})
    public class CustomApplication implements ApplicationRunner,ApplicationContextAware {
    
        ApplicationContext applicationContext;
    
        public static void main(String[] args) {
            SpringApplication.run(CustomApplication.class, args);
        }
        
        /*
        *这个地方也可以直接注入Person
        * @autowired
        * private Person person;
        *
        */
        @Override
        public void run(ApplicationArguments args) throws Exception {
            Person bean = applicationContext.getBean(Person.class);
            System.out.println(bean);
        }
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            this.applicationContext = applicationContext;
        }
    }
    # 这是我的application.yml文件
    person: 
      name: zhangsan
      age: 12

    启动chen程序后控制台打印:Person(name=zhangsan, age=12)

    证明:配置中的值被映射到了实体类中

    嵌套映射:

       新增实体类:

    @Data
    public class Dog {
        private String name;
    }

    同时将personle类中的注解打开,我这里就不贴重复的代码了,yml文件中新增如下

    person: 
      name: zhangsan
      age: 12
      dog: 
        name: wangwang

    运行程序结果如下:

    Person(name=zhangsan, age=12, dog=Dog(name=wangwang))

    证明嵌套映射没有问题。

    之前一直不明白为什么@ConfigurationProperties跟@EnableConfigurationProperties需要同时使用。跟踪@EnableConfigurationProperties源码发现如下代码:

    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    // 导入了EnableConfigurationPropertiesImportSelector,我们继续跟踪这个类
    @Import({EnableConfigurationPropertiesImportSelector.class})
    public @interface EnableConfigurationProperties {
        Class<?>[] value() default {};
    }
    public static class ConfigurationPropertiesBeanRegistrar implements ImportBeanDefinitionRegistrar {
            public ConfigurationPropertiesBeanRegistrar() {
            }
    
            public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
                this.getTypes(metadata).forEach((type) -> {
                    this.register(registry, (ConfigurableListableBeanFactory)registry, type);
                });
            }
            ........

    其实就是将我们在@EnableConfigurationProperties({Person.class})定义的class注册到spring容器中,对于我们的案例来说,就是将person类注册到容器中。为了验证我的想法,我将@EnableConfigurationProperties注解注释,并给person类型添加了@component注解

    代码如下:

    @ConfigurationProperties(prefix = "person")
    @Data
    @Component // 新增注解
    public class Person {
        String name;
        Integer age;
        Dog dog;
    }
    
    
    @SpringBootApplication
    //@EnableConfigurationProperties({Person.class}) 去除这个注解
    public class CustomApplication implements ApplicationRunner,ApplicationContextAware {
    
        ApplicationContext applicationContext;
    
        public static void main(String[] args) {
            SpringApplication.run(CustomApplication.class, args);
        }
    
        @Autowired
        Person person;
    
        @Override
        public void run(ApplicationArguments args) throws Exception {
            Person bean = applicationContext.getBean(Person.class);
            System.out.println(person);
        }
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            this.applicationContext = applicationContext;
        }
    }

    程序照样正常运行,验证正确~

  • 相关阅读:
    JSP自定义标签_用简单标签控制标签体执行10次
    JSP自定义标签_用简单标签实现控制标签体是否执行
    eclipse 使用lombok 精简java bean
    转 :关于springmvc使用拦截器
    转: spring静态注入
    spring 4.0+quartz2.2 实现持久化
    排除maven jar冲突 maven tomcat插件启动报错 filter转换异常
    转 Quartz将Job持久化所需表的说明
    转 maven jetty 插件
    ORA-14300: 分区关键字映射到超出允许的最大分区数的分区
  • 原文地址:https://www.cnblogs.com/daimzh/p/12854476.html
Copyright © 2011-2022 走看看