(七)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<String> servers = new ArrayList<String>();
    
        public List<String> getServers() {
            return this.servers;
        }
    }

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

  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<String> roles = new ArrayList<>(Collections.singleton("USER"));
    
            public String getUsername() { ... }
    
            public void setUsername(String username) { ... }
    
            public String getPassword() { ... }
    
            public void setPassword(String password) { ... }
    
            public List<String> getRoles() { ... }
    
            public void setRoles(List<String> 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM