SpringBoot注解把配置文件自动映射到属性和实体类实战
SpringBoot注解把配置文件自动映射到属性和实体类实战
简介:讲解使用@value注解配置文件自动映射到属性和实体类
1、配置文件加载
方式一
1、Controller上面配置
@PropertySource({"classpath:resource.properties"})
2、增加属性
@Value("${test.name}")
private String name;
文件上传修改示例:
FileController.java:
1 package net.xdclass.demo.controller; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.util.UUID; 6 7 import javax.servlet.http.HttpServletRequest; 8 9 import net.xdclass.demo.domain.JsonData; 10 11 import org.springframework.beans.factory.annotation.Value; 12 import org.springframework.context.annotation.PropertySource; 13 import org.springframework.stereotype.Controller; 14 import org.springframework.web.bind.annotation.RequestMapping; 15 import org.springframework.web.bind.annotation.RequestParam; 16 import org.springframework.web.bind.annotation.ResponseBody; 17 import org.springframework.web.multipart.MultipartFile; 18 19 /** 20 * 功能描述:文件测试 21 * 22 * <p> 创建时间:Apr 22, 2018 11:22:29 PM </p> 23 */ 24 @Controller 25 @PropertySource({"classpath:application.properties"}) 26 public class FileController { 27 28 @RequestMapping(value = "/api/v1/gopage") 29 public Object index() { 30 return "index"; 31 } 32 33 //private static final String filePath = "L:/Workspaces/Eclipse_Neon/txkt/SpringBootClass/xdclass_springboot/src/main/resources/static/images/";//末尾需要加/,这样才能写进来 34 //private static final String filePath = "L:/images/";//末尾需要加/,这样才能写进来 35 36 @Value("${web.file.path}") 37 private String filePath; 38 39 @RequestMapping(value = "upload") 40 @ResponseBody 41 public JsonData upload(@RequestParam("head_img") MultipartFile file, HttpServletRequest request) { 42 43 // file.isEmpty(); 判断图片是否为空 44 // file.getSize(); 图片大小进行判断 45 46 System.out.println("配置注入打印,文件路径为:" + filePath); 47 48 String name = request.getParameter("name"); 49 System.out.println("用户名:" + name); 50 51 // 获取文件名 52 String fileName = file.getOriginalFilename(); 53 System.out.println("上传的文件名为:" + fileName); 54 55 // 获取文件的后缀名,比如图片的jpeg,png 56 String suffixName = fileName.substring(fileName.lastIndexOf(".")); 57 System.out.println("上传的后缀名为:" + suffixName); 58 59 // 文件上传后的路径 60 fileName = UUID.randomUUID() + suffixName; 61 System.out.println("转换后的名称:" + fileName); 62 63 File dest = new File(filePath + fileName); 64 65 try { 66 file.transferTo(dest); 67 68 return new JsonData(0, fileName); 69 } catch (IllegalStateException e) { 70 e.printStackTrace(); 71 } catch (IOException e) { 72 e.printStackTrace(); 73 } 74 return new JsonData(-1, "fail to save ", null); 75 } 76 77 }
application.properties:
1 web.images-path=L:/images 2 spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/test/,file:${web.upload-path} 3 #指定某些文件不进行监听,即不会进行热加载devtool(重启后不会监听下面这个文件) 4 #spring.devtools.restart.exclude=application.properties 5 6 #通过触发器,去控制什么时候进行热加载部署新的文件 7 spring.devtools.restart.trigger-file=trigger.txt 8 9 server.port=8083 10 11 #文件上传路径配置 12 web.file.path=L:/images 13 14 #测试配置文件注入 15 test.name=www.xdclass.net 16 test.domain=springboot
控制台结果:
配置注入打印,文件路径为:L:/images
用户名:123
上传的文件名为:hongmi6.jpg
上传的后缀名为:.jpg
转换后的名称:ee4b60bd-35b4-4df6-bee6-ed62dd5e4a00.jpg
浏览器返回值:
{"code":0,"data":"ee4b60bd-35b4-4df6-bee6-ed62dd5e4a00.jpg","msg":null}
方式二:实体类配置文件
步骤:
1、添加 @Component 注解;
2、使用 @PropertySource 注解指定配置文件位置;
3、使用 @ConfigurationProperties 注解,设置相关属性;
4、必须 通过注入IOC对象Resource 进来 , 才能在类中使用获取的配置文件值。
@Autowired
private ServerSettings serverSettings;
例子:
@Configuration
@ConfigurationProperties(prefix="test")
@PropertySource(value="classpath:resource.properties")
public class ServerConstant {
代码示例:
ServerSettings.java:
1 package net.xdclass.demo.domain; 2 3 import org.springframework.boot.context.properties.ConfigurationProperties; 4 import org.springframework.context.annotation.PropertySource; 5 import org.springframework.stereotype.Component; 6 7 //服务器配置 8 @Component 9 @PropertySource({"classpath:application.properties"}) 10 //@ConfigurationProperties 11 //自动加入前缀,无需写入test. 12 @ConfigurationProperties(prefix="test") 13 //若不想使用prefix="test",前提是该类中定义的名称要与application.properties中定义的名称一致,即一一对应 14 public class ServerSettings { 15 16 //名称 17 //使用prefix="test"后,无需再使用Value注解 18 //@Value("${name}") 19 private String name; 20 21 //域名地址 22 //@Value("${domain}") 23 private String domain; 24 25 public String getName() { 26 return name; 27 } 28 29 public void setName(String name) { 30 this.name = name; 31 } 32 33 public String getDomain() { 34 return domain; 35 } 36 37 public void setDomain(String domain) { 38 this.domain = domain; 39 } 40 41 42 }
application.properties同上一个示例
GetController.java部分代码:
1 @Autowired 2 private ServerSettings serverSettings; 3 @GetMapping("/v1/test_properties") 4 public Object testProperties(){ 5 return serverSettings; 6 }
浏览器测试:
地址栏输入:http://localhost:8083/v1/test_properties
结果:{"name":"www.xdclass.net","domain":"springboot"}
常见问题:
1、配置文件注入失败,Could not resolve placeholder
解决:根据springboot启动流程,会有自动扫描包没有扫描到相关注解,
默认Spring框架实现会从声明@ComponentScan所在的类的package进行扫描,来自动注入,
因此启动类最好放在根路径下面,或者指定扫描包范围
spring-boot扫描启动类对应的目录和子目录
2、注入bean的方式,属性名称和配置文件里面的key一一对应,就不用加@Value 这个注解
如果不一样,就要加@value("${XXX}")
3、SpringBoot注解把配置文件自动映射到属性和实体类实战简介:讲解使用@value注解配置文件自动映射到属性和实体类1、配置文件加载方式一1、Controller上面配置 @PropertySource({"classpath:resource.properties"})2、增加属性 @Value("${test.name}") private String name;
方式二:实体类配置文件步骤:1、添加 @Component 注解;2、使用 @PropertySource 注解指定配置文件位置;3、使用 @ConfigurationProperties 注解,设置相关属性;
4、必须 通过注入IOC对象Resource 进来 , 才能在类中使用获取的配置文件值。@Autowired private ServerSettings serverSettings;
例子: @Configuration@ConfigurationProperties(prefix="test")@PropertySource(value="classpath:resource.properties")public class ServerConstant {
常见问题:1、配置文件注入失败,Could not resolve placeholder解决:根据springboot启动流程,会有自动扫描包没有扫描到相关注解, 默认Spring框架实现会从声明@ComponentScan所在的类的package进行扫描,来自动注入,因此启动类最好放在根路径下面,或者指定扫描包范围spring-boot扫描启动类对应的目录和子目录2、注入bean的方式,属性名称和配置文件里面的key一一对应,就用加@Value 这个注解如果不一样,就要加@value("${XXX}")
转载于:https://www.cnblogs.com/116970u/p/10221268.html
SpringBoot注解把配置文件自动映射到属性和实体类实战相关推荐
- Spring Boot 注解配置文件自动映射到属性和实体类
官网给出的配置文件大全: https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#comm ...
- SpringBoot 注解原理,自动装配原理,图文并茂,万字长文!
来源:cnblogs.com/jing99/p/11504113.html 首先,先看SpringBoot的主配置类: @SpringBootApplication public class Star ...
- SpringBoot项目中获取yml文件的属性时实体属性类出现Spring Boot Configuration Annotation Processor not found in classpath
1.SpringBoot项目的项目结构如下: 2.属性实体类 上面出现了Spring Boot Configuration Annotation Processor not found in clas ...
- generator代码自动生成工具(动态生成注释、类注解、方法注解等)适用于swagger等需要配置实体类的场景
generator代码生成器大家都不陌生,但是在实际的业务场景中,实体类只有属性.getter/setter方法不满足需求,还需要手动去添加需要的功能.比如项目使用swagger生成api文档时,需要 ...
- 自动生成WebForm中对实体类的编辑页面
版权所有:基础软件.作者邮箱:sun.j.l.studio@gmail.com.本文首发于 http://www.cnblogs.com/FoundationSoft.文章转载请保持此版权信息并注明出 ...
- **Mybatis怎么自动生成Mapper文件和实体类**
Mybatis怎么自动生成Sql Mapper文件和实体类 第一步:在resources包下建立generator.xml文件 generator.xml:文件内容 <?xml version= ...
- Java实现自动生成Mysql数据库表实体类
2019独角兽企业重金招聘Python工程师标准>>> 一个工具类就可以实现啦,直接看代码及注释,很方便理解,从一位博主那里拿到加上自己优化一部分的 注:侵删(忘了原博主信息) My ...
- springboot的yml配置文件绑定时必须和相应的类中的属性类型对应,不然启动报错
今天启动springboot应用时一开始一直是error,访问localhost也无法打开: 此时自己的Person.java: package boot.bean; import lombok.Da ...
- SpringBoot注解详解
一.简介 基于 SpringBoot 平台开发的项目数不胜数,与常规的基于Spring开发的项目最大的不同之处,SpringBoot 里面提供了大量的注解用于快速开发,而且非常简单,基本可以做到开箱即 ...
最新文章
- Gradle Tasks clear app:generateDebugSources,app:mockableAndroidJar app prepareDebugUnitTestDependenc
- 核弹级漏洞log4shell席卷全球!危及苹果腾讯百度网易,修改iPhone名称就可触发...
- 推荐系统笔记:Introduction
- Majority Element(169) Majority Element II(229)
- python矩阵操作_Python中的矩阵操作
- Python练习:快乐的数字
- Swift的一些问题
- HDU2547 无剑无我【水题】
- 一文读懂语音语义识别技术的现状与未来
- 【NOI OpenJudge】【1.4】编程基础之逻辑表达式与条件分支
- Ubuntu常用软件大全
- odb对象关系映射系统
- 如何查看电脑里的隐藏文件?
- 金融风控实战——信贷特征衍生与筛选(中国移动人群画像赛TOP1)
- csdn WLW 文件验证
- python读取svg文件_使用python创建SVG
- 第三章——虚拟存储器
- 去卢沟桥,看晓月还是数弹孔?
- 虚拟主机到底哪家比较好呢?
- 自动化测试之Python+selenium
热门文章
- android自定义涂鸦,Android Studio:小Demo-“涂鸦”
- 什么是中台,为什么要中台?一篇文章带你了解中台的概念!
- 阿里“小前台、大中台”的解读
- Python3网络爬虫开发实战(第二版)
- 优雅使用Jsdelivr/CDN加速博客访问速度
- IK 分词器空格支持
- Android 指纹识别(Touch ID)实例
- SAP管理软件系统框架合同业务的实现及相关注意事项解析
- 5.5.JMeter中调度器起作用(需要循环次数设置成永远)
- Linux下父进程子进程先后终止的不同处理