@ConfigurationProperties 和 自动补全 添加自定义的属性
前言:大家都知道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"
}
]
}
编译之后就可以了,原理之后我会另外写文章,如果有需要,请留言