当前位置: 首页> 娱乐> 八卦 > 朋友圈信息流广告投放价格_网站策划书基本项目_太原百度推广开户_万能识图

朋友圈信息流广告投放价格_网站策划书基本项目_太原百度推广开户_万能识图

时间:2025/7/11 23:59:50来源:https://blog.csdn.net/hekai7217/article/details/144842539 浏览次数:1次
朋友圈信息流广告投放价格_网站策划书基本项目_太原百度推广开户_万能识图

在 Spring Boot 中,读取 YAML 配置文件(application.yml )的方式有多种。以下是几种常见的方式以及相关的案例说明:

1. 使用 @Value 注解直接读取 YAML 配置

@Value 注解是 Spring 提供的一种简便方式,可以直接将 YAML 配置中的值注入到字段中。

配置示例 (application.yml):
myapp:name: "Spring Boot Application"version: "1.0.0"features:feature1: truefeature2: false
Java 类使用 @Value 读取配置:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component
public class AppConfig {@Value("${myapp.name}")private String appName;@Value("${myapp.version}")private String version;@Value("${myapp.features.feature1}")private boolean feature1;public void printConfig() {System.out.println("App Name: " + appName);System.out.println("Version: " + version);System.out.println("Feature 1 Enabled: " + feature1);}
}

2. 使用 @ConfigurationProperties 注解将整个配置类映射到 Java 对象

@ConfigurationProperties 注解是 Spring Boot 推荐的方式,它能将 YML 配置文件中的数据绑定到一个 Java 类上,适用于读取复杂的嵌套配置。

配置示例 (application.yml):
myapp:name: "Spring Boot Application"version: "1.0.0"features:feature1: truefeature2: false
Java 类使用 @ConfigurationProperties
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Component
@ConfigurationProperties(prefix = "myapp")
public class MyAppConfig {private String name;private String version;private Features features;public static class Features {private boolean feature1;private boolean feature2;// Getters and setterspublic boolean isFeature1() {return feature1;}public void setFeature1(boolean feature1) {this.feature1 = feature1;}public boolean isFeature2() {return feature2;}public void setFeature2(boolean feature2) {this.feature2 = feature2;}}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getVersion() {return version;}public void setVersion(String version) {this.version = version;}public Features getFeatures() {return features;}public void setFeatures(Features features) {this.features = features;}public void printConfig() {System.out.println("App Name: " + name);System.out.println("Version: " + version);System.out.println("Feature 1 Enabled: " + features.isFeature1());System.out.println("Feature 2 Enabled: " + features.isFeature2());}
}
启用 @ConfigurationProperties

application.yml 中启用 @ConfigurationProperties 的自动绑定(Spring Boot 2.2及以上版本需要加上 @EnableConfigurationProperties 注解):

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@EnableConfigurationProperties(MyAppConfig.class)
public class MyApplication {public static void main(String[] args) {SpringApplication.run(MyApplication.class, args);}
}

3. 使用 Environment 对象获取配置

Environment 接口提供了读取配置值的能力,适合在没有注解的情况下,或者在更动态的环境中使用。

配置示例 (application.yml):
myapp:name: "Spring Boot Application"version: "1.0.0"features:feature1: truefeature2: false
使用 Environment 获取配置:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;@Component
public class EnvironmentConfig {@Autowiredprivate Environment environment;public void printConfig() {String appName = environment.getProperty("myapp.name");String version = environment.getProperty("myapp.version");boolean feature1 = Boolean.parseBoolean(environment.getProperty("myapp.features.feature1"));System.out.println("App Name: " + appName);System.out.println("Version: " + version);System.out.println("Feature 1 Enabled: " + feature1);}
}

4. 使用 @PropertySource 加载 YAML 配置文件

Spring Boot 默认会加载 application.ymlapplication.properties 文件,但如果你需要加载其他外部的 YAML 配置文件,可以使用 @PropertySource 来指定。

配置示例 (my-config.yml`):
my:setting: "Some other config"
Java 类使用 @PropertySource
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;@Component
@PropertySource("classpath:my-config.yml")
public class myConfig {@Value("${my.setting}")private String externalSetting;public void printExternalSetting() {System.out.println("other Setting: " + externalSetting);}
}

总结:

  • @Value:适合简单的配置项注入。
  • @ConfigurationProperties:适用于复杂的多层嵌套配置,推荐使用。
  • Environment:用于动态获取配置,灵活但较为冗长。
  • @PropertySource:用于加载额外的配置文件。
关键字:朋友圈信息流广告投放价格_网站策划书基本项目_太原百度推广开户_万能识图

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: