无需Maven配置文件即可工作,但存在错误 [英] Works without maven profile, bug with

查看:89
本文介绍了无需Maven配置文件即可工作,但存在错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题,

这是pom.xml中的个人资料:

So here is my profile in 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时,我的应用程序可以完美运行:

When I use this context.xml, my application works perfectly :

<?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.

But I want to be able to change my dataSource according if I'm in production or in development.

这就是我现在所拥有的:

So here is what I've got now:

 <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是否在每个软件包中都处于活动状态.但是最后,它不起作用,并且出现错误消息:

I check by using mvn -e -P prod help:active-profiles that the profile prod is active in every package and it is. But at the end, it doesn't work and I get the Error :

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

确保错误在那里,因为这是唯一的更改,从应用程序正在工作"变为应用程序有错误".

For sure the error is there, because that's the only changed to go from "application is working" to "application has errors".

编辑

我现在知道我混淆了Spring配置文件和Maven配置文件,但是我仍然想链接它们中的2个. 因此,为此,我将属性放在pom.xml>配置文件中:

I now understand that I confused Spring profile and Maven profile, but I still, want to link the 2 of them. So for this, I put properties in my 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:

And I add in my web.xml :

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

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

When I hardcode ${environment} for dev or prod it works perfectly. But when I want to be able to switch from Maven : clean -Pprod package tomcat7:redeploy I got the following issue :

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

推荐答案

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

If you are using web.xml you can set context-param property which will point to your current env. First try to hardcode this tag.

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

在您的web.xml配置中

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

in your web.xml config to check if everything will work fine, then try to set your own variable with current environment. See this post for more help.

这篇关于无需Maven配置文件即可工作,但存在错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆