自定义 FieldSetMapper 中忽略的自动装配 JpaRepository 保存
Posted
技术标签:
【中文标题】自定义 FieldSetMapper 中忽略的自动装配 JpaRepository 保存【英文标题】:Autowired JpaRepository Save Ignored in custom FieldSetMapper 【发布时间】:2016-07-26 14:11:32 【问题描述】:简而言之,我正在尝试在 Spring Batch 相关类中使用 JpaRepositories。但由于某种原因,只有选择有效,而插入/更新被忽略。
我有一个自定义FieldSetMapper
,如下所示,
@Component
@StepScope // I have not seen step scope is used as this way!
public class ItemFieldSetMapper implements FieldSetMapper<CategoryItem>
private CSVSchema schema;
private Task task;
Logger logger = Logger.getLogger(LocalPersist.class.getName());
@Autowired
private CSVColumnDao csvColumnDao;
@Autowired
private BasisCategoryDao basisCategoryDao;
@Override
public BasisCategoryItem mapFieldSet(FieldSet fieldSet) throws BindException
// csvColumnDao.save(someobject) // This is ignored
由于我必须异步启动作业(作业是从控制器方法启动的)
我创建了一个自定义的 DefaultBatchConfigurer,如下所示,
@Component
@EnableBatchProcessing
public class ProductImportBatchConfig extends DefaultBatchConfigurer
@Autowired
private TaskExecutor taskExecutor; // This is coming form another bean defined in another configuration class.
/*@Autowired
private DataSource dataSource;*/
@Autowired
private PlatformTransactionManager transactionManager;
@Override
protected JobLauncher createJobLauncher() throws Exception
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(super.getJobRepository());
jobLauncher.setTaskExecutor(taskExecutor);
return jobLauncher;
@Override
public PlatformTransactionManager getTransactionManager()
// TODO Auto-generated method stub
return transactionManager;
@Override
@Autowired
public void setDataSource(DataSource dataSource)
DatabasePopulatorUtils.execute(dropDatabasePopulator(), dataSource);
DatabasePopulatorUtils.execute(createDatabasePopulator(), dataSource);
// TODO Auto-generated method stub
super.setDataSource(dataSource);
private DatabasePopulator dropDatabasePopulator()
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
databasePopulator.setContinueOnError(true);
databasePopulator.addScript(new ClassPathResource("org/springframework/batch/core/schema-drop-mysql.sql"));
return databasePopulator;
private DatabasePopulator createDatabasePopulator()
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
databasePopulator.setContinueOnError(true);
databasePopulator.addScript(new ClassPathResource("org/springframework/batch/core/schema-mysql.sql"));
return databasePopulator;
这是我的应用程序类,
@Configuration
@ComponentScan
@EnableAutoConfiguration(exclude = BatchAutoConfiguration.class )
public class Application extends SpringBootServletInitializer
private static Class<Application> applicationClass = Application.class;
public static void main(String[] args)
SpringApplication.run(applicationClass, args);
@Override
protected final SpringApplicationBuilder configure(final SpringApplicationBuilder application)
return application.sources(applicationClass);
@Bean
public ThreadPoolTaskExecutor taskExecutor()
ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
pool.setCorePoolSize(6);
pool.setMaxPoolSize(30);
pool.setWaitForTasksToCompleteOnShutdown(false);
return pool;
到目前为止我的想法:
我查看了许多文档以使我的方案正常工作。 This 是我得到的最接近的。
问题与@EnableBatchProcessing
有关。它创建自己的DataSourceTransactionManager
,它不知道 jpa/hibernate 并忽略插入/更新。
作为建议的解决方案,
@Bean
public BatchConfigurer batchConfigurer(DataSource dataSource, EntityManagerFactory entityManagerFactory)
return new BasicBatchConfigurer(dataSource, entityManagerFactory);
这似乎不起作用,因为构造函数在较新的版本中发生了变化。
看来,我必须摆脱 @EnableBatchProcessing
并让相同的事务管理器在应用程序和批处理上下文中工作。
这里有一些试验和错误,
如果我保留@EnableBatchProcessing
并使用@EnableAutoConfiguration (exclude = BatchAutoConfiguration.class )
批处理表已创建并且应用程序正常运行,但在运行时我得到以下信息。
java.lang.***Error: null
at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:936) ~[na:1.8.0_72]
at org.springframework.aop.framework.AdvisedSupport.getInterceptorsAndDynamicInterceptionAdvice(AdvisedSupport.java:487) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:193) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at com.sun.proxy.$Proxy126.getTransaction(Unknown Source) ~[na:na]
at sun.reflect.GeneratedMethodAccessor110.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_72]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_72]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:127) ~[spring-batch-core-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at com.sun.proxy.$Proxy126.getTransaction(Unknown Source) ~[na:na]
at sun.reflect.GeneratedMethodAccessor110.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_72]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_72]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:127) ~[spring-batch-core-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at com.sun.proxy.$Proxy126.getTransaction(Unknown Source) ~[na:na]
at sun.reflect.GeneratedMethodAccessor110.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_72]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_72]
这是我正在使用的属性文件,
spring.datasource.url = jdbc:mysql://localhost:3306/db
spring.datasource.username =root
spring.datasource.password =root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.validationQuery = SELECT 1
spring.datasource.timeBetweenEvictionRunsMillis = 3600
spring.datasource.minEvictableIdleTimeMillis = 3600
spring.datasource.testWhileIdle = true
spring.jpa.database = MYSQL
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto =update
spring.jpa.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming_strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.batch.job.enabled=false
spring.view.prefix=/WEB-INF/jsp/
spring.view.suffix=.jsp
但是当我评论 @EnableBatchProcessing
时,应用程序抱怨
JobBuilderFactory
bean 等。似乎使用相同的事务管理器不是一种做法,我还没有看到批处理相关类(例如,ItemWriters 和 Readers 等)中 Jpa 存储库的自动连接。但我希望这个场景能够工作,因为这是从不同的实现到 Spring Batch 的迁移。
【问题讨论】:
我可以知道为什么这被否决了吗? 【参考方案1】:我对将 jpa 事务管理器注入 Spring Batch 感到困惑。这就是我能够做到的方式。
@Configuration
@EnableBatchProcessing
public class ProductImportBatchConfig implements BatchConfigurer
private static final Log logger = LogFactory.getLog(DefaultBatchConfigurer.class);
@Autowired
private TaskExecutor taskExecutor;
@Autowired
private DataSource dataSource;
private PlatformTransactionManager transactionManager;
@Autowired
private LocalContainerEntityManagerFactoryBean entityManagerFactory;
private JobRepository jobRepository;
private JobLauncher jobLauncher;
private JobExplorer jobExplorer;
protected ProductImportBatchConfig()
@Override
public JobRepository getJobRepository()
return jobRepository;
@Override
public PlatformTransactionManager getTransactionManager()
return transactionManager;
@Override
public JobLauncher getJobLauncher()
return jobLauncher;
@Override
public JobExplorer getJobExplorer()
return jobExplorer;
@Autowired
public void setDataSource(DataSource dataSource)
if (this.dataSource == null)
logger.error(null, new Throwable("This is not acceptable"));
DatabasePopulatorUtils.execute(dropDatabasePopulator(), this.dataSource);
DatabasePopulatorUtils.execute(createDatabasePopulator(), this.dataSource);
this.dataSource = dataSource;
EntityManagerFactory object = entityManagerFactory.getObject();
JpaTransactionManager jpaTransactionManager = new JpaTransactionManager(object);
this.transactionManager = jpaTransactionManager;
@PostConstruct
public void initialize()
try
if (dataSource == null)
logger.warn("No datasource was provided...using a Map based JobRepository");
if (this.transactionManager == null)
this.transactionManager = new ResourcelessTransactionManager();
MapJobRepositoryFactoryBean jobRepositoryFactory = new MapJobRepositoryFactoryBean(
this.transactionManager);
jobRepositoryFactory.afterPropertiesSet();
this.jobRepository = jobRepositoryFactory.getObject();
MapJobExplorerFactoryBean jobExplorerFactory = new MapJobExplorerFactoryBean(jobRepositoryFactory);
jobExplorerFactory.afterPropertiesSet();
this.jobExplorer = jobExplorerFactory.getObject();
else
this.jobRepository = createJobRepository();
JobExplorerFactoryBean jobExplorerFactoryBean = new JobExplorerFactoryBean();
jobExplorerFactoryBean.setDataSource(this.dataSource);
jobExplorerFactoryBean.afterPropertiesSet();
this.jobExplorer = jobExplorerFactoryBean.getObject();
this.jobLauncher = createJobLauncher();
catch (Exception e)
throw new BatchConfigurationException(e);
public JobLauncher createJobLauncher() throws Exception
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository);
jobLauncher.setTaskExecutor(taskExecutor);
jobLauncher.afterPropertiesSet();
return jobLauncher;
public JobRepository createJobRepository() throws Exception
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
factory.setDataSource(dataSource);
factory.setTransactionManager(transactionManager);
factory.afterPropertiesSet();
return factory.getObject();
// Batch related scripts
private DatabasePopulator dropDatabasePopulator()
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
databasePopulator.setContinueOnError(true);
databasePopulator.addScript(new ClassPathResource("org/springframework/batch/core/schema-drop-mysql.sql"));
return databasePopulator;
private DatabasePopulator createDatabasePopulator()
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
databasePopulator.setContinueOnError(true);
databasePopulator.addScript(new ClassPathResource("org/springframework/batch/core/schema-mysql.sql"));
return databasePopulator;
【讨论】:
以上是关于自定义 FieldSetMapper 中忽略的自动装配 JpaRepository 保存的主要内容,如果未能解决你的问题,请参考以下文章
iOS - 自定义视图:在 layoutSubviews() 中更新后忽略了固有内容大小