linux环境properties注释,@Value@PropertySource@ConfigurationProperties注解使用

使用配置文件注入属性

Spring Boot 默认的配置文件src/main/java/resources/application.properties或者src/main/java/resources/application.yml,在这里我们可以配置一些常量。

首先我们使用配置文件给一个类注入相关的属性:

com.xiaohang.controller.pet.no = ${random.uuid}

com.xiaohang.controller.pet.name = Tom

通过注解@Value(value=”${config.name}”)就可以绑定到你想要的属性上面。

package com.xiaohang.demo.controller;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

@RequestMapping("/pet")

public class PetController {

@Value(value = "${com.xiaohang.controller.pet.no}")

private String no;

@Value(value = "${com.xiaohang.controller.pet.name}")

private String name;

@RequestMapping("/d")

public String getDeatils() {

return "no: " + no + ", name: " + name;

}

}

一个个绑定数据还是很不方便,可以新建一个Bean,专门用来绑定注入的属性使用注解@ConfigurationProperties(prefix = “prefix”),不过需要注意的是先要引入相关依赖

org.springframework.boot

spring-boot-configuration-processor

true

通过使用spring-boot-configuration-processor jar,你可以从被@ConfigurationProperties注解的节点轻松的产生自己的配置元数据文件。

这里我新建一个PetBean用来注入属性。

package com.xiaohang.demo.bean;

import lombok.Data;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "com.xiaohang.controller.pet")

@Data

public class PetBean {

private String no;

private String name;

}

使用配置文件给一个类注入相关的属性:

com.xiaohang.controller.pet.no = ${random.uuid}

com.xiaohang.controller.pet.name = Tom

注意在启动类上加上注解

@EnableConfigurationProperties({PetBean.class}),

Application.java

package com.xiaohang.demo;

import com.xiaohang.demo.bean.PetBean;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.context.properties.EnableConfigurationProperties;

@SpringBootApplication

@EnableConfigurationProperties(value = PetBean.class)

public class DemoApplication {

public static void main(String[] args) {

SpringApplication.run(DemoApplication.class, args);

}

}

新建一个controller,注入我们创建的PetBean

package com.xiaohang.demo.controller;

import com.xiaohang.demo.bean.PetBean;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

@RequestMapping("/v2/pet")

public class PetController1 {

@Autowired

private PetBean pet;

@RequestMapping("/d")

public String getDetail() {

return "no: " + pet.getNo() + ", name: " + pet.getName();

}

}

使用自定义的配置文件

我们在resouce目录下面创建一个bean/pet.properties,加入

com.xiaohang.no = 123456

com.xiaohang.name = Tom

新建一个PetBean1.java:

@PropertySource 这个注解可以指定具体的属性配置文件,优先级比较低。

package com.xiaohang.demo.bean;

import lombok.Data;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.PropertySource;

@Configuration

@ConfigurationProperties(prefix = "com.xiaohang")

@PropertySource(value = "classpath:bean/pet.properties")

@Data

public class PetBean1 {

private String no;

private String name;

}

在controller中加入PetBean1的注入

package com.xiaohang.demo.controller;

import com.xiaohang.demo.bean.PetBean1;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

@RequestMapping("v1/pet")

public class PetController2 {

@Autowired

private PetBean1 pet1;

@RequestMapping("d1")

public String getDetails() {

return "no: " + pet1.getNo() + ", name: " + pet1.getName();

}

}

随机数

my.secret=${random.value}

my.number=${random.int}

my.bignumber=${random.long}

my.number.less.than.ten=${random.int(10)}

my.number.in.range=${random.int[1024,65536]}

属性占位符

app.name=MyApp

app.description=${app.name} is a Spring Boot application

可以在配置文件中引用前面配置过的属性(优先级前面配置过的这里都能用)。

通过如${app.name:默认名称}方法还可以设置默认值,当找不到引用的属性时,会使用默认的属性。

属性名匹配规则

例如有如下配置对象:

@Component

@ConfigurationProperties(prefix="person")

public class ConnectionSettings {

private String firstName;

}

firstName可以使用的属性名如下:

person.firstName,标准的驼峰式命名

person.first-name,虚线(-)分割方式,推荐在.properties和.yml配置文件中使用

PERSON_FIRST_NAME,大写下划线形式,建议在系统环境变量中使用

配置文件的优先级

Spring Boot 支持多种外部配置方式,这些方式优先级如下:

命令行参数

来自java:comp/env的JNDI属性

Java系统属性(System.getProperties())

操作系统环境变量

RandomValuePropertySource配置的random.*属性值

jar包外部的application-{profile}.properties或application.yml(带spring.profile)配置文件

jar包内部的application-{profile}.properties或application.yml(带spring.profile)配置文件

jar包外部的application.properties或application.yml(不带spring.profile)配置文件

jar包内部的application.properties或application.yml(不带spring.profile)配置文件

@Configuration注解类上的@PropertySource

通过SpringApplication.setDefaultProperties指定的默认属性

同样,这个列表按照优先级排序,也就是说,src/main/resources/config下application.properties覆盖src/main/resources下application.properties中相同的属性,此外,如果你在相同优先级位置同时有application.properties和application.yml,那么application.properties里的属性里面的属性就会覆盖application.yml。

Profile-多环境配置

当应用程序需要部署到不同运行环境时,一些配置细节通常会有所不同,最简单的比如日志,生产日志会将日志级别设置为WARN或更高级别,并将日志写入日志文件,而开发的时候需要日志级别为DEBUG,日志输出到控制台即可。

如果按照以前的做法,就是每次发布的时候替换掉配置文件,这样太麻烦了,Spring Boot的Profile就给我们提供了解决方案,命令带上参数就搞定。

这里我们来模拟一下,只是简单的修改端口来测试

在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,比如:

application-dev.properties:开发环境

application-prod.properties:生产环境

然后在application.properties中加入

spring.profiles.active=dev

或application.yml中加入

spring:

# 环境 dev|test|pro

profiles:

active: dev

或启动命令

java -jar xxx.jar --spring.profiles.active=dev

参数用–xxx=xxx的形式传递。意思就是表示在application.properties文件中配置了属性。

可以通过SpringApplication.setAddCommandLineProperties(false)禁用命令行配置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值