简体   繁体   中英

Any way to override bean definition for spring data jpa repository?

We have a sample app that showcases some things we are doing client side. It pulls in a few internal libraries to get everything functional. It dummies in some hard-coded data so it doesn't have to be concerned with any kind of persistence mechanism.

In some of these libraries that are pulled in, there are spring data jpa repositories. Something like this:

public interface MyLibraryEntityRepository extends JpaRepository<MyLibraryEntity, Long>
{
   //...
}

When the server starts up, I get an error like this:

 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myLibraryEntityRepository': Cannot create inner bean '(inner bean)#788f64f1' 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)#788f64f1': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined

It can't find the entityManager , but I don't want to have to use an entityManager . So, in an attempt to override the myLibraryEntityRepository bean, I added the following to my Java config:

@Bean
public MyLibraryEntityRepository getMyLibraryEntityRepository()
{
    return myDummyImpl();
}

However, this results in the same error.

Is there any way I can override the bean definition for the spring data jpa repository so that I can use my own dummy implementation and not have to configure an entityManager in my app?

您可以使用@Bean(name =“ dummyBean”),在@Autowired中使用注释@Qualifier(“ dummyBean”)

Why don't use create an entityManager on an in memory transient database like h2 ? This way, during bootstrapping the application you can load all your demo data with a simple sql script, too. No hardcoded demo data. No modifications on your code.

Here's an excerpt from my database configuration dedicated for integration tests of my Spring Data app.

@Profile("test")
@Configuration
public class TestDataSourceConfig {

    // ... entity Manager

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("org.h2.Driver");
        dataSource.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1");
        dataSource.setUsername("");
        dataSource.setPassword("");
        return dataSource;
    }

    public JpaVendorAdapter jpaVendorAdapter() {
        return  new HibernateJpaVendorAdapter() {
            {
                setDatabasePlatform("org.hibernate.dialect.H2Dialect");
            }
        };
    }

    public Properties jpaProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.hbm2ddl.auto","create");
        return properties;
    }

    @Bean
    @DependsOn("entityManagerFactory")
    public ResourceDatabasePopulator initDatabase(DataSource dataSource) throws Exception {
        ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
        populator.addScript(new ClassPathResource("test-data.sql"));
        populator.populate(dataSource.getConnection());
        return populator;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM