将spring batch 3 reader转换为spring batch 4 reader config

时间:2017-12-21 06:50:33

标签: spring-batch

下面是我在Spring batch3中定义的Reader Config

.dropdown-menu {
  left:inherit !important;
  right:0px;
}


<!DOCTYPE html>
<html>
<head>
    <title>Login page</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css"
          integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
            integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
            crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js"
            integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh"
            crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"
            integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ"
            crossorigin="anonymous"></script>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="#">User Management</a>
    <div class="collapse navbar-collapse" id="navbarNav">
        <ul class="navbar-nav mr-auto">
            <li class="nav-item">
                <a class="nav-link" href="#">Home</a>
            </li>
        </ul>
        <span class="navbar-text">
         <div class="btn-group">
  <button class="btn btn-secondary btn-sm dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true"
          aria-expanded="false">
         button
  </button>
  <div class="dropdown-menu">
     <a class="dropdown-item" href="#">View Account</a>
    <a class="dropdown-item" href="#">Sign out</a>
  </div>
</div>
    </span>
    </div>
</nav>
</body>
</html>

由于Spring Batch 4添加了构建器以使其更清洁。 下面是SpringBatch 4中的配置。

@Bean
    @Qualifier("myReader")
    @StepScope
    public FlatFileItemReader<POJO> myReader(@Value("#{stepExecutionContext['fileName']}") String filename) {

        FlatFileItemReader<POJO> reader = new FlatFileItemReader<POJO>();     


        try {

            reader.setResource(new UrlResource(filename));
        } catch (MalformedURLException e) {
        }
        reader.setLinesToSkip(1);
        reader.setLineMapper(new DefaultLineMapper<POJO>() {{
            setLineTokenizer(new DelimitedLineTokenizer() {{
                setNames(FileHeader);
                setDelimiter(",");

            }});
            setFieldSetMapper(new BeanWrapperFieldSetMapper<POJO>() {{
                setTargetType(POJO.class);
            }});
        }});
        return reader;
    }

Builder确实使代码更简洁,更清晰。

但我无法理解为什么读者 .name(“”) 是强制性的? 以及它将如何使用?该名称的作用是否类似于限定符

1 个答案:

答案 0 :(得分:0)

name(String value)是必需的,因为框架使用它来生成用于在ExecutionContext中存储状态的密钥。在以前的版本中(如果您选择不使用构建器),配置该属性是可选的。问题是,通过不配置它,ExecutionContext内的密钥命名可能会发生冲突。通过要求配置名称,发生碰撞的可能性较小。