001/* 002 * Copyright 2014 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.jsr.configuration.xml; 017 018import org.springframework.batch.core.repository.JobRepository; 019import org.springframework.beans.BeansException; 020import org.springframework.beans.factory.config.BeanPostProcessor; 021import org.springframework.context.ApplicationContext; 022import org.springframework.context.ApplicationContextAware; 023 024/** 025 * @author Michael Minella 026 */ 027public class JsrNamespacePostProcessor implements BeanPostProcessor, ApplicationContextAware { 028 029 private static final String DEFAULT_JOB_REPOSITORY_NAME = "jobRepository"; 030 031 private ApplicationContext applicationContext; 032 033 @Override 034 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 035 this.applicationContext = applicationContext; 036 } 037 038 @Override 039 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { 040 if(bean instanceof JobFactoryBean) { 041 JobFactoryBean fb = (JobFactoryBean) bean; 042 JobRepository jobRepository = fb.getJobRepository(); 043 if (jobRepository == null) { 044 fb.setJobRepository((JobRepository) applicationContext.getBean(DEFAULT_JOB_REPOSITORY_NAME)); 045 } 046 } 047 048 return bean; 049 } 050 051 @Override 052 public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { 053 return bean; 054 } 055} 056 057 058