多数据源场景下Spring Repository Bean创建错误(XML配置)
Hey there, let's work through your repository bean creation problem with your dual data source setup. I can spot a few key gaps and issues in your configuration that are likely causing the error—let's break them down one by one.
1. Fix the Truncated spring-api.xml
Your provided spring-api.xml is cut off mid-schema declaration, which will definitely cause parsing errors. First, let's complete the schema locations and add the necessary JPA repository scanning configuration tailored for your two data sources.
Here's the corrected and complete version of spring-api.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:security="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd"> <!-- Enable component scanning for your service and controller layers --> <context:component-scan base-package="com.soft.paramount.service, com.soft.paramount.controller"/> <mvc:annotation-driven/> <!-- Configure JPA Repositories for Product Data Source --> <jpa:repositories base-package="com.soft.paramount.repository.product" entity-manager-factory-ref="entityManagerFactoryProduct" transaction-manager-ref="transactionManagerProduct"/> <!-- Configure JPA Repositories for MDM Data Source --> <jpa:repositories base-package="com.soft.paramount.repository.mdm" entity-manager-factory-ref="entityManagerFactoryMdm" transaction-manager-ref="transactionManagerMdm"/> </beans>
2. Correct Package Scanning Mismatch in spring-jpa.xml
I noticed a typo in your MDM entity manager factory configuration:
<property name="packagesToScan" value="com.soft.paramount.model.mdm, com.evantagesoft.panasonic.repository.mdm" />
The repository package com.evantagesoft.panasonic.repository.mdm doesn't match your product's package structure (com.soft.paramount). This will prevent Spring from finding your MDM repositories. Fix it to match your actual package structure, e.g.:
<property name="packagesToScan" value="com.soft.paramount.model.mdm, com.soft.paramount.repository.mdm" />
3. Verify persistence.xml Configuration
Make sure your META-INF/persistence.xml includes both persistence units referenced in your setup, and avoid defining data sources here (since you're already specifying them in spring-jpa.xml):
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="productPersistence" transaction-type="RESOURCE_LOCAL"> <!-- No need to define data-source, it's set in Spring config --> <class>com.soft.paramount.model.product.Product</class> <!-- Add all your product entity classes here --> </persistence-unit> <persistence-unit name="mdmPersistence" transaction-type="RESOURCE_LOCAL"> <class>com.soft.paramount.model.mdm.MdmEntity</class> <!-- Add all your MDM entity classes here --> </persistence-unit> </persistence>
4. Explicitly Specify Transaction Managers in Service Layer
When using multiple transaction managers, you need to tell Spring which one to use for each service method. Use the value attribute of @Transactional to specify the manager:
@Service public class ProductService { @Autowired private ProductRepository productRepository; @Transactional("transactionManagerProduct") public Product saveProduct(Product product) { return productRepository.save(product); } }
@Service public class MdmService { @Autowired private MdmRepository mdmRepository; @Transactional("transactionManagerMdm") public MdmEntity saveMdmEntity(MdmEntity entity) { return mdmRepository.save(entity); } }
5. Ensure JNDI Data Sources Are Properly Configured
Double-check that your application server (Tomcat, JBoss, etc.) has the JNDI resources java:comp/env/jdbc/productDB and java:comp/env/jdbc/mdmDB correctly configured. If you're testing locally, you can switch to a basic DriverManagerDataSource temporarily to rule out JNDI issues:
<!-- Example for local testing (replace with your DB credentials) --> <bean id="productDS" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/product_db"/> <property name="username" value="root"/> <property name="password" value="your_password"/> </bean>
After making these changes, your repository beans should be created successfully. Let me know if you run into any specific error messages after applying these fixes!
内容的提问来源于stack exchange,提问作者Ahsan Iqbal

