JavaConfig:OpenEntityManagerInViewFilter找不到EntityManagerFactory

时间:2012-12-27 23:21:19

标签: spring

我正在尝试使用Spring JavaConfig获取我的spring-application xml-free。

现在我面临以下错误请求网页:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0
    at org.springframework.beans.factory.BeanFactoryUtils.beanOfType(BeanFactoryUtils.java:394)
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.findEntityManagerFactory(EntityManagerFactoryUtils.java:111)
    at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.lookupEntityManagerFactory(OpenEntityManagerInViewFilter.java:229)
    at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.lookupEntityManagerFactory(OpenEntityManagerInViewFilter.java:205)
    at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:152)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

我的持久性配置:

@Configuration
@EnableTransactionManagement
public class PersistenceConfig implements TransactionManagementConfigurer
{

  @Bean(name = "dataSource")
  public DataSource dataSource()
  {
    try
    {
      return (DataSource) new JndiTemplate().lookup("java:comp/env/jdbc/my-db");
    }
    catch (NamingException e)
    {
      throw new ApplicationConfigurationException("db-context not found", e);
    }
  }

  @Bean(name = "entityManagerFactory")
  public EntityManagerFactory entityManagerFactory()
  {
    LocalContainerEntityManagerFactoryBean lcemfb = new LocalContainerEntityManagerFactoryBean();
    lcemfb.setDataSource(dataSource());
    lcemfb.setJpaDialect(new HibernateJpaDialect());
    lcemfb.setJpaVendorAdapter(jpaVendorAdapter());
    lcemfb.setPersistenceUnitName("persistenceUnit");
    lcemfb.setPersistenceXmlLocation("classpath:META-INF/persistence.xml");
    lcemfb.afterPropertiesSet();
    return lcemfb.getObject();
  }

  @Bean(name = "jpaVendorAdapter")
  public JpaVendorAdapter jpaVendorAdapter()
  {
    HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
    jpaVendorAdapter.setShowSql(true);
    jpaVendorAdapter.setDatabase(Database.ORACLE);
    jpaVendorAdapter.setDatabasePlatform(Oracle10gDialect.class.getName());
    jpaVendorAdapter.setGenerateDdl(false);
    return jpaVendorAdapter;
  }

  /**
   * @see org.springframework.transaction.annotation.TransactionManagementConfigurer#annotationDrivenTransactionManager()
   */
  @Bean(name = "transactionManager")
  public PlatformTransactionManager annotationDrivenTransactionManager()
  {
    JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
    jpaTransactionManager.setEntityManagerFactory(entityManagerFactory());
    return jpaTransactionManager;
  }
}

目前,应用程序中的数据库访问工作正常。

任何提示?

1 个答案:

答案 0 :(得分:1)

您可能需要注册容器或进行扫描。见(Click me)