ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-07-14 21:00:08  阅读:166  来源: 互联网

标签:实体类 String 配置文件 Spring Value springframework import org


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

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")
    @ResponseBody
    public 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);
    }
    
}

 

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}")

 

标签:实体类,String,配置文件,Spring,Value,springframework,import,org
来源: https://www.cnblogs.com/jwen1994/p/11185514.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有