上篇学习了一个简单的SpringBoot项目例子,下面接着上一篇的例子进行配置文件的学习。
SpringBoot使用了一个全局配置文件application.properties,该文件在\src\main\resources\目录下,该全局配置文件的作用是对一些默认的配置进行修改。
一、自定义属性
application.properties配置文件支持自定义属性的支持,这样可以把一些常量配置在改配置文件里面。在改配置文件里面进行如下配置:
配置完这些属性,使用的时候就在需要使用的地方通过注解@Value("${cn.org.marshal.name}")就可以将配置的属性值绑定到需要的属性上面。
启动项目:打开浏览器访问http://localhost:8080就可以看到输出内容
有时候属性太多,不适合一个一个绑定到属性字段上。针对这种情况官方提倡绑定成一个bean对象,这里创建一个Java的Bean类,顶部需要使用注解@ConfigurationProperties(prefix = "cn.org.marshal")类指定使用对应的那些属性:
package cn.org.marshal.springboot.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "cn.org.marshal")
public class UserPojo {
private String name;
private int age;
public UserPojo() {}
public UserPojo(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
上面的bean类配置完之后,还需要在SpringBoot的入口处使用@EnableConfigurationProperties({UserPojo.class})指定需要加载那个bean类。如果不写UserPojo.class,在bean类那边添加
package cn.org.marshal.springboot;
import cn.org.marshal.springboot.pojo.UserPojo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@SpringBootApplication
@EnableConfigurationProperties({UserPojo.class})
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
最后在Controller类中引入UserPojo即可:
package cn.org.marshal.springboot.controller;
import cn.org.marshal.springboot.pojo.UserPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SpringbootController {
@Autowired
UserPojo userPojo;
@RequestMapping("/")
public String index() {
return "Hello ! " + userPojo.getName() + ", 你已经" + userPojo.getAge() + "岁了。";
}
}
二、参数间的引用
在application.properties中的各个参数间可以互相引用
cn.org.marshal.name="Marshal"
cn.org.marshal.age=28
cn.org.marshal.nameAge=${cn.org.marshal.name}, 你已经${cn.org.marshal.age}
这样就只需要调用cn.org.marshal.nameAge属性就可以了。
三、使用自定义配置文件
有时候我们不希望把所有配置文件都放在application.properties里面,这时候我们可以另外定义一个,路径也在\src\main\resources\目录下
cn.org.marshal.name="Marshal"
cn.org.marshal.age=28
cn.org.marshal.nameAge=${cn.org.marshal.name}, 你已经${cn.org.marshal.age}
创建一个UserBean类:
注意:需要添加@Configuration和@PropertySource("classpath:applicationtest.properties")后才可以读取,1.5以前的版本可以通过locations指定properties文件的位置@ConfigurationProperties(prefix = "config2",locations="classpath:applicationtest.properties")
package cn.org.marshal.springboot.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ConfigurationProperties(prefix = "cn.org.marshal")
@PropertySource("classpath:applicationtest.properties")
public class UserBean {
private String name;
private int age;
private String nameAge;
public UserBean() {}
public UserBean(String name, int age, String nameAge) {
this.name = name;
this.age = age;
this.nameAge = nameAge;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getNameAge() {
return nameAge;
}
public void setNameAge(String nameAge) {
this.nameAge = nameAge;
}
}
四、随机配置文件
在配置文件中使用${random}来生成各种不同类型的的随机值,从而简化了代码生成的麻烦。
cn.org.marshal.intValues=${random.int}
五、外部参数配置-命令行参数配置