@ConfigurationProperties

功能

  将属性文件与一个Java类绑定,属性文件中的变量与Java类中的成员变量一一对应,无需完全一致。

  如需将 @ConfigurationProperties 注解的目标类添加到Spring IOC容器中,可以

  • 使用 @Component 注解目标类,这样项目启动时会自动将该类添加到容器中。
  • 使用 @EnableConfigurationProperties 间接的将 @ConfigurationProperties 注解的目标类添加到容器中。讲的详细点就是,使用 @ConfigurationProperties 注解 类A,使用 @EnableConfigurationProperties(value =  类A.class) 注解类B,那么容器在加载类B的时候,会先加载类A到容器中,实现了间接的加载。

源码

package org.springframework.boot.context.properties;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.core.annotation.AliasFor;

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ConfigurationProperties {

    /**
     * 属性文件中变量的前缀
     * 如果变量的全名是server.port,那么value==server。
     */
    @AliasFor("prefix")
    String value() default "";

    /**
     * 属性文件中变量的前缀
     * 如果变量的全名是server.port,那么prefix==server。
     */
    @AliasFor("value")
    String prefix() default "";

    /**
     * 是否忽略类型不匹配的错误
     */
    boolean ignoreInvalidFields() default false;

    /**
     * 是否忽略属性变量对应的字段不存在的错误
     */
    boolean ignoreUnknownFields() default true;

}

猜你喜欢

转载自www.cnblogs.com/517cn/p/10946519.html