1

我正在尝试使用 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;
  }
}

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

有什么提示吗?

4

1 回答 1

1

您可能需要注册容器或进行扫描。看这里(点我)

于 2012-12-27T23:26:16.190 回答