整个例子的结构目录如下:

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并使用的更多相关文章

  1. 以Jar形式为Web项目提供资源文件

    以Jar形式为Web项目提供资源文件 http://www.webjars.org/ Web前端使用了越来越多的JS或CSS如jQuery, Backbone.js 和Twitter Bootstra ...

  2. 在eclipse完成对Java_web项目里面资源文件的读取

    Java_web项目的资源文件一般有两种: 一种是存放数据之间有联系的文件,使用xml文件 另一种是存放数据之间没有联系的文件,使用properties文件 这里我们对properties文件读写做示 ...

  3. Spring MVC程序中怎么得到静态资源文件css,js,图片文件的路径问题

    问题描述 在用springmvc开发应用程序的时候.对于像我一样的初学者,而且还是自学的人,有一个很头疼的问题.那就是数据都已经查出来了,但是页面的样式仍然十分简陋,加载不了css.js,图片等资源文 ...

  4. Spring 加载类路径外的资源文件

    原文:http://blog.csdn.net/gaofuqi/article/details/46417259 <bean id="propertyConfigurer" ...

  5. Eclipse 下如何引用另一个项目的资源文件

    为什么要这么做?可参考:Eclipse 下如何引用另一个项目的Java文件 下面直接说下步骤:(项目A 引用 项目B的资源文件) 1.右键 项目A,点击菜单 Properties 2.在弹出的框中,点 ...

  6. eclipse导入maven项目,资源文件位置显示不正确

    eclipse导入maven项目后,资源文件位置显示不正确,如下图所示 解决方法: 在resources上右键Build Path,选择Use as Source Folder即可正确显示资源文件

  7. c# 反射取其他项目的资源文件

    反射获取其他项目里面的资源文件. dll或exe里面 try { System.Reflection.Assembly dll = System.Reflection.Assembly.LoadFil ...

  8. web项目获取资源文件

    首页 博客 学院 CSDN学院 下载 论坛 APP CSDN 问答 商城 活动 VIP会员 专题 招聘 ITeye GitChat GitChat 图文课 写博客 消息 1 评论 关注 点赞 回答 系 ...

  9. Intellij IDEA项目添加资源文件

    添加了一个资源文件,但读取的时候出错了 prop.load(Config.class.getResourceAsStream("/resources/dbconfig.properties& ...

随机推荐

  1. Django环境搭建win(二)

    Django 1.11.x 支持 Python 2.7, 3.4, 3.5 和 3.6,17年4月4号 已经发布(长期支持版本 LTS) win7下: 一.使用pip安装 1.首先要安装pip 2.p ...

  2. Python中Bool为False的情况

    在python中,以下数值会被认为是False: 为0的数字,包括0,0.0空字符串,包括'', ""表示空值的None空集合,包括(),[],{}其他的值都认为是True. No ...

  3. 第11组 Alpha冲刺(2/6)

    第11组 Alpha冲刺(2/6)   队名 不知道叫什么团队 组长博客 https://www.cnblogs.com/xxylac/p/11860949.html 作业博客 https://edu ...

  4. cache magic对pms模块的cache访问模式分析结果

    其中43.184的命中率只有不到70%,是要分析的对象:3.189是命中率98%左右,是做参考的对象. 基本统计:可以知道43.184的update和delete操作占总操作的比例要比3.189打不少 ...

  5. C++继承中的属性

    class A { public: int a; A() { } void print() { cout<<a; } }; class B:public A { public: int a ...

  6. 小D课堂 - 新版本微服务springcloud+Docker教程_4-06 Feign核心源码解读和服务调用方式ribbon和Feign选择

    笔记 6.Feign核心源码解读和服务调用方式ribbon和Feign选择         简介: 讲解Feign核心源码解读和 服务间的调用方式ribbon.feign选择             ...

  7. requestLibrary API

    requestLibrary API Keyword Arguments Documentation Create Ntlm Session alias, url, auth, headers={}, ...

  8. 1. hadoop使用启动命令时报错之分析解决

    今天在学习hadoop启动命令的时候,先jps看了下,发现namenode.datanode都开着,所以想要先停止这些服务,结果输入命令后报错:“WARN util.NativeCodeLoader: ...

  9. python之selenium三种等待方法

    前提: 我们在做Web自动化时,有的时候要等待元素加载出来,才能操作,不然会报错 1.强制等待 2.隐式等待 3.显示等待 内容: 一,强制等待 这个比较简单,就是利用time模块的sleep的方法来 ...

  10. iscsi-文件类型

    iSCSI简介(Internet SCSI): iSCSI 小型计算机系统接口,IBM公司研发,用于在IP网络上运行SCSI协议:解决了 SCSI需要直连存储设备的局限性:可以不停机扩展存储容量,iS ...