spring cloud+spring boot 電子商務-spring boot 引用方式${}和@@用法與區別

gung123發表於2020-01-14

${}和@@都是springboot引用屬性變數的方式,具體區別與用法:


1、${}常用於pom.xml,和 src/main/resources/application.properties等預設配置檔案的屬性變數引用。


語法為:field_name=${field_value}


pom.xml示例:

<properties>

   <dubbo.version>2.7.0</dubbo.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.apache.dubbo</groupId>
         <artifactId>dubbo</artifactId>
         <version>${dubbo.version}</version>
    </dependency>
</dependencies>

application.properties示例:


#logback日誌配置

log.config.address=classpath:config/logback-spring.xml

logging.config=${log.config.address}

2、@@方式常用於引用springboot非預設配置檔案(即其他配置檔案)中的變數,是springboot為替代${}

屬性佔位符產生,原因是${}會被maven處理, 瞭解springcloud架構可以加求求:三五三六二四七二五九,所以引用非預設配置檔案時起不到引用變數的作用。


語法為:field_name=@field_value@


示例:在實際專案開發中,為了在不同環境進行測試,我們會在src/main/resources目錄下建立config資料夾,

並在config中建立多個properties檔案,例如:local.properties, development.properties, production.properties,

當我們在src/main/resources/application.properties檔案中引用src/main/resources/config/local.properties的屬性

變數時,就要使用@@方式

#埠配置

server.port=@server.port.web@

#logback日誌配置
logging.config=@logging.config@



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69952307/viewspace-2673257/,如需轉載,請註明出處,否則將追究法律責任。

相關文章