当前位置: 首页 > wzjs >正文

摄影网站下载超级搜索引擎

摄影网站下载,超级搜索引擎,asp+dreamweaver数据库网站开发与实例,深圳定制巴士怎么预约Spring 提供了几种优雅的方式来将配置文件中的简单值(如字符串、数字、布尔值等)注入到 Bean 中。 主要有两种主流方法: 使用 Value 注解:简单直接,适合注入单个值。使用 ConfigurationProperties:类型安全…

Spring 提供了几种优雅的方式来将配置文件中的简单值(如字符串、数字、布尔值等)注入到 Bean 中。

主要有两种主流方法:

  1. 使用 @Value 注解:简单直接,适合注入单个值。
  2. 使用 @ConfigurationProperties:类型安全,适合将一组相关的配置属性映射到一个对象上,是 Spring Boot 官方推荐的方式。

方法一:使用 @Value 注解(简单直接)

@Value 是最直接的方式,它可以从属性文件、环境变量、系统属性等地方读取值并注入到字段中。

步骤 1:在 application.properties 中定义属性
# 字符串
app.name=My Awesome Application
# 数字
app.thread-pool.size=10
# 布尔值
app.feature.new-ui.enabled=true
# 逗号分隔的列表
app.admin.emails=admin@example.com,support@example.com
# 带默认值的属性 (我们在代码中处理)
# app.description=... (假设这个属性没有被定义)
步骤 2:在 Bean 中使用 @Value 注入

@Value 使用 ${...} 占位符语法来引用属性。

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import jakarta.annotation.PostConstruct; // 使用 jakarta.* 如果是 Spring Boot 3+
import java.util.List;@Component
public class AppConfig {// 注入字符串@Value("${app.name}")private String appName;// Spring 会自动进行类型转换,将 "10" 转换为 int@Value("${app.thread-pool.size}")private int threadPoolSize;// 同样,"true" 会被转换为 boolean@Value("${app.feature.new-ui.enabled}")private boolean isNewUiEnabled;// 注入一个列表 (Spring 会自动按逗号分割)@Value("${app.admin.emails}")private List<String> adminEmails;// 提供默认值:如果属性不存在,则使用冒号后面的默认值@Value("${app.description:This is a default description}")private String appDescription;// 打印出来看看结果@PostConstructpublic void printConfig() {System.out.println("Application Name: " + appName);System.out.println("Thread Pool Size: " + threadPoolSize);System.out.println("Is New UI Enabled: " + isNewUiEnabled);System.out.println("Admin Emails: " + adminEmails);System.out.println("Application Description: " + appDescription);}
}

@Value 的优点:

  • 非常简单,用于注入少量、零散的配置时非常方便。
  • 支持 SpEL(Spring Expression Language),功能强大,例如可以注入其他 Bean 的属性值:@Value("#{otherBean.someProperty}")

@Value 的缺点:

  • 当属性很多时,代码会变得冗长和分散。
  • 不是类型安全的。如果你在属性文件中写了一个非数字的值给 threadPoolSize,错误只会在运行时才会暴露。
  • 重构不友好(比如修改属性前缀)。

方法二:使用 @ConfigurationProperties(官方推荐)

这种方式将一组相关的配置属性绑定到一个类型安全的 POJO(Plain Old Java Object)上。

步骤 1:在 application.properties 中定义属性(与上面相同)
app.name=My Awesome Application
app.thread-pool.size=10
app.feature.new-ui.enabled=true
app.admin.emails=admin@example.com,support@example.com
步骤 2:创建一个配置属性类

这个类就是一个普通的 Java 类,包含了与配置文件中属性名对应的字段。

import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.List;// 这个注解告诉 Spring,将 "app" 前缀的属性绑定到这个类的字段上
@ConfigurationProperties(prefix = "app")
public class AppProperties {private String name;private List<String> adminEmails;// 注意:这里我们创建一个嵌套类来映射嵌套的属性,结构更清晰private final ThreadPool threadPool = new ThreadPool();private final Feature feature = new Feature();// 嵌套类,用于映射 app.thread-pool.*public static class ThreadPool {private int size;// Getter and Setter...public int getSize() { return size; }public void setSize(int size) { this.size = size; }}// 嵌套类,用于映射 app.feature.*public static class Feature {private final NewUi newUi = new NewUi();public static class NewUi {private boolean enabled;// Getter and Setter...public boolean isEnabled() { return enabled; }public void setEnabled(boolean enabled) { this.enabled = enabled; }}public NewUi getNewUi() { return newUi; }}// 主类的 Getters and Setters...public String getName() { return name; }public void setName(String name) { this.name = name; }public List<String> getAdminEmails() { return adminEmails; }public void setAdminEmails(List<String> adminEmails) { this.adminEmails = adminEmails; }public ThreadPool getThreadPool() { return threadPool; }public Feature getFeature() { return feature; }
}

注意

  • Spring Boot 会自动将 kebab-case(如 thread-pool.size)或 snake_case(thread_pool.size)的属性名映射到 camelCase(threadPool.size)的字段名。
  • 必须提供标准的 Getters 和 Setters,Spring Boot 通过它们来设置值。
步骤 3:在主配置类中启用这个属性类
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
// 告诉 Spring Boot 启用 AppProperties,并将其注册为一个 Bean
@EnableConfigurationProperties(AppProperties.class)
public class MyApplication {public static void main(String[] args) {SpringApplication.run(MyApplication.class, args);}
}
步骤 4:在任何其他 Bean 中注入和使用这个属性对象

现在,你可以像注入任何其他 Service 一样,注入整个 AppProperties 对象。

import org.springframework.stereotype.Service;@Service
public class MyBusinessService {private final AppProperties appProperties;// 通过构造函数注入,非常清晰public MyBusinessService(AppProperties appProperties) {this.appProperties = appProperties;}public void doSomething() {System.out.println("Running service for app: " + appProperties.getName());if (appProperties.getFeature().getNewUi().isEnabled()) {System.out.println("Using the new UI with thread pool size: " + appProperties.getThreadPool().getSize());}}
}

总结:@Value vs @ConfigurationProperties

特性@Value@ConfigurationProperties (推荐)
用途注入单个、零散的值将一组相关的配置绑定到一个对象
类型安全运行时检查,弱类型编译时绑定(部分 IDE 支持),强类型
代码组织配置分散在多个类中配置集中在一个 POJO 中,结构清晰
验证不支持 JSR-303 验证支持 @Validated 注解进行数据校验
IDE 支持弱(简单的字符串跳转)强大(在 .properties 文件中输入时会有代码提示和元数据支持)
灵活性支持 SpEL,更灵活面向结构化配置,更规范

最佳实践:

  • 优先使用 @ConfigurationProperties。它让你的代码更健壮、更易于维护和测试。
  • 只有当你需要注入单个与其他配置无关的值,或者需要利用 SpEL 的强大功能时,考虑使用 @Value
http://www.dtcms.com/wzjs/20637.html

相关文章:

  • 东阳市网站建设制作浏览广告赚钱的平台
  • 网站建设需要租赁服务器吗上海互联网公司排名
  • wordpress首页调用分类版块百度小程序优化排名
  • 电商网站上信息资源的特点包括哪些百度搜索排名服务
  • 用dw做的代码怎么放在网站上湖南好搜公司seo
  • 西安网站建设技术外包宣传渠道有哪些
  • 免注册个人网站制作推广网络推广
  • 甘肃三轮建设监理网站网站关键词优化
  • 株洲新站seo网络优化工程师骗局
  • 郑州做网站优化公司企业网站建设流程
  • 工信部网站备案查询步骤杭州网络推广外包
  • 谁有做网站的朋友的V信b站推广网站mmmnba
  • dnf怎么做发卡网站百度搜索风云榜
  • 建站之星 网站排名品牌营销策略分析论文
  • 西安网站建设设计公司企业培训师资格证报考2022
  • 做服装广告素材网站打开app下载
  • 武汉营销网站设计搜索引擎优化的方法
  • 国外做农产品有名的网站有哪些seo优化一般多少钱
  • 那个网站做外贸好百度最怕哪个投诉电话
  • 专门做调查问卷的网站南京响应式网站建设
  • 中国建设银行招投标网站网络销售怎么聊客户
  • 软装设计师招聘搜索引擎优化的内容有哪些
  • 学做网站论坛课程东莞谷歌推广公司
  • 上传了网站标志 功能链接快速网站
  • 网站建设 中企动力西安谷歌官网登录入口
  • 同步朋友圈到wordpress天津seo网站管理
  • 怀化订水网站洛阳网站建设
  • 石家庄网站开发建设百度指数怎么看排名
  • 武汉做网站哪家最好互联网营销师怎么做
  • wordpress vps建站视频推广平台