Springboot(三)配置文件自定义

一、自定义属性

使用注解

@Value("${属性名}")

在用IDEA创建一个Spring Boot工程时,系统默认会在src/main/java/resources目录建一个配置文件application.properties。它也支持yml格式的文件,下面以yml格式的文件来讲解如何自定义属性。

在工程的配置文件application.yml自定义一组属性,如下:

my:
  name: marvin
  age: 18

如果要读取配置文件application.yml的属性值,只需在变量上加@Value("${属性名}")注解,就可以将配置文件application.yml的一个属性值赋给一个变量。新建一个Controller,其代码清单如下:

@RestController
public class MyController {
	@Value("${my.name}")
	private String name;
	@Value("${my.age}")
	private int age;

	@RequestMapping(value = "/getresult")
	public String getResult(){
		return name+":"+age;
	}
}
启动工程 SpringBoot ,打开浏览器访问“ 句://localhost:8080/getresult ”,浏览器显示如下:
marvin:18
这说明配置文件 application.yml 属性 my.name my.age 经成功读入应用程序中。
 
 

二、将配置文件属性赋值给实体类

使用注解

@ConfigurationProperties(prefix = "配置文件中的数据层级")

@EnableConfigurationProperties({实体类.class})

当有很多配置属性时,如果逐个地读取属性会非常麻烦。通常的做法会把这些属性名作为变量名来创建 JavaBean 变量,并将属性值赋给 JavaBean 变量的值。
 
1、在配置文件 Application.yml 中添加如下属性
my:
  name: marvin
  age: 18
  number: ${random.int}
  uuid: ${random.uuid}
  max: ${random.int(10)}
  value: ${random.value}
  greeting: hi,i`m ${my.name}

其中,配置文件中用到了 ${random},它可以用来生成各种不同类型的随机值。
random.in:随机生成一个 int 类型的值;
random.uuid: 随机生成 uuid;
random.value: 随机生成 个值;
random.int(10):随机生成一个小于10的整数。


2、怎么将这些属性赋给 JavaBean 呢?创建一个 JavaBean ,其代码清单如下:

@ConfigurationProperties(prefix = "my")
@Component
public class ConfigBean {
	private String name;
	private int age;
	private int number;
	private String uuid;
	private int max;
	private String value;
	private String greeting;
	//省略getter setter...
}

在上面的代码中,在 bean 类添加注解 @ConfigurationProperties,表明该类为配置属性类,并加上配置的 prefix ,例如本案例的 "my"。
另外需要在 bean 类加 @Component 注解(Spring Boot 启动时通过包扫描将该类作为 Bean 注入 IoC 容器中)
创建 Controller,读取 ConfigBean 类的属性。
Controller 类,加 @EnableConfigurationProperties 注解,并指明 bean 类,其代码清单如下

@RestController
@EnableConfigurationProperties({ConfigBean.class})
public class MyController {

	@Autowired
	private ConfigBean configBean;

	@RequestMapping(value = "/getmarvin")
	public String getResult(){
		return configBean.getGreeting()+"-"+ configBean.getName()+"-"+configBean.getUuid()+"-"+configBean.getMax();
	}
}

 

三、自定义配置文件

使用注解

@Configuration

@PropertySource(value = "classpath:文件名.properties")

@ConfigurationProperties(prefix = "配置文件中的数据层级")

@EnableConfigurationProperties({实体类.class})

上面介绍了如何把配置属性写到 application.yml 文件中,并把配置属性读取到一个配置类中。
有时属性太多,把所有的配置属性都写到 application.yml 配置文件中不太合适。
这时需要自定义配置文件。例如 src/main/resources 目录下定义 test.properties 文件

com.forezp.name=marvin
com.forezp.age=18

如何将这个配置文件 test.properties 的属性性和属性值赋给 JavaBean 呢?
在类名上添加 @Configuration@PropertySource@ConfigurationProperties
需要注意的是,若Spring Boot 1.4 或 1.4 之前,则需要在 @PropertySource 注解上加 location 指明该配置文件的路径。本案例采用的 Spring Boot 版本为 1.5 

@Configuration
@PropertySource(value = "classpath:test.properties")
@ConfigurationProperties(prefix = "com.forezp")
public class UserBean {
	private  String name;
	private int age;
	//省略getter setter...
}

写一个 MyController 在类的上方加上 @RestController 注解,开启 RestControllerde的功能
加上 @EnableConfigurationProperties 注解,并指明要引用的 JavaBean 开启引用配置属性的功能

@RestController
@EnableConfigurationProperties({ConfigBean.class, UserBean.class})
public class MyController {
	@Autowired
	private ConfigBean configBean;
	@RequestMapping(value = "/getmarvin")
	public String getResult(){
		return configBean.getGreeting()+"-"+ configBean.getName()+"-"+configBean.getUuid()+"-"+configBean.getMax();
	}

	@Autowired
	private UserBean userBean;
	@RequestMapping(value = "/getUser")
	public String getUeser(){
		return userBean.getName()+":"+userBean.getAge();
	}
}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章