多环境配置 和 自定义属性与加载

 多环境配置

自定义属性与加载 方法一

### 自定义属性
cn.ay.book.name=spring boot 2
cn.ay.book.author=ay

@Value("${cn.ay.book.name}") // 加载对应的配置属性

package cn.stevekung.properties;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BookProperties {
    @Value("${cn.ay.book.name}") // 加载对应的配置属性
    private String bookName;
    @Value("${cn.ay.book.author}")
    private String author;
}
@Resource
    private BookProperties bookProperties;

    @Test
    public void testProperties(){
        System.out.println(bookProperties.getBookName());
        System.out.println(bookProperties.getAuthor());
    }

自定义属性与加载 方法二

<!--spring boot 配置处理类-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
		</dependency>

@ConfigurationProperties(prefix = "cn.ay.book")

package cn.stevekung.properties;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
@ConfigurationProperties(prefix = "cn.ay.book")
public class BookProperties {
//    @Value("${cn.ay.book.name}") // 加载对应的配置属性
    private String bookName;
//    @Value("${cn.ay.book.author}")
    private String author;
}

@EnableConfigurationProperties({BookProperties.class})

package cn.stevekung;

import cn.stevekung.properties.BookProperties;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@ServletComponentScan // 配置过滤器 用到
@EnableAsync // 开启异步
@EnableRetry // 开启重试
@ImportResource(value = {"classpath:spring-dubbo.xml"})
@EnableConfigurationProperties({BookProperties.class})
public class SpringBootBookV2Application {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootBookV2Application.class, args);
	}

	@Bean
	public ObjectMapper objectMapper(){
		return new ObjectMapper();
	}

}

参数间引用

#### 参数间引用
cn.ay.book.desc=${cn.ay.book.name} : ${cn.ay.book.author}

生成随机数

### 随机字符串:32位MD5字符串
cn.ay.book.randomValue=${random.value}
### 随机生成int类型的数
cn.ay.book.randomInt=${random.int}
### 随机生成long类型的数
cn.ay.book.randomLong=${random.long}
### 随机生成uuid
cn.ay.book.randomUuid=${random.uuid}
### 10以内的随机数
cn.ay.book.randomLen=${random.int(10)}
### 随机生成[1024,65536]范围内的数
cn.ay.book.randomRange=${random.int[1024,65536]}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值