gpt4 book ai didi

java - 如何在 spring 应用程序上下文 xml 文件中使用 pom.xml 中的属性?

转载 作者:行者123 更新时间:2023-11-30 06:37:37 26 4
gpt4 key购买 nike

我有一个 spring 上下文文件,类似于下面给出的内容

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<context:property-placeholder
location="classpath:application.properties, classpath:application-${env}.properties"/>

<context:annotation-config/>
</beans>

配置配置文件的 pom.xml 片段如下 -

        <profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<env>prod</env>
</properties>
</profile>

我想在我的 Spring 应用程序上下文文件中使用根据所选 Maven 配置文件决定的属性“env”。

我怎样才能做到这一点?

编辑:- 它是一个 Spring Web 应用程序

        <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>

最佳答案

不直接回答你的问题,但是,如果你可以使用 Spring Boot,我认为你想要做的事情可以通过使用 Spring Profiles 轻松解决。 。

本质上,您可以避免在 Maven 和 XML 的多个配置文件中手动编码,而让 Spring 为您完成。 Here是一个基本示例。

关于java - 如何在 spring 应用程序上下文 xml 文件中使用 pom.xml 中的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44953470/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com