Spring JpaConfiguration在运行服务器时提供异常

时间:2013-11-29 12:44:19

标签: java spring jpa

我尝试配置JpaConfiguration以使用jetty和H2(仅用于测试)。 当我运行服务器时,我收到一个错误。但我不明白原因。

我正在使用spring-data-jpa。

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "my.package.model.repository")
@ComponentScan(basePackages = {"my.package.model.services"})
public class JpaConfiguration {

    @Bean
    public DataSource dataSource() throws SQLException {
        EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
        return builder.setType(EmbeddedDatabaseType.H2).build();
    }

    @Bean
    public EntityManagerFactory entityManagerFactory() throws SQLException {

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(true);

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan("my.package.model.entity");
        factory.setDataSource(dataSource());
        factory.afterPropertiesSet();
        factory.setPersistenceUnitName("test");
        return factory.getObject();
    }

    @Bean
    public EntityManager entityManager(EntityManagerFactory entityManagerFactory) {
        return entityManagerFactory.createEntityManager();
    }

    @Bean
    public PlatformTransactionManager transactionManager() throws SQLException {
        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(entityManagerFactory());
        return txManager;
    }

    @Bean
    public HibernateExceptionTranslator hibernateExceptionTranslator() {
        return new HibernateExceptionTranslator();
    }

}

错误是:

2013-11-29 13:36:33.042:WARN::Nested in org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'memberService': Injection o
f autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private my.package.model.repository.MemberRepository my.package.model.services.MemberServiceImpl.memberRepository; nested exception is org.springframework.beans.f
actory.BeanCreationException: Error creating bean with name 'memberRepository': Cannot create inner bean '(inner bean)' of type [org.springframework.orm.jp
a.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Err
or creating bean with name '(inner bean)#1': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception i
s org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [my/package/config/JpaConfiguration.class]: No matching factory method found: factory bean 'jpaConfiguration'; factory method 'entityManagerFactory()'. Check tha
t a method with the specified name exists and that it is non-static.:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [my/package/config/JpaConfiguration.class]: No matching factory method found: factory bean 'jpaConfiguration'; factory method 'entityManagerFactory()'. Check that
a method with the specified name exists and that it is non-static.
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:536)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.ja
va:1055)

非常感谢你的帮助。

2 个答案:

答案 0 :(得分:0)

您可以通过更简单的方式执行此操作:您需要使用LocalContainerEntityManagerFactoryBean作为返回类型的单个方法。或者,您可以在任何地方注入EntityManager bean,而不需要任何其他大惊小怪。

public class JpaConfiguration {

@Bean
public DataSource dataSource() throws SQLException {
    EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
    return builder.setType(EmbeddedDatabaseType.H2).build();
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws SQLException {

    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setGenerateDdl(true);

    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setJpaVendorAdapter(vendorAdapter);
    factory.setPackagesToScan("my.package.model.entity");
    factory.setDataSource(dataSource());
    factory.afterPropertiesSet();
    factory.setPersistenceUnitName("test");
    return factory;
}

//remainder configuration for trasnactions and so on
}


@Service
public class Service{
    @PersistenceContext(unitName = "test")
    private EntityManager em;
}

答案 1 :(得分:0)

问题来自我的web.xml文件。

配置必须是内容配置类而不是包:

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>my.package.WebConfiguration</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

Importatn行是:

<param-value>my.package.WebConfiguration</param-value>

我只写了

<param-value>my.package</param-value>