@ConfigurationProperties 和 自动补全 添加自定义的属性

22 篇文章 0 订阅
12 篇文章 0 订阅

前言:大家都知道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"
    }
  ]
}

编译之后就可以了,原理之后我会另外写文章,如果有需要,请留言

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孔明兴汉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值