zoukankan      html  css  js  c++  java
  • Spring注解@PropertySource加载配置文件和SpringBoot注解@Value、@ConfigurationProperties进行属性映射

    SpringBoot的配置文件

    位置:resources目录下

    配置文件的作用:

    (1)、SpringBoot是基于约定的,所以很多配置都有默认值,但如果想使用自己的配置替换默认配置的话,就可以使用application.properties或者application.ymlapplication.yaml)进行配置

    (2)、Springboot整合其他技术时需要配置一些信息。

    SpringBoot默认会从Resources目录下加载application.properties或application.yml(application.yaml)文件到项目中

    当应用比较大的时候,如果所有的内容都当在一个配置文件中,就会显得比较臃肿,同时也不太好理解和维护,此时可以将一个文件拆分为多个,使用 @PropertySource 注解加载指定的配置文件。

    @PropertySource 加载指定配置文件

    如果类路径下有包config/spring则写包,没包就写文件名。

    @PropertySource注解:用于指定properties文件的位置该注解@PropertySource的name和value属性,value属性用于指定文件的名称和文件的路径。关键字classpath表示类路径下

    注意:如果用@PropertySource注解加载yml文件,则需要添加PropertySourceFactory,否则启动会报错。

    import org.springframework.boot.env.YamlPropertySourceLoader;
    import org.springframework.core.env.PropertySource;
    import org.springframework.core.io.support.DefaultPropertySourceFactory;
    import org.springframework.core.io.support.EncodedResource;
    import java.io.IOException;
    import java.util.List;
    
    public class PropertySourceFactory extends DefaultPropertySourceFactory {
        @Override
        public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
            if (resource == null) {
                return super.createPropertySource(name, resource);
            }
            List<PropertySource<?>> sources = new YamlPropertySourceLoader().load(resource.getResource().getFilename(), resource.getResource());
            return sources.get(0);
        }
    }

    配置文件与配置类的属性映射方式:

    使用注解@Value映射

    前提条件:1、配置文件已经被加载;2、当前类反射创建的对象在spring容器中。

    server.port这样的数据是springboot内置好的,Springboot本身可以识别,像自己配置的一些数据如person.name,Springboot本身不识别。@Value是从spring容器中获取数据放到属性上,而不管是properties文件还是yml文件,都是把配置的数据放到spring容器中,

    我们可以通过@Value注解将配置文件中的值映射到一个Spring管理的Bean的字段

    @Component@Controller@Service@Repository:在类上面加注解,用于把当前类反射创建对象并把对象存入spring容器中。

     

    这种方式的缺点:如果person类有很多属性,需要写很多属性去接收,特别繁琐。

    通过注解@ConfigurationProperties(prefix="配置文件中的key的前缀")可以将配置文件中的配置自动与实体进行映射

    通过ConfigurationProperties自动从容器当中根据前缀找到你想要的数据,再根据字段进行字段映射。

    注意:使用@ConfigurationProperties方式可以进行配置文件与实体字段的自动映射,但需要字段必须提供set方法才可以,而且属性名与配置文件中的名称一致。而使用@Value注解修饰的字段不需要提供set方法

  • 相关阅读:
    Springboot 之 自定义配置文件及读取配置文件
    SQLSERVER系统视图 sql server系统表详细说明
    MySQL Workbench建表时 PK NN UQ BIN UN ZF AI 的含义
    使用Ecplise git commit时出现"There are no stages files"
    maven添加sqlserver的jdbc驱动包
    java将XML文档转换成json格式数据
    java将XML文档转换成json格式数据
    cannot be resolved. It is indirectly referenced from required .class files
    org.codehaus.jackson.map.JsonMappingException: Can not construct instance of java.util.Date from String value '2012-12-12 12:01:01': not a valid representation (error: Can not parse date "2012-12-
    @Autowired注解和静态方法 NoClassDefFoundError could not initialize class 静态类
  • 原文地址:https://www.cnblogs.com/zwh0910/p/14308158.html
Copyright © 2011-2022 走看看