spring boot——读取应用配置——方式三——@ConfigurationProperties

文章展示了如何在SpringBoot应用中创建一个配置文件映射的Pojo类,使用@ConfigurationProperties注解绑定配置,并通过@Component注解使其成为可注入的组件。接着,文章演示了一个简单的控制器测试,该测试访问了配置属性并返回其内容,体现了Spring的依赖注入机制在RESTfulAPI中的应用。

 

=====================================================================================

配置文件:

创建映射文件:

package org.example.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component           //使用Component注解,声明一个组件,被控制器依赖注入
@ConfigurationProperties(prefix = "obj")                //obj为配置文件中key的前缀
public class StudentProperties
{
    private String sname;
    private int sage;

    public String getSname()
    {

        return sname;
    }

    public void setSname(String sname)
    {

        this.sname = sname;
    }

    public int getSage()
    {

        return sage;
    }

    public void setSage(int sage)
    {

        this.sage = sage;
    }

    @Override
    public String toString()
    {

        return "StudentProperties [sname=" + sname + ", sage=" + sage + "]";
    }
}

测试:

package org.example.controller;

import org.example.pojo.StudentProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class ConfigurationPropertiesController
{

    @Autowired
    StudentProperties studentProperties;


    @RequestMapping("/testConfigurationProperties")
    public String  testConfigurationProperties()
    {

        return studentProperties.toString();
    }

}

============================================================================

 

 

### 使用 `@ConfigurationProperties` 进行 MySQL 数据源配置 #### 创建 DataSource Properties 类 为了使用 `@ConfigurationProperties` 来管理 Spring Boot 应用程序的数据源配置,可以创建一个专门用于存储这些属性的 Java 类。此类会自动读取并映射来自 `application.properties` 或 `application.yml` 文件中的相应键值。 ```java import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "spring.datasource") public class DataSourceProperties { private String url; private String username; private String password; private String driverClassName; // Getters and Setters public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getDriverClassName() { return driverClassName; } public void setDriverClassName(String driverClassName) { this.driverClassName = driverClassName; } } ``` 此代码片段展示了如何通过指定前缀来匹配配置文件内的条目,并将其注入到相应的字段中[^1]。 #### 配置 application.yml 文件 对于基于 YAML 的配置方式,在 `application.yml` 中设置数据源参数如下所示: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai username: myuser password: mypassword driver-class-name: com.mysql.cj.jdbc.Driver ``` 上述配置指定了连接数据库所需的 URL、用户名、密码以及驱动类名称等信息[^4]。 #### 启用 ConfigurationProperties 支持 为了让应用程序能够识别带有 `@ConfigurationProperties` 注解的组件,还需要确保项目已经启用了该功能。这可以通过在主启动类或其他配置类上添加 `@EnableConfigurationProperties(DataSourceProperties.class)` 实现[^2]。 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; @SpringBootApplication @EnableConfigurationProperties({DataSourceProperties.class}) public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } ``` 这样就完成了整个流程——从定义属性类到实际加载外部化配置的过程[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值