Spring + GWT UnsatisfiedDependencyException

时间:2014-02-25 19:36:59

标签: java spring hibernate gwt

我几乎查看了关于org.springframework.beans.factory.UnsatisfiedDependencyException的每个主题,但我无法找到解决问题的方法。我的服务如下:

    @Service
public class PacientService {

    @Resource
    private PacientDAO dao;

    @Resource
    private PacientModelTransformer transformer;

    public PacientService() {

    }

    @Autowired
    public PacientService(PacientDAO dao, PacientModelTransformer transformer) {
        this.dao = dao;
        this.transformer = transformer;
    }

    public PacientDTO getPacientById(Long pacientId) {
        return transformer.toDTO(dao.readByPrimaryKey(pacientId));
    }
}    


<context:annotation-config />
<context:component-scan base-package="pl.arprojects.dietetyk" />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/d2" />
    <property name="username" value="root" />
    <property name="password" value="" />
</bean>
<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="generateDdl" value="true" />
            <property name="database" value="MYSQL" />
        </bean>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean
    class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

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

<hades:dao-config base-package="pl.arprojects.dietetyk.api.domian" />

这是我的applicationContext.xml。我真的不知道为什么我会有这样的例外:

  

org.springframework.beans.factory.UnsatisfiedDependencyException:在文件[D:\ Dietetyk \ Dietetyk \ target \ dietetyk-1.0.0-SNAPSHOT \ WEB-INF \ classes \ pl \中定义名称'pacientService'创建bean时出错arprojects \ dietetyk \ server \ service \ PacientService.class]:通过构造函数参数表达的不满意的依赖关系,类型为[pl.arprojects.dietetyk.api.dao.PacientDAO]的索引0 ::没有类型匹配的bean [pl.arprojects.dietetyk找到依赖项的.api.dao.PacientDAO]:预计至少有一个bean可以作为此依赖项的autowire候选者。依赖注释:{};嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为[pl.arprojects.dietetyk.api.dao.PacientDAO]的匹配bean依赖:预期至少有1个bean符合此依赖关系的autowire候选资格。依赖注释:{}

不,这个DAO没有任何子类,看起来完全像这样:

@Repository
public interface PacientDAO extends GenericDao<Pacient, Long> {

     @Query("Select * from dietetyk_pacients where name = :name")
     public Pacient getByName(@Param("name") String name);

     @Query("")
     public void deleteByPrimaryKey(@Param("id") long id);
}

从Hades Synyx扩展GenericDao

<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:hades="http://schemas.synyx.org/hades"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://schemas.synyx.org/hades http://schemas.synyx.org/hades/hades.xsd">
<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="generateDdl" value="true" />
            <property name="database" value="MYSQL" />
        </bean>
    </property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean
    class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean
    class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/d2" />
    <property name="username" value="root" />
    <property name="password" value="" />
</bean>
<hades:dao-config base-package="pl.arprojects.dietetyk.api.domian" />

的applicationContext.xml

1 个答案:

答案 0 :(得分:0)

对所有DAO类(包括基本DAO的子类)尝试@Repository@Component注释,无需在注释中定义名称。

依赖关系按类型解析。