gpt4 book ai didi

java - 多模块项目中 JPA 存储库的 @Autowire 失败

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

我有一个 3 模块的 Maven 项目,模型、服务和 Web。在模型中,我定义了 JPARepositories 和我的@entity 类。我实际上在模型项目中有 2 个实体管理器和 2 个事务管理器,因此我可以连接到多个数据库(我不需要分布式事务)。

但是我在服务层注入(inject) JPARepository 时遇到错误。

No qualifying bean of type [com.mycompany.rd.repository.misf.ProjectRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=projectRepository)}

在 web.xml 中我有这个:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/appContext-demo.xml,
classpath*:/appContext-services.xml
</param-value>
</context-param>


<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

appContext-demo.xml 位于 web 模块中,如下所示:

<?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:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">



<bean id="appInitializer" class="com.mycompany.rd.web.misf.demo.AppInitializer"
init-method="init"/>
</beans>

在我的服务模块中,appContext-services.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:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
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-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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">



<context:annotation-config />
<context:component-scan base-package="com.mycompany.rd.service"/>

<!-- Load app context for persistence from base-model project -->
<import resource="classpath*:/appContext-model.xml" />

在我的模型层中,我为 appContext-model.xml 设置了这个:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx" 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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.1.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-3.1.xsd">


<context:annotation-config />

<import resource="persistence.xml" />

<context:component-scan base-package="com.mycompany.rd.model,com.mycompany.rd.repository" />

<bean id="grapsDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url"
value="jdbc:oracle:thin:@xxxx" />
<property name="username" value="xxx" />
<property name="password" value="xxx" />
</bean>

<bean class="org.springframework.orm.jpa.JpaTransactionManager"
id="grapsTM">
<property name="entityManagerFactory" ref="grapsEM" />
<qualifier value="graps" />
</bean>

<bean id="grapsEM"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="grapsDataSource" />
<property name="persistenceUnitName" value="graps-jpa" />
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
</bean>

<tx:annotation-driven transaction-manager="grapsTM" />

<bean id="xpDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="url" value="jdbc:sqlserver://xxxx;"/>
<property name="username" value="xxx"/>
<property name="password" value="xxx"/>
</bean>

<bean class="org.springframework.orm.jpa.JpaTransactionManager"
id="xpTM">
<property name="entityManagerFactory" ref="xpEM" />
<qualifier value="xp"/>
</bean>

<bean id="xpEM"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="xpDataSource" />
<property name="persistenceUnitName" value="xp-jpa" />
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
</bean>

<tx:annotation-driven transaction-manager="xpTM" />


<jpa:repositories base-package="com.mycompany.rd.repository.misf" entity-manager-factory-ref="xpEM" transaction-manager-ref="xpTM"/>
<jpa:repositories base-package="com.mycompany.rd.repository.graps" entity-manager-factory-ref="grapsEM" transaction-manager-ref="grapsTM" />

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />



</beans>

我正在 appContext-model.xml 中导入 persistence.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="graps-jpa" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.mycompany.rd.model.graps.PrProject</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.target-database" value="Oracle"/>
<property name="eclipselink.ddl-generation" value="none"/>
<property name="eclipselink.weaving" value="static"/>
</properties>
</persistence-unit>

<persistence-unit name="xp-jpa" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.mycompany.rd.model.graps.PrProject</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.target-database" value="SQLServer"/>
<property name="eclipselink.ddl-generation" value="none"/>
<property name="eclipselink.weaving" value="static"/>
</properties>
</persistence-unit>
</persistence>

我的服务类引发错误是@Autowired,带有模型模块中的 JPARepository:

package com.mycompany.rd.service.misf;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mycompany.rd.model.misf.Project;
import com.mycompany.rd.repository.misf.ProjectRepository;

@Service
public class ProjectService {

private static Logger logger = LoggerFactory.getLogger(ProjectService.class);

@Autowired @Qualifier("projectRepository")
private ProjectRepository projectRepository;

@Transactional(value="xp")
public Project saveProject(Project project) {
return projectRepository.save(project);
}

@Transactional(value="xp")
public void deleteProject(Long projectSK) {
logger.debug("Deleting project with id: " + projectSK);
Project deleted = projectRepository.findOne(projectSK);
if (deleted == null) {
logger.debug("No project found with id: " + projectSK);
} else {
projectRepository.delete(projectSK);
logger.debug("Project deleted with id: " + projectSK);
}
}

@Transactional(value="xp")
public Project getProjectByProjectSk(Long projectSK) {
return projectRepository.findOne(projectSK);
}

@Transactional(value="xp")
public List<Project> getAllProjects() {
return (List<Project>) projectRepository.findAll();
}

@Transactional(value="xp")
public Page<Project> getAllProjects(Pageable pageable) {
return projectRepository.findAll(pageable);
}

@Transactional(value="xp")
public List<Project> getProjectsByDisease(String disease) {
return projectRepository.findByDisease(disease);
}


}

我的模型模块中的存储库定义如下:

package com.mycompany.rd.repository.misf;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import com.mycompany.rd.model.misf.Project;

@Repository
public interface ProjectRepository extends PagingAndSortingRepository<Project, Long>{


@Query("select p from Project p where p.disease = :disease order by p.projectNm")
public List<Project> findByDisease(
@Param("disease") String disease);

public Page<Project> findAll(Pageable pageable);

}

快把我逼疯了。任何建议表示赞赏。

最佳答案

问题似乎是因为我将 persistence.xml 导入到 appContext.xml 文件中。我被告知的 persistence.xml 文件不是有效的 spring 配置文件。即它在我的实例中使用持久性 xlmns,我正在导入到 bean xmlns 文件中。

我现在正在这样做(感谢 SO!),而不是“导入资源”——而且我的 beans 现在似乎正在连接。

<bean id="pum"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list>
<value>classpath*:META-INF/persistence.xml</value>
</list>
</property>
</bean>

谢谢大家。

关于java - 多模块项目中 JPA 存储库的 @Autowire 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25557906/

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