【问题标题】:Spring boot with spring batch and jpa -configuration带有弹簧批处理和 jpa 配置的 Spring Boot
【发布时间】:2017-12-21 19:29:08
【问题描述】:

我有简单的批处理应用程序,将 csv 读取到 postgres 数据库。

我已在 bitbucket 中的以下 repo 中上传了代码

https://github.com/soasathish/spring-batch-with-jpa.git

我在使用 Spring Data JPA 配置写入数据库时​​遇到问题。 我得到管理 bean not found .issue。

当我尝试与 spring 批处理集成时,相同的 jpa spring 数据配置在不同的项目中工作,它失败并找不到管理 bean。

批处理配置有弹簧作业 只有一步

1) 阅读器 - 从 csv 文件中读取。 处理器对文件应用一些规则.. Drools 请运行 schema-postgresql.sql 来设置数据库

WRITER 使用 SPRING DATA JPA 写入 DB

可以帮忙吗 我已经在 bitbucket 的下面这个 repo 中上传了代码


https://github.com/soasathish/spring-batch-with-jpa.git

我知道这是一个小问题,但任何方向或帮助将不胜感激

用于创建 repo 的代码

========================

package uk.gov.iebr.batch.config;

import static uk.gov.iebr.batch.config.AppProperties.DRIVER_CLASS_NAME;

import static uk.gov.iebr.batch.config.AppProperties.IEBR_DB_PASSWORD_KEY;
import static uk.gov.iebr.batch.config.AppProperties.IEBR_DB_URL_KEY;
import static uk.gov.iebr.batch.config.AppProperties.IEBR_DB_USER_KEY;

import java.util.Properties;
import javax.sql.DataSource;

import org.hibernate.jpa.HibernatePersistenceProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@PropertySource({"classpath:application.properties"})
@EnableJpaRepositories({"uk.gov.iebr.batch.repository"})
@EnableTransactionManagement
@ComponentScan(basePackages="uk.gov.iebr.batch.repository")
public class DataSourceConfiguration {


    @Autowired
    Environment env;




    @Bean(name = "allsparkEntityMF")
    public LocalContainerEntityManagerFactoryBean allsparkEntityMF() {
        final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(allsparkDS());
        em.setPersistenceUnitName("allsparkEntityMF");
        em.setPackagesToScan(new String[] { "uk.gov.iebr.batch"});
        em.setPackagesToScan(new String[] { "uk.gov.iebr.batch.repository"});
        em.setPersistenceProvider(new HibernatePersistenceProvider());

        HibernateJpaVendorAdapter a = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(a);
        Properties p = hibernateSpecificProperties();
        p.setProperty("hibernate.ejb.entitymanager_factory_name", "allsparkEntityMF");
        em.setJpaProperties(p);
        return em;
    }

    @Bean(name = "allsparkDS")
    public DataSource allsparkDS() {

        final DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty(DRIVER_CLASS_NAME));
        dataSource.setUrl(env.getProperty(IEBR_DB_URL_KEY));
        dataSource.setUsername(env.getProperty(IEBR_DB_USER_KEY));
        dataSource.setPassword(env.getProperty(IEBR_DB_PASSWORD_KEY));

        return dataSource;
    }

    @Bean
    public Properties hibernateSpecificProperties(){

        final Properties p = new Properties();
        p.setProperty("hibernate.hbm2ddl.auto", env.getProperty("spring.jpa.hibernate.ddl-auto"));
        p.setProperty("hibernate.dialect", env.getProperty("spring.jpa.hibernate.dialect"));
        p.setProperty("hibernate.show-sql", env.getProperty("spring.jpa.show-sql"));
        p.setProperty("hibernate.cache.use_second_level_cache", env.getProperty("spring.jpa.hibernate.cache.use_second_level_cache"));
        p.setProperty("hibernate.cache.use_query_cache", env.getProperty("spring.jpa.hibernate.cache.use_query_cache"));

        return p;

    }

    @Bean(name = "defaultTm")
    public PlatformTransactionManager transactionManager() {

        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(allsparkEntityMF().getObject());
        return txManager;
    }

}

批处理配置文件:

package uk.gov.iebr.batch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;

import uk.gov.iebr.batch.config.AllSparkDataSourceConfiguration;
import uk.gov.iebr.batch.config.DataSourceConfiguration;
import uk.gov.iebr.batch.dao.PersonDao;
import uk.gov.iebr.batch.model.Person;
import uk.gov.iebr.batch.step.Listener;
import uk.gov.iebr.batch.step.Processor;
import uk.gov.iebr.batch.step.Reader;
import uk.gov.iebr.batch.step.Writer;

@Configuration
@EnableBatchProcessing
//spring boot configuration
@EnableAutoConfiguration
//file that contains the properties
@PropertySource("classpath:application.properties")
@Import({DataSourceConfiguration.class, AllSparkDataSourceConfiguration.class})
public class BatchConfig {

    private static final Logger log = LoggerFactory.getLogger(BatchConfig.class);
    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    public PersonDao PersonDao;

    @Autowired
    public DataSourceConfiguration dataSourceConfiguration;

    @Bean
    public Job job() {
        long startTime = System.currentTimeMillis();
        log.info("START OF BATCH ========================================================================" +startTime);
        return jobBuilderFactory.get("job").incrementer(new RunIdIncrementer())
                //.listener(new Listener(PersonDao))
                .flow(step1()).end().build();
    }

    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1").<Person, Person>chunk(10)
                .reader(Reader.reader("tram-data.csv"))
                .processor(new Processor()).writer(new Writer(PersonDao)).build();
    }


}

Writer 称之为 PersonDaoImpl:

public class PersonDaoImpl  implements PersonDao {



    @Autowired
    DataSourceConfiguration dataSource;

    @Autowired
    PersonRepository personrepo;

    @Override
    public void insert(List<? extends Person> Persons) {
        personrepo.save(Persons);
            }


    }

【问题讨论】:

  • 请检查这里,您只需替换您的数据源配置和依赖项,并将 JDBCItemWriter 替换为 HibernateItemWriter walkingtechie.blogspot.in/2017/03/…
  • @Karthik Prasad,我正在尝试转换此站点中的示例。 walkingtechie.blogspot.co.uk/2017/03/… 在将应用程序转换为 Spring Data JPA 时,我遇到了上述问题。
  • 请提供错误和发生的任何堆栈跟踪。
  • 请看下面的日志。 italic bold 11:42:03.325 [main] INFO osojLocalContainerEntityManagerFactoryBean - 为持久性单元“allsparkEntityMF”关闭 JPA EntityManagerFactory 线程“main”org.springframework.beans.factory 中的异常.BeanCreationException:创建名为“personRepository”的bean时出错:设置bean属性“entityManager”时无法创建类型的内部bean“(内部bean)#7bde1f3a”;引起:org.springframework.beans.factory.NoSuchBeanDefinitionE 异常:没有定义名为“entityManagerFactory”的bean
  • @Michael Minella - 您能否分享一个带有 Spring Data JPA 的 Spring Batch 基本示例的链接

标签: spring hibernate spring-boot spring-data-jpa spring-batch


【解决方案1】:

基于您提供的代码和您评论中的堆栈跟踪。

它抱怨找不到名为entityManagerFactory@Bean

发生这种情况的原因是因为您使用的是@EnableJpaRepositories,而entityManagerFactoryRef 属性默认为entityManagerFactory。此属性为EntityManagerFactory 定义@Bean 的名称。

我认为您的应用程序配置正在阻止处理 spring-boot 的正常自动配置。

我建议删除 IEBRFileProcessApplication 类并按照此示例配置您的 spring-boot 应用程序(如果您愿意,可以使用 ServletInitializer)。

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

}

我也看不出需要DataSourceConfigurationAllSparkDataSourceConfiguration,所以我建议删除它们。如果您确实需要指定自己的DataSource,请告诉我,我可以提供额外的示例。

@SpringBootApplication@EnableBatchProcessing 注释之间,所有必要的东西都将为您引导。

您在BatchConfig 上只需要@Configuration@EnableBatchProcessing

如果您进行这些更改以简化您的代码库,那么您的问题应该会消失。

更新:

我在这里创建了一个拉取请求 https://github.com/soasathish/spring-batch-with-jpa/pull/1

请查看这里的 javadoc,了解@EnableBatchProcessing 的工作原理。 http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/core/configuration/annotation/EnableBatchProcessing.html

【讨论】:

  • 能否请您发送 repo url 以获取其他示例
  • 通过拉取请求更新答案并推荐更改。
  • 我收到了拉取请求,我没有看到 git 分支在工作。
  • 我收到了拉取请求,我没有看到 git 分支在工作。我尝试了所有建议,删除了 DataSourceConfiguration 和 AllSparkDataSourceConfiguration ..Stillf 问题。
    您能否发送示例 **
    如上所述 **您建议您可以提供其他示例
  • 如前所述,您能否将 git repo 发送给我,以获取使用 spring batch 实现 spring data jpa 的示例
猜你喜欢
  • 1970-01-01
  • 2018-10-14
  • 1970-01-01
  • 1970-01-01
  • 2020-06-18
  • 2015-02-08
  • 2022-12-05
  • 2018-03-08
  • 1970-01-01
相关资源
最近更新 更多