在这里查看了几个类似的问题并将逻辑应用于我的情况后,我仍然对我的 Spring/JPA 应用程序中的依赖注入失败感到困惑。申请的前提是:
- 使用基于注释的 JPA2 减少 xml 配置;
- 根据此处的参考教程,使用 @Autowire 为每个 DAO 类型从单个接口/实现自动生成 DAO ;
这是致命错误:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'exampleInstanceBO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.***.dao.IGenericDAO com.***.bo.ExampleInstanceBO.dao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.***.dao.IGenericDAO] 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=IGenericDAO)}
...这是由以下原因引起的:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.***.dao.IGenericDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
它在以下服务类中调用:
实例实例BO:
导入 org.springframework.beans.factory.annotation.Autowired; 导入 org.springframework.beans.factory.annotation.Qualifier; 导入 org.springframework.stereotype.Service; 进口com.***.dao.*; 导入 com.***.entity.*; @服务 公共类 ExampleInstanceBO { @自动连线 @Qualifier(value="IGenericDAO") IGenericDAO 道; 公共无效 setDao(最终 IGenericDAO daoToSet){ dao = daoToSet; dao.setClazz( ExampleInstance.class ); } // }
IGenericDAO 如下:
IGenericDAO:
导入 org.springframework.stereotype.Component; 导入 org.springframework.stereotype.Service; @Component("IGenericDAO") 公共接口 IGenericDAO { public void setClazz(final Class clazzToSet); public T findById(final Long id); 公共列表 findAll(); 公共无效保存(最终T实体); 公共无效更新(最终 T 实体); 公共无效删除(最终T实体); public void deleteById( final Long entityId ); }
其实现如下:
通用DAO
导入 java.io.Serializable; 导入 org.springframework.context.annotation.Scope; 导入 org.springframework.stereotype.Repository; @Repository @Scope("原型") 公共类 GenericDAO 扩展 AbstractDAO 实现 IGenericDAO{ //空,因为抽象类AbstractDAO中的大部分功能 }
抽象道
导入 java.io.Serializable; 导入 java.util.List; 导入 javax.persistence.EntityManager; 导入 javax.persistence.PersistenceContext; 导入 org.springframework.stereotype.Service; /** * @作者 - - * */ @服务 公共抽象类 AbstractDAO { 私人类 clazz; @PersistenceContext 实体管理器实体管理器; 公共无效 setClazz(最终类 clazzToSet){ this.clazz = clazzToSet; } public T findById(final Long id){ 返回 entityManager.find(clazz, id); } 公共列表 findAll(){ return entityManager.createQuery("来自" + clazz.getName()) .getResultList(); } // 其他保存/删除/更新等方法在这里 }
以上是基于这里的讨论,在配置和依赖项方面有很多是理所当然的。这是我的配置和 pom 文件,它们很可能导致了混乱,因为它们在我试图解决这个问题时已经进化了。
beanDefinitions.xml
<jpa:repositories base-package="com.***" />
<context:component-scan base-package="com.***" />
<bean class="org.springframework.orm.jpa.JpaTransactionManager"
id="transactionManager">
<property name="entityManagerFactory"
ref="entityManagerFactory" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="***" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true" />
<property name="database" value="HSQL" />
</bean>
</property>
</bean>
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>database.properties</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
</beans>
最后是pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>maven_projects</groupId>
<artifactId>***</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>***</name>
<description>spring 3 plus maven</description>
<packaging>war</packaging>
<properties>
<org.springframework.version>3.1.3.RELEASE</org.springframework.version>
<hibernate.version>4.1.0.Final</hibernate.version>
<mysql.version>5.1.13</mysql.version>
<junit.version>4.7</junit.version>
<slf4j.version>1.7.0</slf4j.version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<!-- ORM/DB -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
<!-- perhaps using scope = provided, as this will often
be present on the app server -->
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<!-- or hibernate-entitymanager if you use jpa -->
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
<!--
default Jave EE jars don't include code necessary for
bytecode enhancement so we use these instead -->
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-6.0</artifactId>
<version>1.0.0.Final</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
<!-- Jersey (RESTful web services -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
<!-- Jersey + Spring -->
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-spring</artifactId>
<version>1.8</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
<!--
Required for J2EE dependencies, see here:
http://www.ninthavenue.com.au/java-lang-classformaterror-exception-with-eclipselink-static-weaving-solved
-->
<repositories>
<repository>
<id>repository.jboss.org-public</id>
<name>JBoss repository</name>
<url>https://repository.jboss.org/nexus/content/groups/public</url>
</repository>
</repositories>
</project>
** 更新 **
谢谢,试过了,GenericDAO 现在看起来像这样:
// @Component("GenericDAO") @Repository("GenericDAO") @Scope("原型") 公共类 GenericDAO 扩展 AbstractDAO 实现 IGenericDAO{ // }
使用 Component 或 Repository 注释,仍然是相同的结果。查看日志输出,生成的bean如下:
... GenericDAO ... 但它正在寻找 IGenericDAO - 当我更改为 @Repository("IGenericDAO") 时,它会引发空指针异常, - 困惑!
更新 #2 - 数据源配置:
<?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.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<!--<jpa:repositories base-package="***" />-->
<context:component-scan base-package="***" />
<bean class="org.springframework.orm.jpa.JpaTransactionManager"
id="transactionManager">
<property name="entityManagerFactory"
ref="entityManagerFactory" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="***" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true" />
<property name="database" value="HSQL" />
</bean>
</property>
</bean>
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>database.properties</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
</beans>