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. sbt使用详解

    sbt使用详解 安装: 我的系统为centos6.5故采用官方提供的在线安装: curl https://bintray.com/sbt/rpm/rpm > bintray-sbt-rpm.re ...

  2. drupal pathauto的配置

  3. shell脚本路径写法的注意点

    1.在脚本中先cd到指定路径,再操作文件 2.如果不满足第一条,文件请写绝对路径 要不有可能会出现自己手动执行脚本没有问题,但是加入crontab却发现执行失败的情况. --------------- ...

  4. PCL超体聚类

    超体聚类是一种图像的分割方法. 超体(supervoxel)是一种集合,集合的元素是“体”.与体素滤波器中的体类似,其本质是一个个的小方块.与大部分的分割手段不同,超体聚 类的目的并不是分割出某种特定 ...

  5. combobox的值从主页面传递到子页面

    1.主页面传递参数 url: '@Url.Action("WaterLoad")' + '?year=' + year + '&fanwei=' + fanwei, 2.控 ...

  6. VMWare Workstation虚拟机网卡工作模式及配置方法

    打开VMware→虚拟机→设置.如下图 一. 桥接模式(Bridge) 可将虚拟系统IP与本地系统设在同一网段,此时虚拟机相当于一台网络中与本机公用一个HUB的独立设备,网络中其他机器与虚拟机器.本地 ...

  7. Namenode HA原理详解(脑裂)

    转自:http://blog.csdn.net/tantexian/article/details/40109331 Namenode HA原理详解 社区hadoop2.2.0 release版本开始 ...

  8. python3 解决zip解压中文乱码问题,亲测可用, ZipFile

    中文乱码是个很头疼的问题,找了好久都没用找到解决办法 后来也忘了在哪儿找到的解决办法, 很久以前了,但不可行, 解决了思路 在源码里面想要修改内容 if flags & 0x800: # UT ...

  9. 技能UP:SAP CO掌上配置手册

    No. 配置对象 事务代码 路径 1 Enterprise Structure and General Controlling configration       Maintain EC-PCA : ...

  10. html5视频播放解决方案

    关键词:html5  nativeapp webapp mp4 H.264 html5没学习之前总觉的很神秘.近期通过学习和研究html5有点成果,特总结分享给大家.众所周知应用开发分两种:一是原生的 ...