gpt4 book ai didi

java - 如何使用 Maven 配置文件设置 Spring Activity 配置文件

转载 作者:IT老高 更新时间:2023-10-28 13:02:42 32 4
gpt4 key购买 nike

我有一个使用 maven 作为构建工具的应用程序。

我正在使用 maven 配置文件来设置来自不同配置文件的不同属性。

我想做的是 maven 中的所有 Activity 配置文件也将被移植到 spring Activity 配置文件中,以便我可以在 bean 签名 (@profile) 中引用它们。但我不知道该怎么做。

例如:考虑以下 maven 设置

<profiles>
<profile>
<id>profile1</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
</properties>
</profile>
<profile>
<id>profile2</id>
<properties>
</properties>
</profile>
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
</properties>
</profile>
<profile>
<id>production</id>
<properties>
</properties>
</profile>
</profiles>

假设我在没有指定任何其他配置文件的情况下运行 maven,我希望 spring 将 profile1development 作为 Activity 配置文件。

最佳答案

有一种更优雅的方式可以同时在 2 个 maven+spring 配置文件之间切换。

首先,将配置文件添加到 POM(注意 - maven+spring 配置文件由单个系统变量激活):

<profiles>
<profile>
<id>postgres</id>
<activation>
<activeByDefault>true</activeByDefault>
<property>
<name>spring.profiles.active</name>
<value>postgres</value>
</property>
</activation>
<dependencies>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>h2</id>
<activation>
<property>
<name>spring.profiles.active</name>
<value>h2</value>
</property>
</activation>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.191</version>
</dependency>
</dependencies>
</profile>
</profiles>

第二,设置 spring 的默认配置文件(对于 maven,它已经在 POM 中设置)。对于 Web 应用程序,我在 web.xml 中插入了以下几行:

<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>postgres</param-value>
</context-param>

第三,将配置文件相关的 bean 添加到您的配置中。就我而言(XML 配置),它是:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="mainDataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties" ref="hibProps"/>
<property name="packagesToScan">
<list>
<value>my.test.model</value>
</list>
</property>
</bean>
...
<beans profile="postgres">
<bean name="mainDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://127.0.0.1:5432/webchat" />
<property name="username" value="postgres" />
<property name="password" value="postgres" />
</bean>
</beans>

<beans profile="h2">
<bean name="mainDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:file:./newsdb;INIT=RUNSCRIPT FROM 'classpath:init.sql';TRACE_LEVEL_FILE=0" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
</beans>

现在可以:

  • 使用 mvn jetty:runmvn jetty:run -Dspring.profiles.active=postgres 命令在 Postgres DB 上运行我的网络应用
  • 使用 mvn clean jetty:run -Dspring.profiles.active=h2
  • 在 H2 DB 上运行我的网络应用

关于java - 如何使用 Maven 配置文件设置 Spring Activity 配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25420745/

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