NewCoder
NewCoder

Reputation: 81

Spring jpa BeanCreationException : Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument;

I'm new to Spring Data JPA. I'm trying to configure my maven webapp project with Spring Data JPA for 2 days and now i'm stuck because i'm getting annoying errors of entity factory manager. I've verified my configuration but still cannot understand why my LogicImpl is unable to autowire the repository and causing inner bean exception. I think there is something missing but i'm unable to figure it out. And i want to use spring transaction templete in my code as well which is working fine if do not use my repository interface in my logicImpl I'm attaching my configuration and code files 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>

Exception

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

Upvotes: 0

Views: 7023

Answers (1)

Zhang_Ucan
Zhang_Ucan

Reputation: 1

what you need is add @EnableJpaRepositories(basePackages="com.zxg.springdata",entityManagerFactoryRef="factoryBean")

You have to map your entityManagerFactory。

Upvotes: 0

Related Questions