1.映射到属性

package top.ytheng.demo.controller;

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.ResponseBody; @Controller
@RequestMapping("/file")
@PropertySource({"classpath:application.properties"})
public class FileController {
@Value("${web.file.path}")
private String filePath; @RequestMapping("/testpath")
@ResponseBody
private Object testPath() {
return filePath;
} }

2.映射到类

(1)添加类

package top.ytheng.demo.entity;

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
//使用该注解时,表示添加前缀,得把@Value注解去掉
//@ConfigurationProperties(prefix="serversettings")
@PropertySource({"classpath:application.properties"})
public class ServerSettings {
@Value("${serversettings.name}")
private String name;
@Value("${serversettings.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;
} }

(2)添加控制器

package top.ytheng.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import top.ytheng.demo.entity.ServerSettings; @RestController
public class UserController {
@Autowired
private ServerSettings serverSettings; @GetMapping("/testproperties")
public Object testProperties() {
return serverSettings;
}
}

3.添加application.properties配置文件

#自定义文件上传路径
web.file.path=C:\\Users\\chenchao\\eclipse-workspace\\springboot-demo\\src\\main\\resources\\static\\images\\ #端口号
server.port=8090 #测试实体类配置
serversettings.name=www.spring.io
serversettings.domain=springboot

4.添加启动类

package top.ytheng.demo;

import javax.servlet.MultipartConfigElement;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.MultipartConfigFactory; @SpringBootApplication //等于下面3个
//@SpringBootConfiguration
//@EnableAutoConfiguration
//@ComponentScan
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} //文件大小配置
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
//单个文件最大
factory.setMaxFileSize("10240KB");
//设置总上传数据总大小
factory.setMaxRequestSize("102400KB");
return factory.createMultipartConfig();
}
}

5.测试路径

http://localhost:8090/file/testpath
http://localhost:8090/testproperties

SpringBoot------注解把配置文件自动映射到属性和实体类的更多相关文章

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

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

  2. 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-7.接口配置文件自动映射到属性和实体类配置

    笔记 7.接口配置文件自动映射到属性和实体类配置     简介:使用@value注解配置文件自动映射到属性和实体类 1.添加 @Component或者Configuration 注解:        ...

  3. SpringBoot配置文件自动映射到属性和实体类(8)

    一.配置文件加载 1.Controller中配置并指向文件 @Controller @PropertySource(value = { "application.properties&quo ...

  4. 利用在线工具根据JSon数据自动生成对应的Java实体类

    如果你希望根据JSon数据自动生成对应的Java实体类,并且希望能进行变量的重命名,那么“JSON To Java”一定适合你.(下面的地址需要FQ) https://jsontojava.appsp ...

  5. T4 模板自动生成带注释的实体类文件

    T4 模板自动生成带注释的实体类文件 - 只需要一个 SqlSugar.dll 生成实体就是这么简单,只要建一个T4文件和 文件夹里面放一个DLL. 使用T4模板教程 步骤1 创建T4模板 如果你没有 ...

  6. EF自动创建数据库步骤之一(实体类写法)

    文章演示使用EF自动创建数据库第一个步骤创建实体类. 一.创建表映射实体类 using System; using System.Collections.Generic; using System.C ...

  7. springboot自定义属性文件与bean映射注入属性值

    主要有几点: 一.导入依赖 springboot的包和: <dependency> <groupId>org.springframework.boot</groupId& ...

  8. hibernate 数据库列别名自动映射pojo属性名

    package com.pccw.business.fcm.common.hibernate; import java.lang.reflect.Field; import java.math.Big ...

  9. T4 模板自动生成带注释的实体类文件 - 只需要一个 SqlSugar.dll

    生成实体就是这么简单,只要建一个T4文件和 文件夹里面放一个DLL. 使用T4模板教程 步骤1 创建T4模板 ,一定要自已新建,把T4代码复制进去,好多人因为用我现成的T4报错(原因不明) 点击添加文 ...

随机推荐

  1. 浏览器自己主动填表安全漏洞:查看浏览器保存的password

    我通常会使用浏览器保存自己的帐号和password,下次登录就无需又一次输入,很方便.而像傲游这种浏览器还提供了自己主动同步功能,让我一个傲游帐号.就带着互联网上全部帐号password去旅行. 昨天 ...

  2. Entity Framework应用:使用EF的DataBase First模式实现数据库的增删改查

    在上一篇文章中讲解了如何生成EF的DBFirst模式,接下来讲解如何使用DBFirst模式实现数据库数据的增删改查 一.新增数据 新增一个Student,代码如下: static void Add() ...

  3. IDEA 修改文件编码

    Intellij Idea 修改 properties 文件编码 现象:idea 默认的properties文件是GBK,当有中文时,不同的客户端配置的编码不同时,可能产生中文乱码. 解决:修改pro ...

  4. butterknife 配置了点击和绑定无效

    配置了butterknife 没有报错,但是控件绑定和点击均无效. 问题原因: butterknife配置不完善问题导致. 解决办法: 找到app的module的build.gradle depend ...

  5. 如何利用jsp实现防盗链功能

    index.jsp ----------------------------- Place your content here here is index jsp get header info a. ...

  6. [hadoop读书笔记]译者序

    一.并行数据库系统 新一代高性能的数据库系统,是在MPP和集群并行计算环境的基础上建立的数据库系统. MPP:大规模并行处理计算机:Massive Parallel Processor.指的是一种处理 ...

  7. nano 命令 linux

    用途说明 nano是一个字符终端的文本编辑器,有点像DOS下的editor程序.它比vi/vim要简单得多,比较适合Linux初学者使用.某些Linux发行版的默认编辑器就是nano.(nano - ...

  8. ssh 中 远程文件传输

    scp 命令是 SSH 中最方便有用的命令了,试想,在两台服务器之间直接传送文件,仅仅用 scp 一个命令就完全解决了. 你可以在一台服务器上 以 root 身份运行 #scp servername: ...

  9. Java Set List Map

    对JAVA的集合的理解是想对于数组 数组是大小固定的,并且同一个数组只能存放类型一样的数据(基本类型/引用类型) JAVA集合可以存储和操作数目不固定的一组数据. 所有的JAVA集合都位于 java. ...

  10. hostapd作为radius服务器

    使用hostapd作为radius服务器,用于企业wifi加密认证. 参考链接: http://www.cnblogs.com/claruarius/p/5902141.html 去网上下载hosta ...