简体   繁体   English

Spring 批量通配符ItemWriter

[英]Spring Batch Wildcard ItemWriter

I have one dummy question.我有一个假问题。 To explain my use-case, I have different type of DAOs;为了解释我的用例,我有不同类型的 DAO; say Users, Beers... etc. I wanted to use one generic ItemWriter for all of them.比如说用户、啤酒……等等。我想为所有这些人使用一个通用的 ItemWriter。 I created a CommonComponentConfiguration where I defined;我在我定义的地方创建了一个CommonComponentConfiguration

@Bean
@Qualifier(WRITER_INSERT_TO_DATABASE_BEAN)
public ItemWriter<?> insertDbItemWriter(@Qualifier(DATA_SOURCE) DataSource dataSource,
                                        @Qualifier("insertSql") String insertSql) {
         return new MyItemWriter<>(dataSource, insertSql);
     }

The writer class goes like this;作家 class 是这样的;

@Slf4j
public class MyItemWriter<T> extends JdbcBatchItemWriter<T> {

    public MyItemWriter(DataSource dataSource, String sql) {
        this.setDataSource(dataSource);
        this.setSql(sql);
        this.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
        this.setAssertUpdates(false);
    }

    @Override
    public void write(List<? extends T> items) {
        try {
            super.write(items);
        } catch (Exception e) {
            log.error("Could not write the items " + items);
            log.error(e.getMessage());
        }
    }
}

So far everything is okay.到目前为止一切正常。 Where things gets complicated is, I have seperate configuration classes for each repository where I define repository specific items.事情变得复杂的是,我为每个存储库定义了存储库特定项的单独配置类。 For instance the step for inserting to DB for Users.例如为用户插入数据库的步骤。

@Bean
@Qualifier(STEP_INSERT_TO_DB_BEAN)
public Step insertToDbStep(@Qualifier(READER_LOADED_INPUT_DATA_BEAN) ListItemReader<User> sourceItemReader, UserInsertProcessor userInsertProcessor, @Qualifier(WRITER_INSERT_TO_DATABASE_BEAN)
        ItemWriter<User> dbItemWriter) {
    return stepBuilderFactory.get("processInsertStep").<User, User>chunk(100)
            .reader(sourceItemReader)
            .processor(userInsertProcessor)
            .writer(dbItemWriter)
            .build();
}

When I write this code in IJ is complaining Could not autowire. Qualified bean must be of 'ItemWriter<User>' type.当我在 IJ 中编写此代码时,我抱怨Could not autowire. Qualified bean must be of 'ItemWriter<User>' type. Could not autowire. Qualified bean must be of 'ItemWriter<User>' type. , but heavens sake when I execute the code, it works, and does what its supposed to do. ,但是当我执行代码时,天啊,它可以工作,并且可以做它应该做的事情。 When I debug, it binds the right thing.当我调试时,它绑定了正确的东西。

Well, you may say, if it works don't touch it.好吧,你可能会说,如果它有效,请不要碰它。 However I really want to know what's happening behind the curtains.但是我真的很想知道窗帘后面发生了什么。

Also, if you see a flaw in the design (such as trying to use one common thing for everything), your suggestions are more than welcomed.此外,如果您发现设计中存在缺陷(例如尝试对所有事物使用一种通用的东西),您的建议将受到欢迎。

Thank you in advance.先感谢您。

PS: Seen this thread below, looks like very similar -if not the same- case. PS:在下面看到这个线程,看起来非常相似 - 如果不是相同的话 - 案例。 However I would like to know if there's something to do with the generics here.但是我想知道这里是否与 generics 有关。 IntelliJ IDEA shows errors when using Spring's @Autowired annotation IntelliJ IDEA 在使用 Spring 的 @Autowired 注解时显示错误

Edit:编辑: 错误

However I really want to know what's happening behind the curtains.但是我真的很想知道窗帘后面发生了什么。

For this IntelliJ IDEA warning, you are right that it is the same issue discussed in IntelliJ IDEA shows errors when using Spring's @Autowired annotation (and also as explained by Eugene in comments)对于这个 IntelliJ IDEA 警告,您是对的,这是IntelliJ IDEA 中讨论的相同问题,在使用 Spring 的 @Autowired 注释时显示错误(也正如 Eugene 在评论中所解释的那样)

Also, if you see a flaw in the design (such as trying to use one common thing for everything), your suggestions are more than welcomed.此外,如果您发现设计中存在缺陷(例如尝试对所有事物使用一种通用的东西),您的建议将受到欢迎。

If the current approach works for you, you can use it.如果当前方法适合您,您可以使用它。 However, I would recommend making one thing do one thing and do it well.但是,我会建议让一件事做一件事并做好。 In your case, this would be using an item writer for each domain type and wrap those writers in a ClassifierCompositeItemWriter .在您的情况下,这将为每种域类型使用项目编写器,并将这些编写器包装在ClassifierCompositeItemWriter中。 The composite writer uses a Classifier to classify items and call the corresponding writer accordingly.复合编写器使用Classifier器对项目进行分类并相应地调用相应的编写器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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