34
34
import org .springframework .batch .core .Job ;
35
35
import org .springframework .batch .core .Step ;
36
36
import org .springframework .batch .core .configuration .annotation .EnableBatchProcessing ;
37
- import org .springframework .batch .core .configuration .annotation .JobBuilderFactory ;
38
- import org .springframework .batch .core .configuration .annotation .StepBuilderFactory ;
39
- import org .springframework .beans .factory .annotation .Autowired ;
37
+ import org .springframework .batch .core .job .builder .JobBuilder ;
38
+ import org .springframework .batch .core .repository .JobRepository ;
39
+ import org .springframework .batch .core .step .builder .StepBuilder ;
40
+ import org .springframework .batch .item .ItemProcessor ;
41
+ import org .springframework .batch .item .ItemReader ;
42
+ import org .springframework .batch .item .ItemWriter ;
40
43
import org .springframework .context .annotation .Bean ;
41
44
import org .springframework .context .annotation .Configuration ;
42
45
import org .springframework .core .convert .converter .Converter ;
50
53
@ EnableBatchProcessing
51
54
public class SampleJobConfig {
52
55
53
- @ Autowired
54
- private JobBuilderFactory jobBuilderFactory ;
55
-
56
- @ Autowired
57
- private StepBuilderFactory stepBuilderFactory ;
58
-
59
56
@ Bean
60
57
public DataSource dataSource () {
61
58
return new EmbeddedDatabaseBuilder ().setType (EmbeddedDatabaseType .HSQL )
@@ -66,15 +63,15 @@ public DataSource dataSource() {
66
63
}
67
64
68
65
@ Bean
69
- public PlatformTransactionManager transactionalManager ( ) {
70
- return new DataSourceTransactionManager (dataSource () );
66
+ public PlatformTransactionManager transactionManager ( DataSource dataSource ) {
67
+ return new DataSourceTransactionManager (dataSource );
71
68
}
72
69
73
70
@ Bean
74
- public SqlSessionFactory sqlSessionFactory () throws Exception {
71
+ public SqlSessionFactory sqlSessionFactory (DataSource dataSource ) throws Exception {
75
72
PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver ();
76
73
SqlSessionFactoryBean ss = new SqlSessionFactoryBean ();
77
- ss .setDataSource (dataSource () );
74
+ ss .setDataSource (dataSource );
78
75
ss .setMapperLocations (resourcePatternResolver .getResources ("org/mybatis/spring/sample/mapper/*.xml" ));
79
76
org .apache .ibatis .session .Configuration configuration = new org .apache .ibatis .session .Configuration ();
80
77
configuration .setDefaultExecutorType (ExecutorType .BATCH );
@@ -83,10 +80,10 @@ public SqlSessionFactory sqlSessionFactory() throws Exception {
83
80
}
84
81
85
82
@ Bean
86
- public MyBatisCursorItemReader <User > reader () throws Exception {
83
+ public MyBatisCursorItemReader <User > reader (SqlSessionFactory sqlSessionFactory ) {
87
84
// @formatter:off
88
85
return new MyBatisCursorItemReaderBuilder <User >()
89
- .sqlSessionFactory (sqlSessionFactory () )
86
+ .sqlSessionFactory (sqlSessionFactory )
90
87
.queryId ("org.mybatis.spring.sample.mapper.UserMapper.getUsers" )
91
88
.build ();
92
89
// @formatter:on
@@ -98,10 +95,10 @@ public UserToPersonItemProcessor processor() {
98
95
}
99
96
100
97
@ Bean
101
- public MyBatisBatchItemWriter <Person > writer () throws Exception {
98
+ public MyBatisBatchItemWriter <Person > writer (SqlSessionFactory sqlSessionFactory ) {
102
99
// @formatter:off
103
100
return new MyBatisBatchItemWriterBuilder <Person >()
104
- .sqlSessionFactory (sqlSessionFactory () )
101
+ .sqlSessionFactory (sqlSessionFactory )
105
102
.statementId ("org.mybatis.spring.sample.mapper.PersonMapper.createPerson" )
106
103
.itemToParameterConverter (createItemToParameterMapConverter ("batch_java_config_user" , LocalDateTime .now ()))
107
104
.build ();
@@ -120,24 +117,24 @@ public static <T> Converter<T, Map<String, Object>> createItemToParameterMapConv
120
117
}
121
118
122
119
@ Bean
123
- public Job importUserJob () throws Exception {
120
+ public Job importUserJob (JobRepository jobRepository , Step step1 ) {
124
121
// @formatter:off
125
- return jobBuilderFactory . get ("importUserJob" )
126
- .flow (step1 () )
122
+ return new JobBuilder ("importUserJob" , jobRepository )
123
+ .flow (step1 )
127
124
.end ()
128
125
.build ();
129
126
// @formatter:on
130
127
}
131
128
132
129
@ Bean
133
- public Step step1 () throws Exception {
130
+ public Step step1 (JobRepository jobRepository , PlatformTransactionManager transactionManager , ItemReader <User > reader ,
131
+ ItemProcessor <User , Person > processor , ItemWriter <Person > writer ) {
134
132
// @formatter:off
135
- return stepBuilderFactory .get ("step1" )
136
- .<User , Person >chunk (10 )
137
- .reader (reader ())
138
- .processor (processor ())
139
- .writer (writer ())
140
- .transactionManager (transactionalManager ())
133
+ return new StepBuilder ("step1" , jobRepository )
134
+ .<User , Person >chunk (10 , transactionManager )
135
+ .reader (reader )
136
+ .processor (processor )
137
+ .writer (writer )
141
138
.build ();
142
139
// @formatter:on
143
140
}
0 commit comments