?>?新闻?>?国内新闻 > 正文

晋江牟蜒传媒_(七)SpringBoot2.0基础篇- application.properties属性文件的解析及获取

注:由于测试代码较多,影响查看效果,所以只放了核心代码,如需查看,请点示例代码

  1. 默认访问的属性文件为application.properties文件,可在启动项目参数中指定spring.config.location的参数:

    java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

    参考官方文档:https://docs.spring.io/spring-boot/docs/2.0.2.RELEASE/reference/htmlsingle/#boot-features-external-config-application-property-files

  2. 使用@PropertySource来获取配置文件的中属性值(注意:在使用该注解时,属性文件必须为properties文件,yaml文件不可用):

    @Configuration
    @PropertySource("classpath:/app.properties")
    public class AppConfig {
    
        @Autowired
        Environment env;
    
        @Bean
        public TestBean testBean() {
            TestBean testBean = new TestBean();
            testBean.setName(env.getProperty("testbean.name"));
            return testBean;
        }   
    }  

    参考官方文档:https://docs.spring.io/spring/docs/5.0.6.RELEASE/javadoc-api/org/springframework/context/annotation/PropertySource.html

  3. 使用@Value注解直接将属性值注入进修饰对对象中:

    import org.springframework.stereotype.*;
    import org.springframework.beans.factory.annotation.*;
    
    @Component
    public class MyBean {
    
        @Value("${name}")
        private String name;
    
        // ...
    
    }

    参考官方文档:https://docs.spring.io/spring-boot/docs/2.0.2.RELEASE/reference/htmlsingle/#boot-features-external-config

  4. 使用@ConfigurationProperties(prefix="my")将属性值注入进对象,可以注入对象的属性key的对象,也可以注入进List或Set中,但是属性的书写需要有规范:

    my.servers0=dev.example.com
    my.servers1=another.example.com

    使用方式

    @ConfigurationProperties(prefix="my")
    public class Config {
        //set,list不需要Setter方法
        private List servers = new ArrayList();
    
        public List getServers() {
            return this.servers;
        }
    }

    参考官方文档:https://docs.spring.io/spring-boot/docs/2.0.2.RELEASE/reference/htmlsingle/#boot-features-external-config-loading-yaml

    ConfigurationProperties prefix" my" public class Config set, list bu xu yao Setter fang fa private List lt String gt servers new ArrayList lt String gt public List lt String gt getServers return this. servers can kao guan fang wen dang: https: docs. spring. io springboot docs 2. 0. 2. RELEASE reference htmlsingle bootfeaturesexternalconfigloadingyaml

  5. 可以使用yaml文件格式来替换properties,属性获取方式不变(注:yaml文件后缀名为.yml)

  6. 使用POJO方式直接将属性注入进实体对象中:

    application.yml

    acme:
        remote-address: 192.168.1.1
        security:
            username: admin
            password: admincss
            roles:
                - USER
                - ADMIN

    AcmeProperties.java

    package com.example;
    
    import java.net.InetAddress;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    @ConfigurationProperties("acme")
    public class AcmeProperties {
    
        private InetAddress remoteAddress;
    
        private final Security security = new Security();
    
        public InetAddress getRemoteAddress() { ... }
    
        public void setRemoteAddress(InetAddress remoteAddress) { ... }
    
        public Security getSecurity() { ... }
    
        public static class Security {
    
            private String username;
    
            private String password;
    
            private List roles = new ArrayList<>(Collections.singleton("USER"));
    
            public String getUsername() { ... }
    
            public void setUsername(String username) { ... }
    
            public String getPassword() { ... }
    
            public void setPassword(String password) { ... }
    
            public List getRoles() { ... }
    
            public void setRoles(List roles) { ... }
    
        }
    }

?

参考文档:https://docs.spring.io/spring-boot/docs/2.0.2.RELEASE/reference/htmlsingle/#boot-features-external-config-typesafe-configuration-properties

代码示例:https://gitee.com/lfalex/spring-boot-example/tree/dev/spring-boot-properties

当前文章:http://www.kmb-web.com/f474v4riy/123675-1313670-90252.html

发布时间:03:10:00

香港曾道人433433??848484开奖结果今晚??正版心水特马??1861图库??天下彩手机报码??ymz03开奖现场??八马论坛48878??158gpcom香港正版挂牌??正版白小姐铁算盘??www.332555.com??