001/*
002 * Copyright 2012-2013 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.springframework.batch.core.configuration.annotation;
017
018import org.springframework.batch.core.configuration.support.ApplicationContextFactory;
019import org.springframework.batch.core.configuration.support.AutomaticJobRegistrar;
020import org.springframework.batch.core.configuration.support.DefaultJobLoader;
021import org.springframework.batch.core.explore.JobExplorer;
022import org.springframework.batch.core.launch.JobLauncher;
023import org.springframework.batch.core.repository.JobRepository;
024import org.springframework.beans.factory.annotation.Autowired;
025import org.springframework.context.ApplicationContext;
026import org.springframework.context.annotation.Bean;
027import org.springframework.context.annotation.Configuration;
028import org.springframework.transaction.PlatformTransactionManager;
029
030import java.util.Collection;
031
032/**
033 * Base {@code Configuration} class providing common structure for enabling and using Spring Batch. Customization is
034 * available by implementing the {@link BatchConfigurer} interface.
035 *
036 * @author Dave Syer
037 * @since 2.2
038 * @see EnableBatchProcessing
039 */
040@Configuration
041public class ModularBatchConfiguration extends AbstractBatchConfiguration {
042
043        @Autowired
044        private ApplicationContext context;
045
046        @Autowired(required = false)
047        private Collection<BatchConfigurer> configurers;
048
049        private AutomaticJobRegistrar registrar = new AutomaticJobRegistrar();
050
051        @Override
052        @Bean
053        public JobRepository jobRepository() throws Exception {
054                return getConfigurer(configurers).getJobRepository();
055        }
056
057        @Override
058        @Bean
059        public JobLauncher jobLauncher() throws Exception {
060                return getConfigurer(configurers).getJobLauncher();
061        }
062
063        @Override
064        @Bean
065        public PlatformTransactionManager transactionManager() throws Exception {
066                return getConfigurer(configurers).getTransactionManager();
067        }
068
069        @Override
070        @Bean
071        public JobExplorer jobExplorer() throws Exception {
072                return getConfigurer(configurers).getJobExplorer();
073        }
074
075        @Bean
076        public AutomaticJobRegistrar jobRegistrar() throws Exception {
077                registrar.setJobLoader(new DefaultJobLoader(jobRegistry()));
078                for (ApplicationContextFactory factory : context.getBeansOfType(ApplicationContextFactory.class).values()) {
079                        registrar.addApplicationContextFactory(factory);
080                }
081                return registrar;
082        }
083
084}