当前位置: 首页> 文旅> 旅游 > 报价单模板_拼多多官网_广州王牌seo_中国联通腾讯

报价单模板_拼多多官网_广州王牌seo_中国联通腾讯

时间:2025/7/12 14:03:45来源:https://blog.csdn.net/quyunde/article/details/146575278 浏览次数:0次
报价单模板_拼多多官网_广州王牌seo_中国联通腾讯

pom.xml与 .yml java配置参数传递

 

在Java项目中,通过 pom.xml 和 .yml 文件(如 application.yml)传递变量通常涉及 构建时(Maven)和 运行时(Spring Boot)两个阶段的配置。以下是具体的实现方法:

 


 

一、从 pom.xml 传递变量到 .yml(构建时注入)

适用于将 Maven 属性(如版本号、环境配置)动态注入到 Spring Boot 的配置文件中。

 

1. 在 pom.xml 中定义属性


<properties><custom.property>value_from_pom</custom.property></properties>

 

2. 启用资源过滤

在 pom.xml 的 <build> 部分配置资源过滤,允许 Maven 替换 .yml 文件中的占位符:


<resources><resource><directory>src/main/resources</directory><filtering>true</filtering> <!-- 启用过滤 --><includes><include>**/*.yml</include></includes></resource></resources>

 

3. 在 .yml 中使用占位符

在 application.yml 中使用 ${...} 引用 Maven 属性:


myapp:property: @custom.property@ # 注意:此处用 @ 而非 $,避免与 Spring 占位符冲突

 

原理

  • Maven 资源过滤默认使用 ${property} 格式,但 Spring Boot 也使用 ${},为避免冲突,改用 @property@

  • 构建时,Maven 会将 @custom.property@ 替换为 pom.xml 中定义的值。

 


 

二、从 .yml 传递变量到 Java 代码(运行时读取)

Spring Boot 自动加载 application.yml,可通过注解直接注入属性。

 

1. 在 .yml 中定义属性


myapp:name: "My Application"timeout: 5000

 

2. 在 Java 类中注入属性

使用 @Value 注解或 @ConfigurationProperties

 

方法 1:@Value


@Componentpublic class MyConfig {@Value("${myapp.name}")private String appName;@Value("${myapp.timeout}")private int timeout;}

 

方法 2:@ConfigurationProperties(推荐)


@Component@ConfigurationProperties(prefix = "myapp")public class MyConfig {private String name;private int timeout;// Getter 和 Setter}

 


 

三、通过 Maven Profile 实现多环境配置

根据不同环境(如开发、生产)动态切换配置。

 

1. 在 pom.xml 中定义 Profiles


<profiles><profile><id>dev</id><properties><environment>dev</environment></properties></profile><profile><id>prod</id><properties><environment>prod</environment></properties></profile></profiles>

 

2. 创建多环境配置文件

  • application-dev.yml

  • application-prod.yml

 

3. 在 application.yml 中激活环境


spring:profiles:active: @environment@ # 由 Maven 动态替换

 

4. 构建时指定 Profile


mvn clean install -Pdev # 激活 dev 环境

 


 

四、常见问题解决

1. 资源过滤未生效

  • 确保 pom.xml 中正确配置了 <filtering>true</filtering>

  • 检查 .yml 文件是否在 <includes> 范围内。

 

2. 占位符冲突

  • 使用 @property@ 代替 ${property} 避免与 Spring 冲突。

 

3. 敏感信息处理

  • 不要将密码等敏感信息硬编码在 pom.xml 中,建议使用环境变量或加密工具(如 Jasypt)。

 


 

通过以上步骤,可以实现 pom.xml 和 .yml 之间的变量传递,灵活管理构建时和运行时的配置。

 

pom.xml与.yml占位符冲突怎么处理

 

在 Java 项目中,Maven (pom.xml) 和 Spring Boot (application.yml) 都使用 ${} 格式的占位符,但两者的解析阶段不同(构建时 vs. 运行时),直接使用相同符号会导致冲突。以下是解决方案和最佳实践:


一、冲突原因分析

阶段      工具        占位符格式解析时机  冲突场景                  
构建时Maven      ${prop}  编译打包时Maven 未替换,Spring 无法解析
运行时Spring Boot${prop}  应用启动时Spring 误解析 Maven 占位符

二、解决方案

1. 修改 Maven 占位符分隔符(推荐)

在 pom.xml 中 自定义 Maven 资源过滤的分隔符,避免与 Spring Boot 的 ${} 冲突。

配置方法:

<project>...<build><resources><resource><directory>src/main/resources</directory><filtering>true</filtering><!-- 定义 Maven 使用 @ 作为占位符分隔符 --><delimiters><delimiter>@</delimiter> <!-- 关键配置 --></delimiters><includes><include>**/*.yml</include></includes></resource></resources></build>
</project>

在 .yml 中使用 @ 作为占位符:

myapp:version: @project.version@  # 引用 Maven 属性custom: @custom.property@   # 引用 pom.xml 中的自定义属性

优点:

  • 完全避免符号冲突。
  • 无需修改 Spring Boot 配置。

2. 禁用 Spring Boot 对 ${} 的解析(仅限特殊情况)

如果某些 ${} 必须由 Maven 处理,可以 在 Spring Boot 中忽略特定占位符,但需谨慎使用。

在 application.yml 中:

spring:main:banner-mode: offconfig:# 禁用 Spring 对未解析占位符的报错(不推荐)ignore-unresolvable-placeholders: true

缺点:

  • 可能导致 Spring 配置中的真实占位符失效。
  • 隐藏潜在错误,不推荐生产环境使用。

3. 使用 Maven 属性文件传递(替代方案)

将 Maven 属性写入一个临时文件(如 maven.properties),再由 Spring Boot 读取。

步骤:

  1. 在 pom.xml 中生成属性文件:    xml    <plugin>      <groupId>org.codehaus.mojo</groupId>      <artifactId>properties-maven-plugin</artifactId>      <version>1.1.0</version>      <executions>        <execution>          <phase>generate-resources</phase>          <goals>            <goal>write-project-properties</goal>          </goals>          <configuration>            <outputFile>${project.build.outputDirectory}/maven.properties</outputFile>          </configuration>        </execution>      </executions>    </plugin>    

  2. 在 Spring Boot 中读取该文件:    yaml    spring:      config:        import: classpath:maven.properties    

  3. 在 Java 代码中使用属性:    java    @Value("${project.version}")    private String version;    


三、验证与调试

1. 检查构建后的文件

构建完成后,检查 target/classes 目录下的 .yml 文件,确认占位符是否被正确替换:

cat target/classes/application.yml

2. 开启 Maven 调试日志

在构建命令中添加 -X 参数,查看资源过滤过程:

mvn clean install -X

四、最佳实践

  1. 统一约定:    - 使用 @ 作为 Maven 占位符,${} 作为 Spring 占位符。    - 在团队中明确约定符号规则。

  2. 多环境配置:    - 结合 Maven Profile 和 Spring Profile 动态切换环境。    bash    mvn install -Pprod -Dcustom.property=prod_value    

  3. 敏感信息处理:    - 不要将密码等敏感信息硬编码到 pom.xml,使用环境变量或加密工具(如 Jasypt)。


五、常见问题

1. 占位符未被替换

  • 检查 pom.xml 中是否配置了 <filtering>true</filtering>
  • 确认文件路径在 <includes> 范围内。

2. Spring 解析了 Maven 占位符

  • 确保在 .yml 中使用的是 @ 而非 ${}

3. 特殊字符转义

如果属性值包含 @ 或 $,需使用转义:

password: "@@password@@"  # 最终解析为 @password@

通过以上方法,可以彻底解决 pom.xml 和 .yml 的占位符冲突问题,实现构建时与运行时的无缝配置传递。

后端xmlspring bootjava系统架构

发布于2025-03-27著作权归作者所有

关键字:报价单模板_拼多多官网_广州王牌seo_中国联通腾讯

版权声明:

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

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

责任编辑: