0

这是我的问题,

所以这是我在 pom.xml 中的个人资料:

<profiles>
    <profile>
            <id>dev</id>
            <activation>
                <property>
                    <name>environment</name>
                    <value>dev</value>
                </property>
            </activation>
        </profile>

        <profile>
            <id>prod</id>
            <activation>
                <property>
                    <name>environment</name>
                    <value>prod</value>
                </property>
            </activation>
        </profile>
</profiles>

当我使用这个 context.xml 时,我的应用程序运行良好:

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jd="http://www.springframework.org/schema/jdbc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:sws="http://www.springframework.org/schema/web-services"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3-0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<context:component-scan base-package="net.xxx.xxx.dao" />       

  <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
            <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
            <property name="url" value="jdbc:oracle:thin:@xxx.xxx.xxx.xxx:1521:essmh" />
            <property name="username" value="*****" />
            <property name="password" value="*****" />
      </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="persistenceUnitName" value="carfleetPersistenceUnit" />
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven />    
</beans>    

dataSource但是,如果我在生产或开发中,我希望能够改变我的风格。

所以这就是我现在得到的:

 <beans profile="dev">
  <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
            <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
            <property name="url" value="jdbc:oracle:thin:@xxx.xxx.xxx.xxx:1521:dev" />
            <property name="username" value="*****" />
            <property name="password" value="*****" />
      </bean>   
</beans>

<beans profile="prod">
  <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
            <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
            <property name="url" value="jdbc:oracle:thin:@xxx.xxx.xxx.xxx:1522:prod" />
            <property name="username" value="*****" />
            <property name="password" value="*****" />
      </bean>
</beans>

<beans profile="dev, prod">

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="persistenceUnitName" value="carfleetPersistenceUnit" />
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven />

</beans>

我通过使用mvn -e -P prod help:active-profiles该配置文件prod在每个包中是否处于活动状态来检查它是否处于活动状态。但最后,它不起作用,我得到错误:

Caused by : org.springframework.beans.factory.NoSuchBeanDefinitionException : No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0.

肯定存在错误,因为这是从“应用程序正在运行”到“应用程序有错误”的唯一更改。


编辑

我现在明白我混淆了 Spring 配置文件和 Maven 配置文件,但我仍然想链接它们中的 2 个。因此,为此,我将属性放在我的 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>

我添加了我的 web.xml :

<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>${environment}</param-value>
</context-param>

当我硬编码${environment}devprod完美地工作时。但是当我希望能够从 Maven 切换时:clean -Pprod package tomcat7:redeploy 我遇到了以下问题:

IllegalArgumentException : Could not resolve placeholder 'environment' in string value "${environment}`
4

3 回答 3

2

您正在混合 Maven 配置文件和弹簧配置文件!这些是不同的东西。您可以使用

spring.profiles.active

启用所需的配置文件。

于 2015-07-24T14:02:25.623 回答
1

编辑解决方案:Maven:如何在 web.xml 文件中填充变量

该插件maven-war-plugin需要一些配置,以便能够对 Spring 配置文件的描述符进行过滤。

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.5</version>
  <configuration>
    <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
  </configuration>
</plugin>

有了这一切,您可以从 Maven 配置文件切换到 Spring 配置文件。谢谢大家的帮助:)。

于 2015-07-24T15:06:30.140 回答
1

如果您使用的是 web.xml,您可以设置 context-param 属性,该属性将指向您当前的环境。首先尝试硬编码这个标签。

<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>dev</param-value>
</context-param>

在您的 web.xml 配置中检查是否一切正常,然后尝试使用当前环境设置您自己的变量。请参阅此帖子以获取更多帮助。

于 2015-07-24T14:15:52.240 回答