简体   繁体   English

配置问题:Bean名称“ dataSource”已在此使用 <beans> 元件

[英]Configuration issue : Bean name “dataSource” is already used in this <beans> element

Here is my problem, I want to define 2 Maven profiles, 这是我的问题,我想定义2个Maven配置文件,

one for the dev environment and 一个用于dev environment

one for the prod environment . 一种适用于prod environment

In my pom.xml, I so defined 2 profiles : 在pom.xml中,我定义了2个配置文件:

<!-- pom.xml -->

<profiles>

    .....

    <profile>
      <id>dev</id>
      <activation>
        <property>
          <name>environment</name>
          <value>dev</value>
        </property>
        <activeByDefault>true</activeByDefault>
      </activation>
    </profile>

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

</profiles>

and in my context.xml file, I want to use a different dataSource according to which profile I choosed : 并且在我的context.xml文件中,我想根据选择的配置文件使用不同的dataSource

<?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="xxx.xxx.xxx.dao" />       

    <!-- DEV -->
    <bean p:profiles="dev" 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:@127.0.0.1:1522:******" />     
        <property name="username" value="******" />
        <property name="password" value="******" />
    </bean>     

    <!-- PROD -->
    <bean p:profiles="prod" 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:@xxxx.xxxx.xxxx.xxxx:1521:******" />
        <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>    

I for sure did something wrong in the configuration or the utilisation of theses profiles... but I don't know what... 我肯定在配置或这些配置文件的利用方面出了点问题...但是我不知道...

I'm building with maven by using the command : 我正在通过使用命令使用maven进行构建:

mvn -e -Denvironment=dev clean package tomcat7:redeploy

But I got the error : 但是我得到了错误:

Configuration problem : Bean name "dataSource" is already used in this <beans> element.

If someone can tell me what is wrong :/ Because I test a lot of things, but it's just giving different errors, I don't know exactly what to do to fix it. 如果有人可以告诉我什么地方不对://因为我测试了很多东西,但是它只是给出了不同的错误,所以我不知道该怎么做才能解决它。

Thanks in advance ! 提前致谢 !

Profiles in xml are defined in the following way : xml中的配置文件以以下方式定义:

Your dev profile beans here : 您的开发人员档案bean在这里:

 <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:@127.0.0.1:1522:******" />     
    <property name="username" value="******" />
    <property name="password" value="******" />
</bean>    
</beans>

Your Prod profile bean here : 您的产品概要文件bean在这里:

<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:@xxxx.xxxx.xxxx.xxxx:1521:******" />
    <property name="username" value="******" />
    <property name="password" value="******" />
  </bean>
</beans>

and Common beans here : 和普通豆在这里:

<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>
</beans>

For activating a particular profile you need to set System Property as spring.profiles.active as <your profile name> . 要激活特定的配置文件,您需要将System Property设置为spring.profiles.active作为<your profile name> It can also be achieved through web.xml by : 也可以通过以下方式通过web.xml实现:

 <servlet>
  <servlet-name>dispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
      <param-name>spring.profiles.active</param-name>
      <param-value>production</param-value>
  </init-param>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 @Configuration 类后 SpringbootTest 失败:创建名为“datasource”的 bean 时出错 - SpringbootTest failing after @Configuration class: Error creating bean with name 'datasource' 错误org.springframework.beans.factory.BeanCreationException:创建类路径资源中定义的名称为“ dataSource”的bean时出错 - Error org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource Android simpleXML元素“ [element Name]”已被使用 - Android simpleXML Element '[element Name]' is already used Spring在Main中使用任何bean之前从Configuration类加载/运行每个bean? - Spring load/runs every beans from Configuration class before any bean is used in main? JBoss DataSource配置问题 - JBoss DataSource configuration Issue JBoss AS 7数据源配置问题 - JBoss AS 7 datasource configuration issue 在@Configuration bean 中的 SpEL 表达式中引用 ConfigurationProperties Beans - Referring to ConfigurationProperties Beans in SpEL expression in @Configuration bean 嵌套类中的spring Bean Overriding 2.1.x @Configuration创建@Bean创建失败,并且&#39;已经定义了具有该名称的bean&#39; - spring Bean Overriding 2.1.x in nested class @Configuration @Bean creation fails with 'A bean with that name has already been defined' @ org.simpleframework.xml.Element已使用元素“名称” - Element 'Name' is already used with @org.simpleframework.xml.Element 不允许属性&#39;profile&#39;出现在元素&#39;beans:bean&#39;中 - Attribute 'profile' is not allowed to appear in element 'beans:bean'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM