亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

聊聊@value注解和@ConfigurationProperties注解的使用

 更新時(shí)間:2021年09月16日 14:34:21   作者:qq_29914229  
這篇文章主要介紹了@value注解和@ConfigurationProperties注解的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

@value注解和@ConfigurationProperties注解

@value讀取默認(rèn)配置

yml文件內(nèi)容如下(裝了STS插件以后即可直接使用,改后綴就行了)

user:
  username: xiongda
  sex: man
  age: 20
school:
  name: xtu
  location: hunan

備注:一定要注意之間要留空格,發(fā)現(xiàn)顏色變綠色了才是正確的格式,這個(gè)坑我剛踩

package com.example.demo.service.impl; 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; 
import com.example.demo.service.ReadConfigService; 
@Service
public class ReadConfigServiceImpl implements ReadConfigService {    
    @Value(value="${user.username}")
    private String username;
    
    @Value(value="${user.sex}")
    private String sex;
    
    @Value(value="${user.age}")
    private String age;
    
    @Value(value="${school.name}")
    private String name;
    
    @Value(value="${school.location}")
    private String location;
    
    @Override
    public String getUserMessage() {
        return "user ="+username+" sex ="+sex+" age="+age;
    }
 
    @Override
    public String getAuthorMessage() {
        return "schoolname="+name+"location="+location;
    }    
}

@ConfigurationProperties讀取默認(rèn)配置

package com.example.demo.config; 
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; 
@Component
@ConfigurationProperties(prefix="user")
public class HelloConfig {
    private String username;    
    private String sex;    
    private String age; 
    public String getUsername() {
        return username;
    }
 
    public void setUsername(String username) {
        this.username = username;
    }
 
    public String getSex() {
        return sex;
    }
 
    public void setSex(String sex) {
        this.sex = sex;
    }
 
    public String getAge() {
        return age;
    }
 
    public void setAge(String age) {
        this.age = age;
    }    
}

調(diào)用的controller層

package com.example.demo.web; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; 
import com.example.demo.config.HelloConfig;
import com.example.demo.service.ReadConfigService;
 
@RestController
@RequestMapping("hello")
public class HelloController {
    
    @Autowired
    ReadConfigService readConfigService;
    
    @Autowired
    HelloConfig helloConfig;
    
    @RequestMapping("/user")
    public String say() {
       return "username "+helloConfig.getUsername()+" sex "+helloConfig.getSex()+" age "+helloConfig.getAge();
    }
    
    @RequestMapping("/school")
    public String school() {
       return readConfigService.getAuthorMessage();
    }
}

@ConfigurationProperties和@Value使用上的一點(diǎn)區(qū)別

@ConfigurationProperties和@Value的一個(gè)共同點(diǎn)就是從配置文件中讀取配置項(xiàng)。

發(fā)現(xiàn)有一點(diǎn)區(qū)別,我項(xiàng)目配置中并沒(méi)有配置hello.msg ,當(dāng)使用第一段代碼時(shí),啟動(dòng)后讀取到msg為null,而第二段代碼則會(huì)拋出異常。

第二段代碼有個(gè)好處,就是防止我們配置項(xiàng)遺漏,當(dāng)遺漏時(shí),啟動(dòng)程序肯定出錯(cuò),這樣避免了一些因?yàn)檫z漏配置項(xiàng)導(dǎo)致的BUG.

第一段代碼

import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("hello")
public class HelloProperties {
    private String msg;
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
}

第二段代碼

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Hello2Properties {
    @Value("${hello.msg}")
    private String msg;
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • java使用elasticsearch分組進(jìn)行聚合查詢過(guò)程解析

    java使用elasticsearch分組進(jìn)行聚合查詢過(guò)程解析

    這篇文章主要介紹了java使用elasticsearch分組進(jìn)行聚合查詢過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • Java性能調(diào)優(yōu)及排查方式

    Java性能調(diào)優(yōu)及排查方式

    這篇文章主要介紹了Java性能調(diào)優(yōu)及排查方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • 解決MyEclipse下啟動(dòng)項(xiàng)目時(shí)JBoss內(nèi)存溢出的問(wèn)題

    解決MyEclipse下啟動(dòng)項(xiàng)目時(shí)JBoss內(nèi)存溢出的問(wèn)題

    下面小編就為大家?guī)?lái)一篇解決MyEclipse下啟動(dòng)項(xiàng)目時(shí)JBoss內(nèi)存溢出的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-07-07
  • 一名Java高級(jí)工程師需要學(xué)什么?

    一名Java高級(jí)工程師需要學(xué)什么?

    作為一名Java高級(jí)工程師需要學(xué)什么?如何成為一名合格的工程師,這篇文章給了你較為詳細(xì)的答案,需要的朋友可以參考下
    2017-08-08
  • 解決SpringBoot application.yaml文件配置schema 無(wú)法執(zhí)行sql問(wèn)題

    解決SpringBoot application.yaml文件配置schema 無(wú)法執(zhí)行sql問(wèn)題

    這篇文章主要介紹了解決SpringBoot application.yaml文件配置schema 無(wú)法執(zhí)行sql問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • java?web實(shí)現(xiàn)簡(jiǎn)單登錄注冊(cè)功能全過(guò)程(eclipse,mysql)

    java?web實(shí)現(xiàn)簡(jiǎn)單登錄注冊(cè)功能全過(guò)程(eclipse,mysql)

    前期我們學(xué)習(xí)了javaweb項(xiàng)目用JDBC連接數(shù)據(jù)庫(kù),還有數(shù)據(jù)庫(kù)的建表功能,下面這篇文章主要給大家介紹了關(guān)于java?web實(shí)現(xiàn)簡(jiǎn)單登錄注冊(cè)功能的相關(guān)資料,需要的朋友可以參考下
    2022-07-07
  • spring kafka框架中@KafkaListener 注解解讀和使用案例

    spring kafka框架中@KafkaListener 注解解讀和使用案例

    Kafka 目前主要作為一個(gè)分布式的發(fā)布訂閱式的消息系統(tǒng)使用,也是目前最流行的消息隊(duì)列系統(tǒng)之一,這篇文章主要介紹了kafka @KafkaListener 注解解讀,需要的朋友可以參考下
    2023-02-02
  • Spring Boot開(kāi)發(fā)Web應(yīng)用詳解

    Spring Boot開(kāi)發(fā)Web應(yīng)用詳解

    這篇文章主要介紹了Spring Boot開(kāi)發(fā)Web應(yīng)用詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-04-04
  • SpringBoot整合Jasypt實(shí)現(xiàn)配置加密的步驟詳解

    SpringBoot整合Jasypt實(shí)現(xiàn)配置加密的步驟詳解

    Jasypt是一個(gè)Java庫(kù),提供了一種簡(jiǎn)單的加密解密方式,可用于保護(hù)敏感數(shù)據(jù),例如密碼、API密鑰和數(shù)據(jù)庫(kù)連接信息等,本文給大家介紹了SpringBoot整合Jasypt實(shí)現(xiàn)配置加密的詳細(xì)步驟,感興趣的同學(xué)可以參考一下
    2023-11-11
  • java中每月等額與先息后本計(jì)算

    java中每月等額與先息后本計(jì)算

    一般信用貸款會(huì)提供兩種還款方式:每月等額或者先息后本。每月等額,就是每月歸還等同的部分本金和利息,你手里在使用的本金其實(shí)是逐月減少的。先息后本就是先還利息,到期歸還本金。本文將介紹他們的實(shí)現(xiàn)方法。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧
    2017-03-03

最新評(píng)論