前言:大家都知道application.properties,裏面的屬性是怎麼配置生效的,又是爲什麼會有那些Alt + / 出來的提示呢?
首先
第一個問題:application.properties,裏面的屬性是怎麼配置生效的
一.通過@Value 註解
xgf.port=8888
package com.springcloud.xgf;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@EnableEurekaServer
@SpringBootApplication
@RestController
public class EurekaApplication {
@Value("${xgf.port}")
Integer port;
@RequestMapping("/test/xgf/port")
public Integer test() {
return port;
}
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
注意:在@Value的${}中包含的是核心配置文件中的鍵名。在Controller類上加@RestController表示將此類中的所有視圖都以JSON方式顯示,類似於在視圖方法上加@ResponseBody。
訪問:http://localhost:8761/test/xgf/port 時得到:8888
(2)方式二:使用Environment方式
package com.springcloud.xgf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@EnableEurekaServer
@SpringBootApplication
@RestController
public class EurekaApplication {
@Autowired
Environment env;
@Value("${xgf.port}")
Integer port;
@RequestMapping("/test/xgf/port")
public Integer test() {
return env.getProperty("xgf.port");
}
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
問題1:如果env.getProperty("xgf.port") 獲取不到值的時候 會有什麼效果呢?
返回null
問題2:Environment是怎麼加載的,爲什麼會有指定參數?
Environment是spring-core中的一個類,具體的請看https://www.jb51.net/article/145192.htm
(3).通過@ConfigurationProperties類 裏面有value,prefix,ignoreInvalidFields,ignoreUnknownFields
value 有效綁定到對象屬性的值
prefix 前綴
ignoreInvalidFields 用於指示綁定到此對象時應忽略無效字段的標誌。
ignoreUnknownFields 用於指示綁定到此對象時應忽略未知字段的標誌。
package com.springcloud.xgf;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "xgf", ignoreUnknownFields = true)
public class XgfProperties {
private Integer port;
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
}
package com.springcloud.xgf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@EnableEurekaServer
@SpringBootApplication
@RestController
public class EurekaApplication {
@Autowired
XgfProperties properties;
@Autowired
Environment env;
@Value("${xgf.port}")
Integer port;
@RequestMapping("/test/xgf/port")
public Integer test() {
return properties.getPort();
}
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
這三種個人好處,推薦1 和 3
怎麼讓eclipse 的 自動補全添加我們自定義的呢 達到這樣的效果
src/main/resources下新建META-INF 文件夾 再新建spring-configuration-metadata.json 文件
{
"hints": [],
"groups": [
{
"sourceType": "com.springcloud.xgf.XgfProperties",
"name": "xgf",
"type": "com.springcloud.xgf.XgfProperties"
}
],
"properties": [
{
"sourceType": "com.springcloud.xgf.XgfProperties",
"name": "xgf.port",
"type": "java.lang.Integer"
}
]
}
編譯之後就可以了,原理之後我會另外寫文章,如果有需要,請留言