我试图在main-project context.xml中导入两个不同的jar,就像这个
一样<import resource="classpath*:/META-INF/mini-project-1-context.xml" />
<import resource="classpath*:/META-INF/mini-project-2-context.xml" />
它未能通过迷你项目服务回购。每个迷你项目都有mysql配置。请找到我的项目结构。
main-project.war
|
------- main-project-context.xml
| |
| |
| mini-project-1.jar
| |
| ------- mini-project-1-context.xml
| |
| ------- persistence.xml
|
mini-project-2.jar
|
------- mini-project-2-context.xml
|
------- persistence.xml
找到mini-project-1-context.xml。项目名称仅在另一个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:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
<context:component-scan
base-package="com.mini.project.one" />
<jpa:repositories base-package="com.mini.project.one.repo" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
depends-on="flyway">
<property name="persistenceXmlLocation"
value="classpath:/META-INF/persistence.xml" />
<property name="dataSource" ref="mini-1-DataSource" />
<property name="persistenceUnitName" value="Mini1PU" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">0</prop>
</props>
</property>
</bean>
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<jee:jndi-lookup jndi-name="project/DS/miniproject1"
resource-ref="true" id="mini-1-DataSource" />
<bean id="flyway" class="org.flywaydb.core.Flyway" init-method="migrate">
<property name="dataSource" ref="mini-1-DataSource" />
<property name="table" value="module1_schema_version" />
<property name="initOnMigrate" value="false" />
<property name="locations" value="classpath:db/migration" />
</bean>
<!-- ************ JPA configuration *********** -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
主项目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:camel="http://camel.apache.org/schema/spring"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
<context:component-scan base-package="com.main.project" />
<import resource="classpath*:/META-INF/mini-project-1-context.xml" />
<import resource="classpath*:/META-INF/mini-project-2-context.xml" />
<camelContext xmlns="http://camel.apache.org/schema/spring"
trace="true">
<contextScan />
</camelContext>
我收到以下错误
12:03:33.894 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Retrieved dependent beans for bean 'org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter#1e5bc130': [entityManagerFactory]
12:03:33.894 [localhost-startStop-1] ERROR o.s.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'service': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field:
的pom.xml
<dependency>
<groupId>com.project</groupId>
<artifactId>mini-project-1</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.project</groupId>
<artifactId>mini-project-1</artifactId>
<version>1.0</version>
</dependency>
答案 0 :(得分:0)
通过在依赖模块的context.xml(此处为main-project-context.xml)中定义transactionManager
和annotation-driven
并从jar项目中删除transactionManager
来解决此问题(mini-project-1-context.xml和mini-project-2-context.xml)。
从 mini-project-1-context.xml和mini-project-2-context.xml中删除 transactionManager
和annotation-driven
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
和添加 main-project-context.xml 中的transactionManager
和annotation-driven
。
<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<qualifier value="qualifier" />
</bean>
<bean id="transactionManager1" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory1" />
<qualifier value="qualifier1" />
</bean>