Springboot聯結萬物學習筆記--Springboot微服務基礎搭建篇(二)--自定義配置的方法
阿新 • • 發佈:2021-06-26
部落格說明:撰寫部落格目的是在記錄自己所學知識、在工作中使用技術遇到的技術問題、一些技術感悟,因此避免不了涉及到和其他文章有相似之處。本文從作者自己的實踐中指出相關踩坑問題,著重指出學習過程中遇到的相關問題。如果存在相關侵權問題請聯絡博主刪除,同時有技術上的見解可以在評論去裡發出,會不定期回覆,謝謝。
01目標
1、利用@value自定義配置類使用
2、新建配置類引入配置
02SpringBoot關於自定義屬性@Value注入
在resource檔案下面建立application-dev.yml
server: port: 8080 #自定義屬性 woniu: info: id: 12 type: happy desc: happy_woniu hobby: hava fun
修改application.yml
# 環境啟用
spring:
profiles:
active: dev
建立service包並建立ConfigTestService類
寫入:
package com.hkx.demo.service; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; /** * 測試配置類使用 */ @Service public class ConfigTestService { @Value("${woniu.info.id}") private Integer id; @Value("${woniu.info.type}") private String type; @Value("${woniu.info.desc}") private String desc; @Value("${woniu.info.hobby}") private String hobby; public void testvalue(){ System.out.println(id); System.out.println(type); System.out.println(desc); System.out.println(hobby); } }
在上一次建立的工程中的hello控制層寫GET介面驗證
@Autowired
ConfigTestService configTestService;
@GetMapping("/test/value")
public void testValue(){
configTestService.testvalue();
}
瀏覽器呼叫
http://localhost:8080/test/value
結果:
03SpringBoot關於@ConfigurationProperties注入屬性
建立config包並建立WoniuProperties類
寫入:
package com.hkx.demo.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "woniu.info")
@Configuration
@Data
public class WoniuProperties {
//id
private Integer id;
//type
private String type;
//desc
private String desc;
//hobby
private String hobby;
}
本工程引用了lombok代替set、get,如何使用自行百度,或後期等作者跟新如何使用
ConfigTestService類更新:
package com.hkx.demo.service;
import com.hkx.demo.config.WoniuProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
/**
* 測試配置類使用
*/
@Service
public class ConfigTestService {
@Value("${woniu.info.id}")
private Integer id;
@Value("${woniu.info.type}")
private String type;
@Value("${woniu.info.desc}")
private String desc;
@Value("${woniu.info.hobby}")
private String hobby;
@Autowired
WoniuProperties woniuProperties;
public void testvalue(){
System.out.println(id);
System.out.println(type);
System.out.println(desc);
System.out.println(hobby);
}
public void testConfigClass(){
System.out.println(woniuProperties.getId());
System.out.println(woniuProperties.getType());
System.out.println(woniuProperties.getDesc());
System.out.println(woniuProperties.getHobby());
}
}
Hello控制層更新:
package com.hkx.demo.controller;
import com.hkx.demo.service.ConfigTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Hello {
@Autowired
ConfigTestService configTestService;
@GetMapping("/hello")
public String hello(){
return "hello woniurunfast";
}
@GetMapping("/test/value")
public void testValue(){
configTestService.testvalue();
}
@GetMapping("/test/configclass")
public void testConfigClass(){
configTestService.testConfigClass();
}
}
瀏覽器呼叫
http://localhost:8080/test/configclass
結果: