SpringBoot_自定义配置属性

博客介绍了Spring Boot中两种配置注入方式。一是使用@ConfigurationProperties,可将application.properties配置数据注入Bean,prefix属性指定配置前缀,还可处理YAML文件及遵循宽松绑定规则;二是使用@Value注解,能将配置值注入指定变量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@ConfigurationProperties

在aplication.properties 中添加如下一段配置:

mysql.jdbcName=com.mysql.jdbc.Driver

mysql.dbUrl=jdbc:mysql://localhost:3306/db_boot

mysql.userName=root

mysql.password=123456

 

将这一段配置数据注入如下Bean中:

/**

 * Mysql属性配置文件

 * @author Administrator

 *

 */

@Component

@ConfigurationProperties(prefix="mysql")

public class MysqlProperties {

    private String jdbcName;

    private String dbUrl;   

    private String userName;

    private String password;

    //省略getter/setter

}

 

1.@ConfigurationProperties中的prefix属性描述了要加载的配置文件的前缀。

2.如果配直文件是一个YAML文件,那么可以将数据注入一个集合中。

3. Spring Boot采用了一种宽松的规则来进行属性绑定,如果Bean中的属性名为authorName,那么配直文件中的属性可以是book.author-name、book.author-name、book.authorName。

 

 

 

@Value

在aplication.properties 中添加如下一段配置:

helloWorld=spring boot

 

使用@Value注解:

@RestController

public class HelloWorldController {

    @Value("${helloWorld}")

    private String helloWorld;

}

 

转载于:https://www.cnblogs.com/zhiboluo/p/10813130.html

### 定义并读取自定义配置类 为了在 Spring Boot 中读取自定义配置类,通常会采用 `@ConfigurationProperties` 注解来简化这一过程。下面是一个完整的例子展示如何创建和使用自定义配置。 #### 创建自定义配置类 首先,在应用程序中定义一个 Java 类用于保存配置数据,并标注此 Java Bean 使用 `@Component` 和 `@ConfigurationProperties(prefix="myapp")` 来指示这是一个配置属性bean以及其前缀[^2]: ```java import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "myapp") public class MyCustomConfig { private String name; private int port; // Getters and Setters are required by @ConfigurationProperties public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } } ``` #### 配置文件设置 接着,在项目的资源目录下的 application.yml 或者 application.properties 文件里添加对应的键值对作为配置项[^4]: 对于application.yml: ```yaml myapp: name: MyAppName port: 8090 ``` 对于application.properties: ```properties myapp.name=MyAppName myapp.port=8090 ``` #### 将配置注入到服务层或其他组件 为了让其他部分能够访问这个配置对象,可以在任何需要的地方通过 `@Autowired` 进行自动装配[^5]: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ConfigController { private final MyCustomConfig myCustomConfig; @Autowired public ConfigController(MyCustomConfig myCustomConfig){ this.myCustomConfig = myCustomConfig; } @GetMapping("/config") public String getConfig(){ return "Application Name is "+myCustomConfig.getName()+" running on Port:"+myCustomConfig.getPort(); } } ``` 这样就完成了整个流程——从定义配置类到最后将其应用于具体的服务逻辑之中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值