1.配置文件
可以更改配置,比如端口号等等
在Resource文件夹创建配置文件application.properties
server.port=8090
即可设置端口号为8090.
2.json格式返回值
以前需要@ResponseBody,现在只需要将@Controller改为@RestController即可。
3.配置常量
补充:通过注解指向类,需要配置处理器,pom.xml已有(简单配置一)
1)可以通过注解方式
// @Value("${rice}")
// private String rice;
// @Value("${meat}")
// private String meat;
2)一个bean
bean上添加注解
@ConfigurationProperties(prefix = "food")
public class FoodConfig {
private String rice;
private String meat;
入口类添加注解
@SpringBootApplication
@EnableConfigurationProperties({FoodConfig.class})
public class Demo {
public static void main(String[] args) {
SpringApplication.run(Demo.class,args);
}
}
我的目录结构:
3)多个bean
bean上添加注解:
@Configuration
@ConfigurationProperties(prefix = "vegetables")
@PropertySource("classpath:vegetables.properties")
public class VegetablesConfig {
private String potato;
private String eggplant;
private String greenPepper;
public String getPotato() {
return potato;
}
public void setPotato(String potato) {
this.potato = potato;
}
public String getEggplant() {
return eggplant;
}
public void setEggplant(String eggplant) {
this.eggplant = eggplant;
}
public String getGreenPepper() {
return greenPepper;
}
public void setGreenPepper(String greenPepper) {
this.greenPepper = greenPepper;
}
}
配置文件内容:
4)注意!
properties文件可能乱码
需要修改idea的配置
将图中的Transparent native-to。。。。勾选,全都设置为UTF-8,然后重新输入配置文件的内容



