|
| 1 | +/* |
| 2 | + * Copyright 2020-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.task.batch.autoconfigure.jdbc; |
| 18 | + |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import javax.sql.DataSource; |
| 22 | + |
| 23 | +import org.springframework.batch.item.ItemWriter; |
| 24 | +import org.springframework.batch.item.database.ItemPreparedStatementSetter; |
| 25 | +import org.springframework.batch.item.database.ItemSqlParameterSourceProvider; |
| 26 | +import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder; |
| 27 | +import org.springframework.beans.factory.annotation.Autowired; |
| 28 | +import org.springframework.boot.autoconfigure.AutoConfigureAfter; |
| 29 | +import org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration; |
| 30 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 31 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 32 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 33 | +import org.springframework.context.annotation.Bean; |
| 34 | +import org.springframework.context.annotation.Configuration; |
| 35 | + |
| 36 | +/** |
| 37 | + * Autconfiguration for a {@code JdbcBatchItemWriter}. |
| 38 | + * |
| 39 | + * @author Glenn Renfro |
| 40 | + * @since 2.3 |
| 41 | + */ |
| 42 | +@Configuration |
| 43 | +@EnableConfigurationProperties(JdbcItemWriterProperties.class) |
| 44 | +@AutoConfigureAfter(BatchAutoConfiguration.class) |
| 45 | +public class JdbcItemWriterAutoConfiguration { |
| 46 | + |
| 47 | + @Autowired(required = false) |
| 48 | + private ItemPreparedStatementSetter itemPreparedStatementSetter; |
| 49 | + |
| 50 | + @Autowired(required = false) |
| 51 | + private ItemSqlParameterSourceProvider itemSqlParameterSourceProvider; |
| 52 | + |
| 53 | + private JdbcItemWriterProperties properties; |
| 54 | + |
| 55 | + private DataSource dataSource; |
| 56 | + |
| 57 | + public JdbcItemWriterAutoConfiguration(DataSource dataSource, |
| 58 | + JdbcItemWriterProperties properties) { |
| 59 | + this.dataSource = dataSource; |
| 60 | + this.properties = properties; |
| 61 | + } |
| 62 | + |
| 63 | + @Bean |
| 64 | + @ConditionalOnMissingBean |
| 65 | + @ConditionalOnProperty(prefix = "spring.batch.job.jdbcwriter", name = "name") |
| 66 | + public ItemWriter<Map<Object, Object>> itemWriter() { |
| 67 | + |
| 68 | + JdbcBatchItemWriterBuilder<Map<Object, Object>> jdbcBatchItemWriterBuilder = new JdbcBatchItemWriterBuilder<Map<Object, Object>>() |
| 69 | + .dataSource(this.dataSource).sql(this.properties.getSql()); |
| 70 | + if (this.itemPreparedStatementSetter != null) { |
| 71 | + jdbcBatchItemWriterBuilder |
| 72 | + .itemPreparedStatementSetter(this.itemPreparedStatementSetter); |
| 73 | + } |
| 74 | + else if (this.itemSqlParameterSourceProvider != null) { |
| 75 | + jdbcBatchItemWriterBuilder |
| 76 | + .itemSqlParameterSourceProvider(this.itemSqlParameterSourceProvider); |
| 77 | + } |
| 78 | + else { |
| 79 | + jdbcBatchItemWriterBuilder.columnMapped(); |
| 80 | + } |
| 81 | + jdbcBatchItemWriterBuilder.assertUpdates(this.properties.isAssertUpdates()); |
| 82 | + return jdbcBatchItemWriterBuilder.build(); |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments