【spring boot】3.spring boot项目,绑定资源文件为bean并使用
发布日期:2021-09-08 22:55:04 浏览次数:15 分类:技术文章

本文共 2784 字,大约阅读时间需要 9 分钟。

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

 

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;    }}
View Code

 

 

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);    }}
View Code

 

 

4.运行结果:

 

转载于:https://www.cnblogs.com/sxdcgaq8080/p/7653543.html

转载地址:https://blog.csdn.net/weixin_34341229/article/details/93386105 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:如何向android studio中导入第三方类库
下一篇:ffmpeg-20160929-bin.7z

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年03月24日 18时12分44秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

知识地图 2019-04-26
罗马总会建成 2019-04-26
程序通过技术赚钱的八个途径 2019-04-26
我在爬坡阶段 2019-04-26
大疆机甲大师教育机器人Python开发:中文命名变量初尝试 2019-04-26
大疆机甲大师教育机器人Python开发:API中文化初尝试 2019-04-26
大疆机甲大师Python开发: 两只老虎 2019-04-26
大疆机甲大师教育机器人Python API中文化之一:枪亮枪暗 2019-04-26
大疆机甲大师教育机器人Python API中文化之二:LED闪烁 2019-04-26
大疆 RoboMaster 机甲大师官方刚刚开通”机甲小 S 实验室”知乎专栏 2019-04-26
大疆机甲大师教育机器人Python API中文化之三:底盘灯效 2019-04-26
大疆机甲大师教育机器人Python API中文化之四五:云台灯效,指定序号 2019-04-26
大疆机甲大师教育机器人Python API中文化之六:关灯 2019-04-26
“中文编程”知乎专栏两岁了——山雨欲来风满楼 2019-04-26
大疆机甲大师Python API之七:做个闹钟 2019-04-26
【意外走向】大疆机甲大师Python API之八:计时——为性能测试展开1000次循环 2019-04-26
RFC#2457——Rust 语言支持非 ASCII 码标识符在 GitHub 引发的激辩(一) 2019-04-26
RFC#2457——Rust 语言选择支持非 ASCII 码标识符在 GitHub 引发的激辩(二) 2019-04-26
”为什么有这么多人执着于中文编程?”回答两千赞留念及回应 2019-04-26
【家务】盘点小孩玩具零件缺失情况 2019-04-26