【发布时间】: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