多环境配置
自定义属性与加载 方法一
### 自定义属性
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]}