官网给出的配置文件大全:

https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#common-application-properties

1.配置文件自动映射到属性

步骤1:Controller上面配置 @PropertySource({"classpath:xxxx.properties"})

步骤2:属性上增加注解

@Value("${test.name}")
private String name;

承接上文的例子:https://www.cnblogs.com/jwen1994/p/11184566.html

我们通过配置文件设置上传文件存放的地址,而不是通过硬编码的方式

注意加粗的部分

@Controller
@PropertySource({"classpath:application.properties"})
public class FileController {@Value("${web.file.path}")private static final String filePath = "F:/IdeaProjects/springbootDemo/src/main/resources/static/images/";@RequestMapping(value = "upload")@ResponseBodypublic JsonData upload(@RequestParam("head_img") MultipartFile file, HttpServletRequest request) {//file.isEmpty(); 判断图片是否为空//file.getSize(); 图片大小进行判断
        String name = request.getParameter("name");System.out.println("用户名:"+name);// 获取文件名String fileName = file.getOriginalFilename();            System.out.println("上传的文件名为:" + fileName);// 获取文件的后缀名,比如图片的jpeg,pngString suffixName = fileName.substring(fileName.lastIndexOf("."));System.out.println("上传的后缀名为:" + suffixName);// 文件上传后的路径fileName = UUID.randomUUID() + suffixName;System.out.println("转换后的名称:"+fileName);File dest = new File(filePath + fileName);try {file.transferTo(dest);return new JsonData(0, fileName);} catch (IllegalStateException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return  new JsonData(-1, "fail to save ", null);}}

2.实体类配置文件

步骤1:添加 @Component 注解。(为了让 Spring Boot 扫描成一个 Bean)

步骤2:使用 @PropertySource 注解指定配置文件位置。

步骤3:使用 @ConfigurationProperties 注解,设置相关属性。

步骤4:必须通过注入 IOC 对象,才能在类中使用获取的配置文件值。

类文件如下:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;//服务器配置
@Component
@PropertySource({"classpath:application.properties"})
@ConfigurationProperties
public class ServerSettings {@Value("${test.appname}")private String name;@Value("${test.domain}")private String domain;//省略getter、setter方法

}

@ConfigurationProperties 可以设置前缀,如果设置了前缀,@Value 就可以不写前缀,修改后的代码如下所示。

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;//服务器配置
@Component
@PropertySource({"classpath:application.properties"})
@ConfigurationProperties(prefix="test")//设置前缀
public class ServerSettings {@Value("${appname}")private String name;@Value("${domain}")private String domain;//省略getter、setter方法

}

如果不写@Value,那么属性值就得和配置文件里的 key 一样。

类文件如下:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;//服务器配置
@Component
@PropertySource({"classpath:application.properties"})
@ConfigurationProperties
public class ServerSettings {private String name;private String domain;//省略getter、setter方法

} 

获取这个对象信息

@Autowired//必须通过IOC依赖注入,才能获取这个对象的值
private ServerSettings serverSettings;@GetMapping("/v1/test_properties")
public Object testPeroperties(){return serverSettings;
}

常见问题:

1)配置文件注入失败,Could not resolve placeholder

解决:根据 Spring Boot 启动流程,会有自动扫描包没有扫描到相关注解,,默认 Spring 框架实现会从声明 @ComponentScan 所在的类的 package 进行扫描,来自动注入,因此启动类最好放在根路径下面,或者指定扫描包范围 Spring Boot 扫描启动类对应的目录和子目录

2)注入 Bean 的方式,属性名称和配置文件里面的 key一一对应,就不用加@Value 这个注解,如果不一样,就要加 @value("${XXX}")

转载于:https://www.cnblogs.com/jwen1994/p/11185514.html

Spring Boot 注解配置文件自动映射到属性和实体类相关推荐

  1. SpringBoot注解把配置文件自动映射到属性和实体类实战

    SpringBoot注解把配置文件自动映射到属性和实体类实战 简介:讲解使用@value注解配置文件自动映射到属性和实体类 1.配置文件加载 方式一 1.Controller上面配置 @Propert ...

  2. Spring Boot —— YAML配置文件

    引言 首先,YAML并不是仅仅可以使用在Java项目中,它是一种类似于json结构的标记语言,可以为所有的编程语言服务.它强调更直观的层级表示,比较适合描述配置文件中的层级关系. Spring Boo ...

  3. Spring Security 实战:Spring Boot 下的自动配置

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 来源 | 公众号「码农小胖哥」 1. 前言 我们在前几篇 ...

  4. 超级详细的Spring Boot 注解总结

    日常编程中我相信大家肯定都用过spring,也用过spring的注解,哪怕面试的时候也经常会被问到一些spring和spring boot注解的作用和含义等,那么这篇就带大家来看看超级详细的Sprin ...

  5. Spring Boot 面试杀手锏—自动配置原理

    点击关注公众号,回复"1024"获取2TB学习资源! 引言 不论在工作中,亦或是求职面试,Spring Boot已经成为我们必知必会的技能项.除了某些老旧的政府项目或金融项目持有观 ...

  6. Spring Boot面试杀手锏————自动配置原理

    引言 不论在工作中,亦或是求职面试,Spring Boot已经成为我们必知必会的技能项.除了某些老旧的政府项目或金融项目持有观望态度外,如今的各行各业都在飞速的拥抱这个已经不是很新的Spring启动框 ...

  7. Spring Boot 灵活实现自动配置背后的故事~用起来更香了

    最近一直忙着在做新应届生的员工技术培训和面试 ,培训的则是Spring Boot部分的内容,这部分也是面试常问的点,于是想到了各位读者大大,特地的把内容分享大家一份. 不知道大家第一次搭Spring ...

  8. Spring Boot注解

    文章目录 简介 @SpringBootApplication @EnableAutoConfiguration 条件自动配置 @ConditionalOnClass 和 @ConditionalOnM ...

  9. java配置文件放置到jar外_java相关:Spring Boot 把配置文件和日志文件放到jar外部...

    java相关:Spring Boot 把配置文件和日志文件放到jar外部 发布于 2020-3-6| 复制链接 如果不想使用默认的application.properties,而想将属性文件放到jar ...

最新文章

  1. 鸿蒙系统比比安卓系统好么,鸿蒙系统与安卓系统的区别 鸿蒙系统与安卓系统哪个更好...
  2. python变量类型-【干货】Python基础语法之变量类型
  3. (转)几种流行的JS框架的选择
  4. 数据库年月日时分秒_数据库基本使用系列(二)
  5. 使用 Vue.js 和 Chart.js 制作绚丽多彩的图表
  6. STM32工作笔记006---常见硬件介绍-以及常见术语--随时更新
  7. Java正则表达式中的捕获组的概念及相关API使用
  8. [FFmpeg] 编译官方例子
  9. 计算机键盘换挡键,电脑键盘上的换挡键是哪个
  10. Android WebView性能分析与优化
  11. 使用pyserial开发的一个串口调试工具
  12. yolo批量检测图片
  13. iOS框架引见--媒体层
  14. undefined symbol 问题解决记录
  15. wordpress的excerpt()函数
  16. 到底什么是UI设计规范
  17. android10暗色适配,Android沉浸式状态栏,支持状态栏渐变色,纯色, 全屏,亮光、暗色模式,适配android 4.4 -10.0机型...
  18. 利用TI的官网Sysconfig配置beagleboard-x15 linux系统中的串口
  19. 到底是加瓦好还是C好呢?
  20. 计算机网络实验报告:【验证性实验】

热门文章

  1. pyspark join代码示例
  2. k8s kubesphere进入ks-installer容器删除istio并重启ks-installer命令
  3. Scala数组的mkString()方法
  4. Python3 打印九九乘法表
  5. MySQL的timestamp类型存储时间范围为什么是2038年之内
  6. 事务的隔离级别演示-避免脏读,演示不可重复读发生
  7. jdbc,mybatis,hibernate各自优缺点及区别
  8. python rm 条件_删除文件 (rm)
  9. Windows访问Fedora共享文件夹
  10. Qt 断言的使用 Q_ASSERT