View Javadoc

1   /*
2    * Copyright 2012-2013 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    *      http://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  package org.springframework.batch.core.configuration.annotation;
17  
18  import java.util.Collection;
19  
20  import org.springframework.batch.core.configuration.support.ApplicationContextFactory;
21  import org.springframework.batch.core.configuration.support.AutomaticJobRegistrar;
22  import org.springframework.batch.core.configuration.support.DefaultJobLoader;
23  import org.springframework.batch.core.launch.JobLauncher;
24  import org.springframework.batch.core.repository.JobRepository;
25  import org.springframework.beans.factory.annotation.Autowired;
26  import org.springframework.context.ApplicationContext;
27  import org.springframework.context.annotation.Bean;
28  import org.springframework.context.annotation.Configuration;
29  import org.springframework.transaction.PlatformTransactionManager;
30  
31  /**
32   * Base {@code Configuration} class providing common structure for enabling and using Spring Batch. Customization is
33   * available by implementing the {@link BatchConfigurer} interface.
34   *
35   * @author Dave Syer
36   * @since 2.2
37   * @see EnableBatchProcessing
38   */
39  @Configuration
40  public class ModularBatchConfiguration extends AbstractBatchConfiguration {
41  
42  	@Autowired
43  	private ApplicationContext context;
44  
45  	@Autowired(required = false)
46  	private Collection<BatchConfigurer> configurers;
47  
48  	private AutomaticJobRegistrar registrar = new AutomaticJobRegistrar();
49  
50  	@Override
51  	@Bean
52  	public JobRepository jobRepository() throws Exception {
53  		return getConfigurer(configurers).getJobRepository();
54  	}
55  
56  	@Override
57  	@Bean
58  	public JobLauncher jobLauncher() throws Exception {
59  		return getConfigurer(configurers).getJobLauncher();
60  	}
61  
62  	@Override
63  	@Bean
64  	public PlatformTransactionManager transactionManager() throws Exception {
65  		return getConfigurer(configurers).getTransactionManager();
66  	}
67  
68  	@Bean
69  	public AutomaticJobRegistrar jobRegistrar() throws Exception {
70  		registrar.setJobLoader(new DefaultJobLoader(jobRegistry()));
71  		for (ApplicationContextFactory factory : context.getBeansOfType(ApplicationContextFactory.class).values()) {
72  			registrar.addApplicationContextFactory(factory);
73  		}
74  		return registrar;
75  	}
76  
77  }