自定义的properties文件
1.创建一个自定义文件,我在resource下创建的myapplication.properties
osChina.name=hua
osChina.gender=male
2.创建一个实体类,与配置文件相对应
@ConfigurationProperties(prefix = "osChina",locations = "classpath:myapplication.properties")
public class Myapplication {
private String name;
private String gender;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
3.创建一个controller
@Controller
public class PropertiesController {
@Autowired
private Myapplication myapplication;
@RequestMapping("/properties")
@ResponseBody
public String propertiesTest(){
System.out.println("name:"+myapplication.getName()+",gender:"+myapplication.getGender());
return "ok";
}
}
4.需要在入口处添加
@EnableConfigurationProperties({Myapplication.class})
这个注解,否则不会生效
如果在application.properties文件中增加配置会更加简单,过程类似,只是在实体上不用配置
locations = "classpath:myapplication.properties"