Spring jpa BeanCreationException:在设置构造函数参数时无法解析对bean“entityManagerFactory”的引用;

时间:2017-02-11 21:59:11

标签: maven spring-mvc spring-data-jpa hibernate-entitymanager

我是Spring Data JPA的新手。我正在尝试使用Spring Data JPA配置我的maven webapp项目2天,现在我被卡住了,因为我遇到了实体工厂经理的恼人错误。我已经验证了我的配置,但仍然无法理解为什么我的LogicImpl无法自动装配存储库并导致内部bean异常。我觉得有些东西不见了,但我无法弄明白。 我想在我的代码中使用spring transaction templete,如果不在我的logicImpl中使用我的存储库接口,它工作正常 我正在附加我的配置和代码文件      ApplicationConfiguration.java

@Configuration
@EnableJpaRepositories("com.itob.persistence.repository")
@EnableTransactionManagement
public class ApplicationConfiguration {
@Autowired
private DataSource dataSource;
@Bean
public JdbcTemplate jdbcTemplate(){return new JdbcTemplate(dataSource);}
@Bean
public DataSource dataSource(){
    BasicDataSource dataSource =new BasicDataSource();
    dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
    dataSource.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
    dataSource.setUsername("hr");dataSource.setPassword("system");return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager() {
    EntityManagerFactory factory = entityManagerFactory();
    return new JpaTransactionManager(factory);
}
@Bean
public EntityManagerFactory entityManagerFactory() {
    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setGenerateDdl(Boolean.TRUE);
    vendorAdapter.setShowSql(Boolean.TRUE);
    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setJpaVendorAdapter(vendorAdapter);
    factory.setPackagesToScan("com.itob.persistence.model");
    factory.setDataSource(dataSource());
    factory.afterPropertiesSet();
    factory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
    return factory.getObject();
}

DepartmentLogicImpl.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public final class DepartmentLogicImpl implements IDepartmentLogic {

 @Autowired
 private DepartmentDao departmentDaoHbnImpl;

 @Autowired
private IDepartmentConverter departmentConverterImpl;

 @Autowired
 private UserRepository userRepository;

 public DepartmentResponseBean getData() {


    Department department = departmentConverterImpl.convertRequest("AMR");
    int insertedRows = departmentDaoHbnImpl.saveDepartment(department);
    DepartmentResponseBean businessResponse = departmentConverterImpl.convertInsertedRecord(insertedRows);
    return businessResponse;
 }

 @Transactional
 @Override
 public long totalUsers() {
    return userRepository.count();
 }
}

的pom.xml

<!--Test-->
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>com.oracle</groupId>
  <artifactId>ojdbc6</artifactId>
  <version>11.2.0.3</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>4.2.0.RELEASE</version>
  <exclusions>
    <exclusion>
      <!--Exclude Commons Logging in favor of SLF4j-->
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
    </exclusion>
  </exclusions>
</dependency>

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-jpa</artifactId>
  <version>1.8.2.RELEASE</version>
</dependency>

<!--<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-dbcp2</artifactId>
</dependency>-->

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>4.3.5.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>4.2.0.RELEASE</version>
</dependency>

<!-- JPA Provider (Hibernate) -->
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-entitymanager</artifactId>
  <version>4.3.8.Final</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.javaconfig/spring-javaconfig -->
<dependency>
  <groupId>org.springframework.javaconfig</groupId>
  <artifactId>spring-javaconfig</artifactId>
  <version>1.0.0.m3</version>
</dependency>

<!--AspectJ-->
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjrt</artifactId>
  <version>1.8.6</version>
</dependency>

<!--Logging-->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>${org.self4j-version}</version>
</dependency>

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>jcl-over-slf4j</artifactId>
  <version>${org.self4j-version}</version>
  <scope>runtime</scope>
</dependency>

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>${org.self4j-version}</version>
  <scope>runtime</scope>
</dependency>

<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.17</version>
  <exclusions>
    <exclusion>
      <groupId>javax.mail</groupId>
      <artifactId>mail</artifactId>
    </exclusion>
    <exclusion>
      <groupId>javax.jms</groupId>
      <artifactId>jmxtools</artifactId>
    </exclusion>
    <exclusion>
      <groupId>com.sun.jmx</groupId>
      <artifactId>jmxri</artifactId>
    </exclusion>
  </exclusions>
  <scope>runtime</scope>
</dependency>

<!--@Inject-->
<dependency>
  <groupId>javax.inject</groupId>
  <artifactId>javax.inject</artifactId>
  <version>1</version>
</dependency>

<dependency>
  <groupId>javax.persistence</groupId>
  <artifactId>persistence-api</artifactId>
  <version>1.0</version>
</dependency>

<!--Servlet-->
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
  <version>2.5</version>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>javax.servlet.jsp</groupId>
  <artifactId>jsp-api</artifactId>
  <version>2.2</version>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>jstl</artifactId>
  <version>1.2</version>
</dependency>
<dependency>
  <groupId>org.codehaus.jackson</groupId>
  <artifactId>jackson-core-asl</artifactId>
  <version>1.9.7</version>
</dependency>
<dependency>
  <groupId>org.codehaus.jackson</groupId>
  <artifactId>jackson-mapper-asl</artifactId>
  <version>1.9.7</version>
</dependency>
<dependency>
  <groupId>commons-dbcp</groupId>
  <artifactId>commons-dbcp</artifactId>
  <version>1.4</version>
</dependency>

异常

org.springframework.beans.factory.BeanCreationException: Error creating bean
    with name 'departmentLogicImpl': Injection of autowired dependencies failed;
    nested exception is org.springframework.beans.factory.BeanCreationException:
    Could not autowire field: private       
    com.itob.persistence.repository.UserRepository
    com.itob.business.impl.DepartmentLogicImpl.userRepository; nested exception is
**org.springframework.beans.factory.BeanCreationException: Error creating bean
    with name 'userRepository':** Cannot create inner bean '(inner bean)#803be8a'
    of type **[org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager';** nested exception is org.springframework.beans.factory.BeanCreationException: 
    Error creating bean with name '(inner bean)#803be8a':Cannot resolve reference to bean **'entityManagerFactory'** while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in com.itob.configuration.ApplicationConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.**BeanInstantiationException: Failed to instantiate** [javax.persistence.EntityManagerFactory]: Factory method 'entityManagerFactory' threw exception; nested exception is javax.persistence.PersistenceException [PersistenceUnit: default] Unable to build Hibernate SessionFactory

1 个答案:

答案 0 :(得分:0)

你需要的是添加 @EnableJpaRepositories(basePackages="com.zxg.springdata",entityManagerFactoryRef="factoryBean")

您必须映射您的entityManagerFactory。