element-6ren"> element-这是我的问题,我想定义 2 个 Maven 配置文件, 一个用于开发环境和 一个用于生产环境。 在我的 pom.xml 中,我定义了 2 个配置文件: ..... d-6ren">
gpt4 book ai didi

java - 配置问题: Bean name "dataSource" is already used in this element

转载 作者:行者123 更新时间:2023-11-30 08:02:59 25 4
gpt4 key购买 nike

这是我的问题,我想定义 2 个 Maven 配置文件,

一个用于开发环境

一个用于生产环境

在我的 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>

在我的 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>

我肯定在配置或使用这些配置文件时做错了什么......但我不知道是什么......

我正在使用以下命令使用 Maven 进行构建:

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

但我收到错误:

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

如果有人可以告诉我出了什么问题:/因为我测试了很多东西,但它只是给出了不同的错误,我不知道到底该怎么做才能修复它。

提前致谢!

最佳答案

xml 中的配置文件按以下方式定义:

您的开发配置文件 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>

您的 Prod 配置文件 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>

这里还有普通 bean :

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

要激活特定配置文件,您需要将系统属性设置为 spring.profiles.active<your profile name> 。也可以通过 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>

关于java - 配置问题: Bean name "dataSource" is already used in this <beans> element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31608640/

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