当前位置: 首页 > 知识库问答 >
问题:

带有Spring批处理和jpa配置的Spring Boot

唐照
2023-03-14
https://github.com/soasathish/spring-batch-with-jpa.git

批处理配置具有spring作业,只有一个步骤

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

WRITER使用SPRING DATA JPA写入DB

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);
            }


    }

共有1个答案

鲁鹏
2023-03-14

基于您提供的代码和注释中的堆栈跟踪。

它在抱怨找不到名为EntityManagerFactory@bean

发生这种情况的原因是您使用了@enablejparepositories,而EntityManagerFactoryRef属性默认为EntityManagerFactory。该属性为EntityManagerFactory定义@bean的名称。

@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);
    }

}

batchconfig上,您所需要的只是@configuration@enablebatchprocessing

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

更新:

 类似资料:
  • null 问题出在我希望启动Spring批处理应用程序时。当阅读这里的说明http://static.springsource.org/spring-batch/reference/html/configurejob.html#runningjobsfromcommandline时,它假设每个作业都有一个Spring配置文件,但我不希望这样做。 如何从命令行运行使用多个bean配置文件的Sprin

  • 我有一个带有两个数据库的Spring Batch应用程序:一个SQLDB用于Spring Batch元数据,另一个是存储所有业务数据的MongoDB。关系DB仍然使用。但是我不认为Mongo写入是在带有回滚的活动事务中完成的。以下是上官方Spring Batch留档的摘录: ItemWriter实现,使用Spring数据的MongoOperations实现写入MongoDB存储。由于MongoDB

  • 我们正在处理一个Spring批处理项目(Spring Boot1.2.2.Release),要求使用Spring SFTP集成以一定频率轮询从服务器位置读取文件。我们使用java config实现了Spring批处理,并在使用Spring Integration java config的过程中实现了Spring批处理。我找不到描述上述情况的例子。我浏览了各种链接,但看到的主要是XML配置示例。 h

  • 我有下一个spring批处理配置类: 启动应用程序时,我收到下一个异常:

  • 我正在尝试创建一个Spring批处理应用程序。我们使用SQL Anywhere数据库,它实际上是,一种已知的数据库类型。为了简化操作,我在主类上使用了,在配置类上使用了。 问题是,我的数据库驱动程序返回一个产品名“SQL Anywhere”,而Spring无法识别这个名称,从而导致各种错误。通过在配置类中显式创建JobRepositoryFactoryBean,我能够克服其中的一些问题: 但是,在

  • 我正在将Spring Boot项目与Spring批处理和数据jpa项目集成。所有与作业和数据配置相关的东西都是正确的,除了将我的作业编写器结果保存在数据库中。在我读取文件并对其进行处理后,我无法将其写入mysql数据库。没有错误,但也没有插入。有趣的是我的数据源已配置。因为在插入之前,我可以从数据库中获取示例记录。请协助我解决这个问题。 我的申请。属性: 批次配置: 道类: 作家类: temPer