欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
SpringBoot的properties和yml兩種配置方式, 配置注入參數, 以及配置文件讀取失效的問(wèn)題

SpringBoot支持兩種配置方式,一種是properties文件,一種是yml

首先在pom文件中添加依賴(lài):

<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-configuration-processor</artifactId>    <optional>true</optional></dependency>

1. yml配置文件綁定屬性

使用properties可能會(huì )有中文亂碼的問(wèn)題,而使用yml可以避免這種情況
,yml的結構與json相似:
注意: yml冒號后要有空格,如果不加空格, 會(huì )導致yml配置讀取失效


沒(méi)有空格, 顏色都沒(méi)有變色,顯然有問(wèn)題.

list用-,使用markdown的都應該熟悉,map是跟json一樣key: value

user:   username: "小紅"   password: "${user.username}123"   # 100以?xún)鹊碾S機數   age: ${random.int(100)}   array : 1,2,3,4,5,6,7   strlist:      - list1     - list2     - list3   maplist:      - name: abc       value: abcValue     - name: 123       value: 123Value   map:      key1: value1     key2: value2server:  # 端口號   port: 8081

對應的User類(lèi):
@ConfigurationProperties這個(gè)注解,會(huì )將yml配置文件中屬性名一樣的值注入到User類(lèi)對應的字段中, 相當于給每個(gè)字段加上@Value

@Component//@ConfigurationProperties設置前綴user@ConfigurationProperties(prefix = "user")public class User {    private int userid;    private String username;    private String password;    private int age;    private String[] array ;    private List<Map<String, String>> maplist = new ArrayList<Map<String, String>>();    private List<String> strlist = new ArrayList<>();    private Map<String, String> map = new HashMap<>();.....

比如password,對應的配置是user.password,所以設置一個(gè)前綴user

Controller層

@RestController@RequestMapping("/boot")public class MyController {    @Autowired    private User user;    @RequestMapping("/hello")    public String hello() {        return "這是第一個(gè)springboot";    }    @RequestMapping("/user")    public User getUser() throws JsonProcessingException {        ObjectMapper objectMapper = new ObjectMapper();        //測試加載yml文件        System.out.println("username: " + user.getUsername());        System.out.println("username: " + user.getPassword());        System.out.println("age: " + user.getAge());        System.out.println("array: " + objectMapper.writeValueAsString(user.getArray()));        System.out.println("maplist: " + objectMapper.writeValueAsString(user.getMaplist()));        System.out.println("strlist: " + objectMapper.writeValueAsString(user.getStrlist()));        System.out.println("map: " + objectMapper.writeValueAsString(user.getMap()));        return user;    }}

頁(yè)面顯示結果和控制臺結果


2. properties配置方式

*.properties文件中的中文默認以ISO-8859-1方式編碼,因此需要對中文內容進(jìn)行重新編碼

public String getTitle3() {        try {            return new String(title3.getBytes("ISO-8859-1"), "UTF-8");        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        }        return title3;    }

2.1 @ConfigurationProperties方式

application.properties

com.zyd.type3=Springboot - @ConfigurationPropertiescom.zyd.title3=使用@ConfigurationProperties獲取配置文件#mapcom.zyd.login[username]=zhangdeshuaicom.zyd.login[password]=zhenshuaicom.zyd.login[callback]=http://www.flyat.cc#listcom.zyd.urls[0]=http://ztool.cccom.zyd.urls[1]=http://ztool.cc/format/jscom.zyd.urls[2]=http://ztool.cc/str2imagecom.zyd.urls[3]=http://ztool.cc/json2Entitycom.zyd.urls[4]=http://ztool.cc/ua

在需要賦值的類(lèi)上打上注解@ConfigurationProperties

@Component@ConfigurationProperties(prefix = "com.zyd")// PropertySource默認取application.properties// @PropertySource(value = "config.properties")public class PropertiesConfig {    public String type3;    public String title3;    public Map<String, String> login = new HashMap<String, String>();    public List<String> urls = new ArrayList<>();    // 轉換編碼格式    public String getTitle3() {        try {            return new String(title3.getBytes("ISO-8859-1"), "UTF-8");        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        }        return title3;    }    ....getter setter

main Application類(lèi):

@SpringBootApplication@RestControllerpublic class Applaction {    @Autowired    private PropertiesConfig propertiesConfig;    /**     *      * 第一種方式:使用`@ConfigurationProperties`注解將配置文件屬性注入到配置對象類(lèi)中     *      * @author zyd     * @throws UnsupportedEncodingException     * @since JDK 1.7     */    @RequestMapping("/config")    public Map<String, Object> configurationProperties() {        Map<String, Object> map = new HashMap<String, Object>();        map.put("type", propertiesConfig.getType3());        map.put("title", propertiesConfig.getTitle3());        map.put("login", propertiesConfig.getLogin());        map.put("urls", propertiesConfig.getUrls());        return map;    }    public static void main(String[] args) throws Exception {        SpringApplication application = new SpringApplication(Applaction.class);        application.run(args);    }}

運行結果:

{"title":"使用@ConfigurationProperties獲取配置文件","urls":["http://ztool.cc","http://ztool.cc/format/js","http://ztool.cc/str2image","http://ztool.cc/json2Entity","http://ztool.cc/ua"],"login":{"username":"zhangdeshuai","callback":"http://www.flyat.cc","password":"zhenshuai"},"type":"Springboot - @ConfigurationProperties"}縮進(jìn)量:    引號   顯示控制  展開(kāi) 疊起 2級 3級 4級 5級 6級 7級 8級 格式化JSON:{    "title": "使用@ConfigurationProperties獲取配置文件",     "urls": [        "http://ztool.cc",         "http://ztool.cc/format/js",         "http://ztool.cc/str2image",         "http://ztool.cc/json2Entity",         "http://ztool.cc/ua"    ],     "login": {        "username": "zhangdeshuai",         "callback": "http://www.flyat.cc",         "password": "zhenshuai"    },     "type": "Springboot - @ConfigurationProperties"}

2.2 @Value注解方式

在需要賦值的屬性上,加上@value注解
main Application類(lèi):

@SpringBootApplication@RestControllerpublic class Applaction {    @Value("${com.zyd.type}")    private String type;    @Value("${com.zyd.title}")    private String title;    /**     *      * 第二種方式:使用`@Value("${propertyName}")`注解     *      * @author zyd     * @throws UnsupportedEncodingException     * @since JDK 1.7     */    @RequestMapping("/value")    public Map<String, Object> value() throws UnsupportedEncodingException {        Map<String, Object> map = new HashMap<String, Object>();        map.put("type", type);        // *.properties文件中的中文默認以ISO-8859-1方式編碼,因此需要對中文內容進(jìn)行重新編碼        map.put("title", new String(title.getBytes("ISO-8859-1"), "UTF-8"));        return map;    }    public static void main(String[] args) throws Exception {        SpringApplication application = new SpringApplication(Applaction.class);        application.run(args);    }}

運行結果

{"title":"使用@Value獲取配置文件","type":"Springboot - @Value"}
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
SpringBoot非官方教程 | 第二篇:Spring Boot配置文件詳解
SpringBoot-配置文件
springboot yml 配置文件注入Map,List
關(guān)于SpringBoot的application.yml的相關(guān)配置(自定義,開(kāi)發(fā),測試,正式)切換
Spring Boot第二彈,配置文件怎么造?
SpringBoot中這5種高大上的yml文件讀取方式,你知道嗎?
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久