第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

(七)SpringBoot2.0基礎(chǔ)篇- application.properties屬性文件的解析及獲取

標(biāo)簽:
Java SpringBoot

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

  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.*;
    
    @Componentpublic class MyBean {
    
        @Value("${name}")    private String name;    // ...}

    复制代码

    参考官方文档:

    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

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

  5. 使用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

原文出处

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消