【spring boot】3.spring boot项目,绑定资源文件为bean并使用
整个例子的结构目录如下:

1.自定义一个资源文件

com.sxd.name = 申九日木
com.sxd.secret = ${random.value}
com.sxd.intValue = ${random.int}
com.sxd.uuid = ${random.uuid}
com.sxd.age= ${random.int(100)} com.sxd.resume = 简历:①姓名:${com.sxd.name} ②年龄:${com.sxd.age}
2.将资源文件中的属性绑定到一个bean上

package com.sxd.beans; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; /**
* User实体
* @Component 声明User实体为一个bean
* @PropertySource 声明对应绑定了test.properties文件 【应对ConfigurationProperties注解取消掉location的属性】
* @ConfigurationProperties 对应绑定前缀为com.sxd的属性名
*/
@Component
@PropertySource(value = "classpath:/test.properties")
@ConfigurationProperties(prefix = "com.sxd")
public class User {
private String name;
private Integer age; //资源文件中定义的是com.sxd.uuid而不是uu,这里的uu字段只是测试如果名称不对应,是否会赋值成功
private String uu;
private String resume; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public String getUu() {
return uu;
} public void setUu(String uu) {
this.uu = uu;
} public String getResume() {
return resume;
} public void setResume(String resume) {
this.resume = resume;
}
}
3.spring boot的主入口

package com.sxd.secondemo; import com.sxd.beans.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* spring boot 的主入口类
* @RestController = @ResponseBody + @Controller
* @SpringBootApplication spring boot的核心注解
* @EnableConfigurationProperties 激活绑定资源文件的Bean,例如这里的User.class或者更多
*/
@RestController
@SpringBootApplication
@EnableConfigurationProperties(User.class)
public class SecondemoApplication { /**
* @Autowired 自动注入,需要@EnableConfigurationProperties中声明已经激活的Bean才能自动注入成功
*/
@Autowired
User user; /**
* 请求地址为localhost:8080/即可访问到本方法
* @return
*/
@RequestMapping("/")
public String hello(){
/**
* idea中 System.out.println()快捷方式为sout,然后Alt+Enter才能出来
*/
System.out.println(user.getResume()); return "打印简历:"+user.getResume()+"\n"+"uu是否有值:"+user.getUu();
} public static void main(String[] args) {
SpringApplication.run(SecondemoApplication.class, args);
}
}
4.运行结果:

【spring boot】3.spring boot项目,绑定资源文件为bean并使用的更多相关文章
- 以Jar形式为Web项目提供资源文件
以Jar形式为Web项目提供资源文件 http://www.webjars.org/ Web前端使用了越来越多的JS或CSS如jQuery, Backbone.js 和Twitter Bootstra ...
- 在eclipse完成对Java_web项目里面资源文件的读取
Java_web项目的资源文件一般有两种: 一种是存放数据之间有联系的文件,使用xml文件 另一种是存放数据之间没有联系的文件,使用properties文件 这里我们对properties文件读写做示 ...
- Spring MVC程序中怎么得到静态资源文件css,js,图片文件的路径问题
问题描述 在用springmvc开发应用程序的时候.对于像我一样的初学者,而且还是自学的人,有一个很头疼的问题.那就是数据都已经查出来了,但是页面的样式仍然十分简陋,加载不了css.js,图片等资源文 ...
- Spring 加载类路径外的资源文件
原文:http://blog.csdn.net/gaofuqi/article/details/46417259 <bean id="propertyConfigurer" ...
- Eclipse 下如何引用另一个项目的资源文件
为什么要这么做?可参考:Eclipse 下如何引用另一个项目的Java文件 下面直接说下步骤:(项目A 引用 项目B的资源文件) 1.右键 项目A,点击菜单 Properties 2.在弹出的框中,点 ...
- eclipse导入maven项目,资源文件位置显示不正确
eclipse导入maven项目后,资源文件位置显示不正确,如下图所示 解决方法: 在resources上右键Build Path,选择Use as Source Folder即可正确显示资源文件
- c# 反射取其他项目的资源文件
反射获取其他项目里面的资源文件. dll或exe里面 try { System.Reflection.Assembly dll = System.Reflection.Assembly.LoadFil ...
- web项目获取资源文件
首页 博客 学院 CSDN学院 下载 论坛 APP CSDN 问答 商城 活动 VIP会员 专题 招聘 ITeye GitChat GitChat 图文课 写博客 消息 1 评论 关注 点赞 回答 系 ...
- Intellij IDEA项目添加资源文件
添加了一个资源文件,但读取的时候出错了 prop.load(Config.class.getResourceAsStream("/resources/dbconfig.properties& ...
随机推荐
- 预处理、const、static与sizeof-C++中const有什么作用(至少说出3个)
1:作用如下: (1)const用于定义常量:const定义的常量编译器可以对其进行数据静态类型安全检查. (2)const修饰函数形式的参数:当输入参数为用户自定义类型和抽象数据类型时,应该将“值传 ...
- Nginx之编写HTTP模块
1. 常用数据结构 1.1 ngx_str_t typedef struct { /* * 字符串的有效长度 */ size_t len; /* * 有效字符串的起始地址,该字符串通常并不以'\0'结 ...
- 状压dp之不相连块
传送门 一块田里草地格子不能相邻,问有几种方案. 预处理不相邻块 #include<iostream> #include<cstdio> #include<algorit ...
- 修改Visual Studio的默认模板
如果我在Visual Studio创建的项目中每次新建一个文件,自动生成注释或者是结构的话,那么就需要改下默认的模板了.下面以vs2013为例 我们添加的文件有很多种,这里就举例3种,CSharp类文 ...
- Linux下 安装jdk8
一.文件准备 1.1 文件名称 jdk-8u121-linux-x64.tar.gz 1.2 下载地址 http://www.oracle.com/technetwork/java/javase/do ...
- 搞清clientHeight、offsetHeight、scrollHeight、offsetTop、scrollTop
每个HTML元素都具有clientHeight offsetHeight scrollHeight offsetTop scrollTop 这5个和元素高度.滚动.位置相关的属性,单凭单词很难搞清楚分 ...
- Django运行项目时候出现DisallowedHost at / Invalid HTTP_HOST header:
web端错误现象: DisallowedHost at / Invalid HTTP_HOST header: 'ip:8000'. You may need to add u'ip' to ALLO ...
- 关于java面试题
java的优点: Java是一种跨平台,适合于分布式计算环境的面向对象编程语言. 具体来说,它具有如下特性: 简单性.面向对象.分布式.解释型.可靠.安全.平台无关.可移植.高性能.多线程.动态性等.
- Hadoop HDFS命令学习笔记
cat Usage: hadoop fs -cat [-ignoreCrc] URI [URI ...] Copies source paths to stdout. Options The -ign ...
- 用事件队列来处理pixi中的场景元素入场
在pixi中,添加一个精灵元素,你可能需要,先将贴图load进来,然后才能添加到场景中去,所以一般会这么操作 Loader.add("tree","static/imag ...