springboot标签小笔记----@ConfigurationProperties
2018-08-30 10:55
218 查看
1、spring boot 使用@ConfigurationProperties
a) yml配置
b)我们可以定义一个实体类在装载配置文件信息
c)使用
controller中注入
@Autowired private BootdoConfig bootdoConfig;
/** * 删除 */ @PostMapping("/remove") @ResponseBody // @RequiresPermissions("common:remove") public R remove(Long id, HttpServletRequest request) { if ("test".equals(getUsername())) { return R.error(1, "演示系统不允许修改,完整体验请部署程序"); } String fileName = bootdoConfig.getUploadPath() + sysFileService.get(id).getUrl().replace("/files/", ""); if (sysFileService.remove(id) > 0) { boolean b = FileUtil.deleteFile(fileName); if (!b) { return R.error("数据库记录删除成功,文件删除失败"); } return R.ok(); } else { return R.error(); } }
我们还可以把@ConfigurationProperties还可以直接定义在@bean的注解上,这是bean实体类就不用@Component和@ConfigurationProperties了
[code]@SpringBootApplication public class DemoApplication{ //... @Bean @ConfigurationProperties(prefix = "connection") public ConnectionSettings connectionSettings(){ return new ConnectionSettings(); } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
然后我们需要使用的时候就直接这样子注入
[code]@RestController @RequestMapping("/task") public class TaskController { @Autowired ConnectionSettings conn; @RequestMapping(value = {"/",""}) public String hellTask(){ String userName = conn.getUsername(); return "hello task !!"; } }
如果发现@ConfigurationPropertie不生效,有可能是项目的目录结构问题,你可以通过@EnableConfigurationProperties(ConnectionSettings.class)来明确指定需要用哪个实体类来装载配置信息。
阅读更多相关文章推荐
- spring boot 使用@ConfigurationProperties
- 关于spring boot自动注入出现Consider defining a bean of type 'xxx' in your configuration问题解决方案 标签: spring 201
- spring boot 注入properties属性文件的值 @ConfigurationProperties
- spring boot 使用@ConfigurationPropertiesu将配置自动封装成实体类
- spring-boot配置(一):@Configuration,@ConfigurationProperties和application.yml
- Spring Boot第四弹:Spring Boot之@Value和@ConfigurationProperties
- 在Spring Boot中使用 @ConfigurationProperties 注解
- spring boot -Properties & configuration
- spring boot@ConfigurationProperties
- springboot @ConfigurationProperties
- Spring Boot 之 @ConfigurationProperties
- 解决spring boot1.5以上版本@ConfigurationProperties提示“Spring Boot Configuration Annotation Processor not.."
- Spring boot 学习笔记(1)——hello world创建,属性application.properties、 yml配置
- springboot 学习笔记(二)--- properties 配置
- springboot项目接入配置中心,实现@ConfigurationProperties的bean属性刷新方案
- Spring Boot核心原理-自动配置 以及@ConfigurationProperties 注解
- spring boot:使用@ConfigurationProperties注解加载配置文件
- spring boot @ConfigurationProperties @EnableConfigurationProperties 配置注解使用
- Spring Boot 引入 properties 文件的笔记
- spring boot1.5以上版本@ConfigurationProperties取消location注解后的替代方案 cannot resolve method location