2015-09-27 2 views
0

Недавно я начал изучать весну. Я пробую простой пример с весенними данными jpa в проекте весеннего mvc. Я получаю следующую ошибку при развертывании военного файла в tomcat.Проблема с пружинными данными JPA - BeanEntityManagerFactory

Caused by: org.springframework.beans.factory.BeanCreationException: Error creati 
ng bean with name '(inner bean)#584d15f2': Cannot resolve reference to bean 'ent 
ityManagerFactory' while setting constructor argument; nested exception is org.s 
pringframework.beans.factory.BeanCurrentlyInCreationException: Error creating be 
an with name 'entityManagerFactory': Requested bean is currently in creation: Is 
there an unresolvable circular reference? 
     at org.springframework.beans.factory.support.BeanDefinitionValueResolver 
.resolveReference(BeanDefinitionValueResolver.java:359) 
     at org.springframework.beans.factory.support.BeanDefinitionValueResolver 
.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108) 
     at org.springframework.beans.factory.support.ConstructorResolver.resolve 
ConstructorArguments(ConstructorResolver.java:634) 
     at org.springframework.beans.factory.support.ConstructorResolver.instant 
iateUsingFactoryMethod(ConstructorResolver.java:444) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBean 
Factory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:11 
19) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBean 
Factory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1014) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBean 
Factory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBean 
Factory.createBean(AbstractAutowireCapableBeanFactory.java:476) 
     at org.springframework.beans.factory.support.BeanDefinitionValueResolver 
.resolveInnerBean(BeanDefinitionValueResolver.java:299) 
     ... 92 more 
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: E 
rror creating bean with name 'entityManagerFactory': Requested bean is currently 
in creation: Is there an unresolvable circular reference? 
     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistr 
y.beforeSingletonCreation(DefaultSingletonBeanRegistry.java:347) 
     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistr 
y.getSingleton(DefaultSingletonBeanRegistry.java:223) 
     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBe 
an(AbstractBeanFactory.java:299) 
     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean 
(AbstractBeanFactory.java:194) 
     at org.springframework.beans.factory.support.BeanDefinitionValueResolver 
.resolveReference(BeanDefinitionValueResolver.java:351) 
     ... 100 more 
27-Sep-2015 17:56:45.304 INFO [http-apr-8080-exec-35] org.apache.catalina.startu 
p.HostConfig.deployWAR Deployment of web application archive D:\ApacheTomcat\apa 
che-tomcat-8.0.26\webapps\springTest.war has finished in 6,124 ms 

Мой контроллер код выглядит следующим образом,

package com.demo.repo; 

import com.demo.model.Customer; 

import java.text.DateFormat; 
import java.util.Date; 
import java.util.Locale; 
import java.util.Properties; 

import javax.activation.DataSource; 
import javax.persistence.EntityManagerFactory; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.core.env.Environment; 
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 
import org.springframework.orm.jpa.JpaTransactionManager; 
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; 
import org.springframework.stereotype.Controller; 
import org.springframework.transaction.annotation.EnableTransactionManagement; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

import com.zaxxer.hikari.HikariConfig; 
import com.zaxxer.hikari.HikariDataSource; 

/** 
* Handles requests for the application home page. 
*/ 
@Controller 
@Configuration 
@EnableJpaRepositories("com.demo.repo") 


@EnableTransactionManagement 
public class HomeController { 

    @Autowired 
    customerRepository repository; 

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class); 

    /** 
    * Simply selects the home view to render by returning its name. 
    */ 

    @Bean(destroyMethod = "close") 
    DataSource dataSource(Environment env) { 
    HikariConfig dataSourceConfig = new HikariConfig(); 
    dataSourceConfig.setDriverClassName(env.getRequiredProperty("db.driver")); 
    dataSourceConfig.setJdbcUrl(env.getRequiredProperty("db.url")); 
    dataSourceConfig.setUsername(env.getRequiredProperty("db.username")); 
    dataSourceConfig.setPassword(env.getRequiredProperty("db.password")); 

    return (DataSource) new HikariDataSource(dataSourceConfig); 
    } 


    @Bean 
    LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, 
                  Environment env) { 
    LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); 
    entityManagerFactoryBean.setDataSource((javax.sql.DataSource) dataSource); 
    entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); 
    entityManagerFactoryBean.setPackagesToScan("com.demo.repo"); 

    Properties jpaProperties = new Properties(); 

    //Configures the used database dialect. This allows Hibernate to create SQL 
    //that is optimized for the used database. 
    jpaProperties.put("hibernate.dialect", env.getRequiredProperty("hibernate.dialect")); 

    //Specifies the action that is invoked to the database when the Hibernate 
    //SessionFactory is created or closed. 
    jpaProperties.put("hibernate.hbm2ddl.auto", 
      env.getRequiredProperty("hibernate.hbm2ddl.auto") 
    ); 

    //Configures the naming strategy that is used when Hibernate creates 
    //new database objects and schema elements 
    jpaProperties.put("hibernate.ejb.naming_strategy", 
      env.getRequiredProperty("hibernate.ejb.naming_strategy") 
    ); 

    //If the value of this property is true, Hibernate writes all SQL 
    //statements to the console. 
    jpaProperties.put("hibernate.show_sql", 
      env.getRequiredProperty("hibernate.show_sql") 
    ); 

    //If the value of this property is true, Hibernate will format the SQL 
    //that is written to the console. 
    jpaProperties.put("hibernate.format_sql", 
      env.getRequiredProperty("hibernate.format_sql") 
    ); 

    entityManagerFactoryBean.setJpaProperties(jpaProperties); 

    return entityManagerFactoryBean; 
    } 


    @Bean 
    JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { 
     JpaTransactionManager transactionManager = new JpaTransactionManager(); 
     transactionManager.setEntityManagerFactory(entityManagerFactory); 
     return transactionManager; 
    } 

    @RequestMapping(value = "/", method = RequestMethod.GET) 
    public String home(Locale locale, Model model) { 
     logger.info("Welcome home! The client locale is {}.", locale); 

     Date date = new Date(); 
     DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); 

     String formattedDate = dateFormat.format(date); 

     model.addAttribute("serverTime", formattedDate); 

     repository.save(new Customer("Jack", "Bauer")); 
     repository.save(new Customer("Chloe", "O'Brian")); 
     repository.save(new Customer("Kim", "Bauer")); 
     repository.save(new Customer("David", "Palmer")); 
     repository.save(new Customer("Michelle", "Dessler")); 

     for(Customer customer : repository.findAll()) 
     { 
      System.out.println("Log Results :: "+customer.toString()); 
     } 

     return "myhome"; 
    } 

} 

Может кто-нибудь предложить мне, что случилось с моим кодом и любые предложения по решению той же.

+1

Вы создаете 1 боб, чтобы управлять ими всеми. Ваш контроллер также является вашей конфигурацией, не делайте этого. Разделите контроллер и конфигурацию в двух отдельных классах. Это разные вещи, которые вы не должны сочетать. –

ответ

2

Похоже, что для вашего entityManagerFactory требуется dataSource, который определен в том же файле конфигурации.

Попробуйте переместить определение dataSource к другому классу конфигурации, или, вместо передачи dataSource в качестве параметра, просто вызовите метод dataSource(), когда вам это нужно в entityManagerFactory.

@Autowired 
Environment env; 

@Bean 
LocalContainerEntityManagerFactoryBean entityManagerFactory() { 
    LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = 
        new LocalContainerEntityManagerFactoryBean(); 
    entityManagerFactoryBean.setDataSource((javax.sql.DataSource) dataSource()); 
    .... 
} 

СОВЕТ: Не следует смешивать ваши @Controller и @Configuration. Создайте другой файл для каждого из них.

+0

Вызов метода будет работать, только если конфигурация находится в том же классе, иначе вы можете легко ввести его в качестве аргумента метода. Таким образом, если вы хотите, вы можете отделить настройки JDBC и JPA. Какой из них лучше - это личное предпочтение imho :). –