【spring boot】3.spring boot项目,绑定资源文件为bean并使用

简介: 整个例子的结构目录如下: 1.自定义一个资源文件com.sxd.name = 申九日木com.sxd.secret = ${random.value}com.sxd.intValue = ${random.

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

 

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.运行结果:

 

相关文章
|
6天前
|
JavaScript 前端开发 Java
解决跨域问题大集合:vue-cli项目 和 java/springboot(6种方式) 两端解决(完美解决)
这篇文章详细介绍了如何在前端Vue项目和后端Spring Boot项目中通过多种方式解决跨域问题。
150 1
解决跨域问题大集合:vue-cli项目 和 java/springboot(6种方式) 两端解决(完美解决)
|
6天前
|
Java 测试技术 开发者
springboot学习四:Spring Boot profile多环境配置、devtools热部署
这篇文章主要介绍了如何在Spring Boot中进行多环境配置以及如何整合DevTools实现热部署,以提高开发效率。
24 2
|
6天前
|
前端开发 Java 程序员
springboot 学习十五:Spring Boot 优雅的集成Swagger2、Knife4j
这篇文章是关于如何在Spring Boot项目中集成Swagger2和Knife4j来生成和美化API接口文档的详细教程。
20 1
|
6天前
|
Java API Spring
springboot学习七:Spring Boot2.x 拦截器基础入门&实战项目场景实现
这篇文章是关于Spring Boot 2.x中拦截器的入门教程和实战项目场景实现的详细指南。
10 0
springboot学习七:Spring Boot2.x 拦截器基础入门&实战项目场景实现
|
6天前
|
Java API Spring
springboot学习六:Spring Boot2.x 过滤器基础入门&实战项目场景实现
这篇文章是关于Spring Boot 2.x中过滤器的基础知识和实战项目应用的教程。
11 0
springboot学习六:Spring Boot2.x 过滤器基础入门&实战项目场景实现
|
6天前
|
Java 测试技术 Spring
springboot学习三:Spring Boot 配置文件语法、静态工具类读取配置文件、静态工具类读取配置文件
这篇文章介绍了Spring Boot中配置文件的语法、如何读取配置文件以及如何通过静态工具类读取配置文件。
14 0
springboot学习三:Spring Boot 配置文件语法、静态工具类读取配置文件、静态工具类读取配置文件
|
6天前
|
Java Spring
springboot 学习十一:Spring Boot 优雅的集成 Lombok
这篇文章是关于如何在Spring Boot项目中集成Lombok,以简化JavaBean的编写,避免冗余代码,并提供了相关的配置步骤和常用注解的介绍。
30 0
|
6天前
|
监控 Java Maven
springboot学习二:springboot 初创建 web 项目、修改banner、热部署插件、切换运行环境、springboot参数配置,打包项目并测试成功
这篇文章介绍了如何快速创建Spring Boot项目,包括项目的初始化、结构、打包部署、修改启动Banner、热部署、环境切换和参数配置等基础操作。
37 0
|
27天前
|
SQL 监控 druid
springboot-druid数据源的配置方式及配置后台监控-自定义和导入stater(推荐-简单方便使用)两种方式配置druid数据源
这篇文章介绍了如何在Spring Boot项目中配置和监控Druid数据源,包括自定义配置和使用Spring Boot Starter两种方法。
|
8天前
|
人工智能 自然语言处理 前端开发
SpringBoot + 通义千问 + 自定义React组件:支持EventStream数据解析的技术实践
【10月更文挑战第7天】在现代Web开发中,集成多种技术栈以实现复杂的功能需求已成为常态。本文将详细介绍如何使用SpringBoot作为后端框架,结合阿里巴巴的通义千问(一个强大的自然语言处理服务),并通过自定义React组件来支持服务器发送事件(SSE, Server-Sent Events)的EventStream数据解析。这一组合不仅能够实现高效的实时通信,还能利用AI技术提升用户体验。
43 2