|
| 1 | +/* |
| 2 | + * Copyright 2022 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 | +package org.springframework.batch.core.configuration.annotation; |
| 17 | + |
| 18 | +import java.util.Iterator; |
| 19 | + |
| 20 | +import org.springframework.batch.core.configuration.JobRegistry; |
| 21 | +import org.springframework.batch.core.configuration.support.ApplicationContextFactory; |
| 22 | +import org.springframework.batch.core.configuration.support.AutomaticJobRegistrar; |
| 23 | +import org.springframework.batch.core.configuration.support.DefaultJobLoader; |
| 24 | +import org.springframework.beans.BeansException; |
| 25 | +import org.springframework.beans.factory.config.BeanFactoryPostProcessor; |
| 26 | +import org.springframework.beans.factory.config.BeanPostProcessor; |
| 27 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 28 | + |
| 29 | +/** |
| 30 | + * Post processor that configures the {@link AutomaticJobRegistrar} registered by |
| 31 | + * {@link BatchRegistrar} with required properties. |
| 32 | + * |
| 33 | + * @author Mahmoud Ben Hassine |
| 34 | + * @since 5.0 |
| 35 | + */ |
| 36 | +class AutomaticJobRegistrarBeanPostProcessor implements BeanFactoryPostProcessor, BeanPostProcessor { |
| 37 | + |
| 38 | + private ConfigurableListableBeanFactory beanFactory; |
| 39 | + |
| 40 | + @Override |
| 41 | + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { |
| 42 | + this.beanFactory = beanFactory; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { |
| 47 | + if (bean instanceof AutomaticJobRegistrar) { |
| 48 | + AutomaticJobRegistrar automaticJobRegistrar = (AutomaticJobRegistrar) bean; |
| 49 | + automaticJobRegistrar.setJobLoader(new DefaultJobLoader(this.beanFactory.getBean(JobRegistry.class))); |
| 50 | + for (ApplicationContextFactory factory : this.beanFactory.getBeansOfType(ApplicationContextFactory.class) |
| 51 | + .values()) { |
| 52 | + automaticJobRegistrar.addApplicationContextFactory(factory); |
| 53 | + } |
| 54 | + return automaticJobRegistrar; |
| 55 | + } |
| 56 | + return bean; |
| 57 | + } |
| 58 | + |
| 59 | +} |
0 commit comments