泰康人寿保险官方网站专题学习网站模板
news/
2025/10/2 22:52:22/
文章来源:
泰康人寿保险官方网站,专题学习网站模板,网站对接微信接口,qq推广boot读取配置文件1. yml配置文件2. 绑定方式13. 绑定方式23.1 依赖.3.2 boot 中提供的属性装配功能。1. 在某些业务中#xff0c;需要将类的配置写到配置文件中, 不在代码中写死配置。
2. 所以需要读取配置文件(yaml, properties) 读取到Java Bean中。
3. 本文以oss对象存储配…
boot读取配置文件1. yml配置文件2. 绑定方式13. 绑定方式23.1 依赖.3.2 boot 中提供的属性装配功能。1. 在某些业务中需要将类的配置写到配置文件中, 不在代码中写死配置。
2. 所以需要读取配置文件(yaml, properties) 读取到Java Bean中。
3. 本文以oss对象存储配置为demo。两种方式初始化配置文件。1. yml配置文件
server:port: 8888spring:application:name: hello-world# oss对象存储配置
oss-constant:endpoint: secretaccess-key: keysecret-key: keybucket: bucket2. 绑定方式1
使用Value注解, 注入配置文件的值。 好处能够选择性的配置。能够类名.属性。 坏处写死了Value(value“xxx”) 前缀项目打包后了不方便修改。
package top.bitqian.hello.constant;import lombok.Data;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;/*** 加载oss对象存储配置* author echo lovely* date 2021/3/20 09:30*/Data
Component
public class OssLoadConstant implements InitializingBean {Value(${oss-constant.endpoint})private String endpoint;Value(${oss-constant.access-key})private String accessKeyId;Value(${oss-constant.secret-key})private String accessKeySecret;Value(${oss-constant.bucket})private String bucketName;public static String ENDPOINT;public static String ACCESS_KEY_ID;public static String ACCESS_KEY_SECRET;public static String BUCKET_NAME;Overridepublic void afterPropertiesSet() {// 初始化设置值ENDPOINT this.endpoint;ACCESS_KEY_ID this.accessKeyId;ACCESS_KEY_SECRET this.accessKeySecret;BUCKET_NAME this.bucketName;}} 3. 绑定方式2
3.1 依赖. dependencies!-- 属性装配提示 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-configuration-processor/artifactIdoptionaltrue/optional/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdexecutionsexecution!-- 打包时排除 属性装配提示插件 --configurationgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-configuration-processor/artifactId/configuration/execution/executions/plugin/plugins/build
3.2 boot 中提供的属性装配功能。
yaml中的前缀和下面的ConfigurationProperties的prefix对应。 yaml中的属性和实体类属性对应。(yaml中的横杠可以转驼峰)
package top.bitqian.hello.constant;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;/*** 此Java类与配置文件绑定了.* author echo lovely* date 2021/3/20 09:36*//*1. 绑定方法1:ComponentConfigurationProperties(prefix oss-constant)2. 绑定方法2:实体配置类, ConfigurationProperties(prefix oss-constant)在配置类上开启和配置文件(yml, properties)绑定 :EnableConfigurationProperties(value {OssConstantConfig.class})*/Data
Component
ConfigurationProperties(prefix oss-constant)
public class OssConstantConfig {private String endpoint;private String accessKey;private String secretKey;private String bucket;
}
获取的话从容器中获取就行了。配置文件中的值会自动装配到Java Bean中。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/925386.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!