国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Spring @Value 屬性注入使用總結(jié)一

@Value注入

不通過配置文件的注入屬性的情況

通過@Value將外部的值動(dòng)態(tài)注入到Bean中,使用的情況有:

  • 注入普通字符串
  • 注入操作系統(tǒng)屬性
  • 注入表達(dá)式結(jié)果
  • 注入其他Bean屬性:注入beanInject對象的屬性another
  • 注入文件資源
  • 注入U(xiǎn)RL資源

    詳細(xì)代碼見:

    @Value("normal")    private String normal; // 注入普通字符串    @Value("#{systemProperties['os.name']}")    private String systemPropertiesName; // 注入操作系統(tǒng)屬性    @Value("#{ T(java.lang.Math).random() * 100.0 }")    private double randomNumber; //注入表達(dá)式結(jié)果    @Value("#{beanInject.another}")    private String fromAnotherBean; // 注入其他Bean屬性:注入beanInject對象的屬性another,類具體定義見下面    @Value("classpath:com/hry/spring/configinject/config.txt")    private Resource resourceFile; // 注入文件資源    @Value("http://www.baidu.com")    private Resource testUrl; // 注入U(xiǎn)RL資源
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

注入其他Bean屬性:注入beanInject對象的屬性another

@Componentpublic class BeanInject {    @Value("其他Bean的屬性")    private String another;    public String getAnother() {        return another;    }    public void setAnother(String another) {        this.another = another;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

注入文件資源:com/hry/spring/configinject/config.txt

test configuration file
  • 1
  • 1

測試類:

@SpringBootApplicationpublic class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}@RunWith(SpringRunner.class)@SpringBootTest(classes=Application.class)public class ConfiginjectApplicationTest {    @Autowired    private BaseValueInject baseValueInject;    @Test    public void baseValueInject(){        System.out.println(baseValueInject.toString());    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

運(yùn)行測試類

normal=normalsystemPropertiesName=Windows 10randomNumber=35.10603794922444fromAnotherBean=其他Bean的屬性resourceFile=test configuration filetestUrl=<html>...<title>百度一下,你就知道</title>...略</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

通過配置文件的注入屬性的情況

通過@Value將外部配置文件的值動(dòng)態(tài)注入到Bean中。配置文件主要有兩類:

  • application.properties。application.properties在spring boot啟動(dòng)時(shí)默認(rèn)加載此文件
  • 自定義屬性文件。自定義屬性文件通過@PropertySource加載。@PropertySource可以同時(shí)加載多個(gè)文件,也可以加載單個(gè)文件。如果相同第一個(gè)屬性文件和第二屬性文件存在相同key,則最后一個(gè)屬性文件里的key啟作用。加載文件的路徑也可以配置變量,如下文的${anotherfile.configinject},此值定義在第一個(gè)屬性文件config.properties

第一個(gè)屬性文件config.properties內(nèi)容如下:
${anotherfile.configinject}作為第二個(gè)屬性文件加載路徑的變量值

book.name=bookNameanotherfile.configinject=placeholder
  • 1
  • 2
  • 1
  • 2

第二個(gè)屬性文件config_placeholder.properties內(nèi)容如下:

book.name.placeholder=bookNamePlaceholder
  • 1
  • 1

下面通過@Value(“${app.name}”)語法將屬性文件的值注入bean屬性值,詳細(xì)代碼見:

@Component// 引入外部配置文件組:${app.configinject}的值來自config.properties。// 如果相同@PropertySource({"classpath:com/hry/spring/configinject/config.properties",    "classpath:com/hry/spring/configinject/config_${anotherfile.configinject}.properties"})public class ConfigurationFileInject{    @Value("${app.name}")    private String appName; // 這里的值來自application.properties,spring boot啟動(dòng)時(shí)默認(rèn)加載此文件    @Value("${book.name}")    private String bookName; // 注入第一個(gè)配置外部文件屬性    @Value("${book.name.placeholder}")    private String bookNamePlaceholder; // 注入第二個(gè)配置外部文件屬性    @Autowired    private Environment env;  // 注入環(huán)境變量對象,存儲(chǔ)注入的屬性值    public String toString(){        StringBuilder sb = new StringBuilder();        sb.append("bookName=").append(bookName).append("\r\n")        .append("bookNamePlaceholder=").append(bookNamePlaceholder).append("\r\n")        .append("appName=").append(appName).append("\r\n")        .append("env=").append(env).append("\r\n")        // 從eniroment中獲取屬性值        .append("env=").append(env.getProperty("book.name.placeholder")).append("\r\n");        return sb.toString();    }   }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

測試代碼:
Application.Java同上文

@RunWith(SpringRunner.class)@SpringBootTest(classes=Application.class)public class ConfiginjectApplicationTest {    @Autowired    private ConfigurationFileInject configurationFileInject;    @Test    public void configurationFileInject(){        System.out.println(configurationFileInject.toString());    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

測試運(yùn)行結(jié)果:

bookName=bookNamebookNamePlaceholder=bookNamePlaceholderappName=appNameenv=StandardEnvironment {activeProfiles=[], defaultProfiles=[default], propertySources=[Inlined Test Properties,systemProperties,systemEnvironment,random,applicationConfig: [classpath:/application.properties],class path resource [com/hry/spring/configinject/config_placeholder.properties],class path resource [com/hry/spring/configinject/config.properties]]}env=bookNamePlaceholder
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

代碼

Github代碼

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
JSP中的九大內(nèi)置對象
Spring Boot 配置優(yōu)先級順序
Apache Commons Configuration 包使用
Springboot 中配置文件的優(yōu)先級和加載順序
產(chǎn)品設(shè)計(jì):Android應(yīng)用
應(yīng)用模式
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服