@Autowire在多模块项目中失败了JPA Repository

时间:2014-08-28 20:58:52

标签: java spring spring-data

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

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

  

没有类型的限定bean   找到[com.mycompany.rd.repository.misf.ProjectRepository]   依赖:预计至少有1个bean有资格成为autowire   这种依赖的候选人。依赖注释:   {@ org.springframework.beans.factory.annotation.Autowired(所需=真),   @ org.springframework.beans.factory.annotation.Qualifier(值= 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来自Model模块的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);

}

让我疯了。任何建议表示赞赏。

2 个答案:

答案 0 :(得分:1)

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

我现在正在这样做(感谢SO!)而不是“导入资源” - 而我的bean现在正在接线。

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

谢谢大家。

答案 1 :(得分:0)

尝试将所有必要的包放入组件扫描中

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

也许你可以使用注释@Component