SpringBoot注解把配置文件自动映射到属性和实体类实战
SpringBoot注解把配置文件自动映射到属性和实体类实战
简介:讲解使用@value注解配置文件自动映射到属性和实体类
1、配置文件加载
方式一
1、Controller上面配置
@PropertySource({"classpath:resource.properties"})
2、增加属性
@Value("${test.name}")
private String name;
文件上传修改示例:
FileController.java:
package net.xdclass.demo.controller; import java.io.File;
import java.io.IOException;
import java.util.UUID; import javax.servlet.http.HttpServletRequest; import net.xdclass.demo.domain.JsonData; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile; /**
* 功能描述:文件测试
*
* <p> 创建时间:Apr 22, 2018 11:22:29 PM </p>
*/
@Controller
@PropertySource({"classpath:application.properties"})
public class FileController { @RequestMapping(value = "/api/v1/gopage")
public Object index() {
return "index";
} //private static final String filePath = "L:/Workspaces/Eclipse_Neon/txkt/SpringBootClass/xdclass_springboot/src/main/resources/static/images/";//末尾需要加/,这样才能写进来
//private static final String filePath = "L:/images/";//末尾需要加/,这样才能写进来 @Value("${web.file.path}")
private String filePath; @RequestMapping(value = "upload")
@ResponseBody
public JsonData upload(@RequestParam("head_img") MultipartFile file, HttpServletRequest request) { // file.isEmpty(); 判断图片是否为空
// file.getSize(); 图片大小进行判断 System.out.println("配置注入打印,文件路径为:" + filePath); String name = request.getParameter("name");
System.out.println("用户名:" + name); // 获取文件名
String fileName = file.getOriginalFilename();
System.out.println("上传的文件名为:" + fileName); // 获取文件的后缀名,比如图片的jpeg,png
String 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);
} }
application.properties:
web.images-path=L:/images
spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/test/,file:${web.upload-path}
#指定某些文件不进行监听,即不会进行热加载devtool(重启后不会监听下面这个文件)
#spring.devtools.restart.exclude=application.properties #通过触发器,去控制什么时候进行热加载部署新的文件
spring.devtools.restart.trigger-file=trigger.txt server.port=8083 #文件上传路径配置
web.file.path=L:/images #测试配置文件注入
test.name=www.xdclass.net
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:
package net.xdclass.demo.domain; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; //服务器配置
@Component
@PropertySource({"classpath:application.properties"})
//@ConfigurationProperties
//自动加入前缀,无需写入test.
@ConfigurationProperties(prefix="test")
//若不想使用prefix="test",前提是该类中定义的名称要与application.properties中定义的名称一致,即一一对应
public class ServerSettings { //名称
//使用prefix="test"后,无需再使用Value注解
//@Value("${name}")
private String name; //域名地址
//@Value("${domain}")
private String domain; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getDomain() {
return domain;
} public void setDomain(String domain) {
this.domain = domain;
} }
application.properties同上一个示例
GetController.java部分代码:
@Autowired
private ServerSettings serverSettings;
@GetMapping("/v1/test_properties")
public Object testProperties(){
return serverSettings;
}
浏览器测试:
地址栏输入: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}")
SpringBoot注解把配置文件自动映射到属性和实体类实战的更多相关文章
- 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-7.接口配置文件自动映射到属性和实体类配置
笔记 7.接口配置文件自动映射到属性和实体类配置 简介:使用@value注解配置文件自动映射到属性和实体类 1.添加 @Component或者Configuration 注解: ...
- SpringBoot配置文件自动映射到属性和实体类(8)
一.配置文件加载 1.Controller中配置并指向文件 @Controller @PropertySource(value = { "application.properties&quo ...
- SpringBoot------注解把配置文件自动映射到属性和实体类
1.映射到属性 package top.ytheng.demo.controller; import org.springframework.beans.factory.annotation.Valu ...
- 利用在线工具根据JSon数据自动生成对应的Java实体类
如果你希望根据JSon数据自动生成对应的Java实体类,并且希望能进行变量的重命名,那么“JSON To Java”一定适合你.(下面的地址需要FQ) https://jsontojava.appsp ...
- T4 模板自动生成带注释的实体类文件
T4 模板自动生成带注释的实体类文件 - 只需要一个 SqlSugar.dll 生成实体就是这么简单,只要建一个T4文件和 文件夹里面放一个DLL. 使用T4模板教程 步骤1 创建T4模板 如果你没有 ...
- EF自动创建数据库步骤之一(实体类写法)
文章演示使用EF自动创建数据库第一个步骤创建实体类. 一.创建表映射实体类 using System; using System.Collections.Generic; using System.C ...
- springboot自定义属性文件与bean映射注入属性值
主要有几点: 一.导入依赖 springboot的包和: <dependency> <groupId>org.springframework.boot</groupId& ...
- hibernate 数据库列别名自动映射pojo属性名
package com.pccw.business.fcm.common.hibernate; import java.lang.reflect.Field; import java.math.Big ...
- T4 模板自动生成带注释的实体类文件 - 只需要一个 SqlSugar.dll
生成实体就是这么简单,只要建一个T4文件和 文件夹里面放一个DLL. 使用T4模板教程 步骤1 创建T4模板 ,一定要自已新建,把T4代码复制进去,好多人因为用我现成的T4报错(原因不明) 点击添加文 ...
随机推荐
- Codeforces Round #337 (Div. 2) B. Vika and Squares
B. Vika and Squares time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- BZOJ 3745: [Coci2015]Norma(分治)
题意 给定一个正整数序列 \(a_1, a_2, \cdots, a_n\) ,求 \[ \sum_{i=1}^{n} \sum_{j=i}^{n} (j - i + 1) \min(a_i,a_{i ...
- 01 自学Aruba之功率单位和相对单位
点击返回:自学Aruba之路 01 自学Aruba之功率单位和相对单位 功率单位是用来测量传输振幅和接受振幅的大小,功率单位测量的是绝对功率 相对单位是用来计算增加电缆或天线后的损耗和增益的大小,相对 ...
- [luogu2280][bzoj1218][HNOI2003]激光炸弹
题目描述 一种新型的激光炸弹,可以摧毁一个边长为R的正方形内的所有的目标.现在地图上有n(N<=10000)个目标,用整数Xi,Yi(其值在[0,5000])表示目标在地图上的位置,每个目标都有 ...
- 【洛谷P1638】逛画展
题目大意:给定 N 个数字组成的序列,求刚好拥有所有 M 种数字的最短区间. 题解:双指针算法是一种对于暴力的优化算法,对于这道题来说,一个显然的暴力是:对于序列中每一个位置 pos,计算出这个位置右 ...
- kafka安装以及入门
一.安装 下载最新版kafka,Apache Kafka,然后上传到Linux,我这里有三台机器,192.168.127.129,130,131 . 进入上传目录,解压到/usr/local目录下 - ...
- linu查看当前目录下文件大小
查看当前目录下文件大小 [root@izm5e33l0ge76tvdj3qtnfz var]# du -sh * .0K adm 190M cache .0K crash 24K db .0K emp ...
- mac 本上对 rar 压缩包解压
以前从晚上各种找软件对 xxx.rar 压缩包文件进行解压,也确实找到过那么几个,要不是不好用就是解压完有乱码,很是头疼. 这次又遇到这样的问题,于是下定决心将这个问题彻底解决好,经过验证,总结一下最 ...
- Overload和Override的区别 C++ Java
Overload:顾名思义,就是Over(重新)——load(加载),所以中文名称是重载. 它可以表现类的多态性,可以是函数里面可以有相同的函数名但是参数名.返回值.类型不能相同: 或者说可以改变参数 ...
- Linux记录-shell一行代码杀死进程(收藏)
ps -ef |grep hello |awk '{print $2}'|xargs kill -9