如果有大量的参数配置在 application.properties 或 application.yml 文件中,通过 @ConfigurationProperties 注解,我们可以方便的获取这些参数值。具体使用方法如下:
1.创建AutoConfigProperty类
方法一:
@ConfigurationProperties(prefix="auto")
@Component
public class AutoConfigProperty {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
方法二:
创建configure类,添加@EnableConfigurationProperties,使用此方法时,AutoConfigProperty类中的@Component去掉
@Configuration
@EnableConfigurationProperties({AutoConfigProperty.class})
public class AutoConfigure {
}
2.添加auto开头的配置
3.测试使用
创建测试controller
@Controller("testProperty")
public class AutoPropertyController {
@Autowired
private AutoConfigProperty autoConfigProperty;
@RequestMapping("autoMethod")
public void autoMethod(){
System.out.println("==========="+autoConfigProperty.getName()+"+++++++++");
}
}