springboot实体类映射配置文件_Spring Boot 注解配置文件自动映射到属性和实体类...

本文介绍了如何在Spring Boot中将配置文件映射到属性和实体类。首先展示了如何通过@PropertySource和@Value注解将配置文件中的属性注入到Controller,然后详细说明了实体类的配置过程,包括使用@Component、@PropertySource和@ConfigurationProperties注解。还提到了@ConfigurationProperties可以设置前缀,并给出了不同情况下的使用示例。最后,文章列举了解决配置文件注入失败的常见问题及解决方案。
摘要由CSDN通过智能技术生成

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

一、配置文件自动映射到属性

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

步骤2:属性上增加注解

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

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

注意加粗的部分

@Controller

@PropertySource({"classpath:application.properties"})public classFileController {

@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,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);

}

}

二、实体类配置文件

步骤1:添加 @Component 或者 @Configuration 注解(为了让 SpringBoot 扫描成一个 Bean)

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

步骤3:使用 @ConfigurationProperties 注解,设置相关属性。注意:这个注解可以设置前缀,但不是必须使用

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

类文件如下:

importorg.springframework.beans.factory.annotation.Value;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.context.annotation.PropertySource;importorg.springframework.stereotype.Component;//服务器配置

@Component

@PropertySource({"classpath:application.properties"})

@ConfigurationPropertiespublic classServerSettings {

@Value("${test.appname}")privateString name;

@Value("${test.domain}")privateString domain;//省略getter、setter方法

}

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

importorg.springframework.beans.factory.annotation.Value;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.context.annotation.PropertySource;importorg.springframework.stereotype.Component;//服务器配置

@Component

@PropertySource({"classpath:application.properties"})

@ConfigurationProperties(prefix="test")//设置前缀public classServerSettings {

@Value("${appname}")privateString name;

@Value("${domain}")privateString domain;//省略getter、setter方法

}

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

类文件如下:

importorg.springframework.beans.factory.annotation.Value;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.context.annotation.PropertySource;importorg.springframework.stereotype.Component;//服务器配置

@Component

@PropertySource({"classpath:application.properties"})

@ConfigurationPropertiespublic classServerSettings {privateString name;privateString domain;//省略getter、setter方法

}

获取这个对象信息

@Autowired//必须通过IOC依赖注入,才能获取这个对象的值

privateServerSettings serverSettings;

@GetMapping("/v1/test_properties")publicObject testPeroperties(){returnserverSettings;

}

常见问题:

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值