View Javadoc

1   /*
2    * Copyright 2006-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.support;
17  
18  import org.springframework.batch.core.Job;
19  import org.springframework.batch.core.configuration.JobFactory;
20  import org.springframework.context.ApplicationContext;
21  import org.springframework.context.ConfigurableApplicationContext;
22  
23  /**
24   * A {@link JobFactory} that creates its own {@link ApplicationContext} and
25   * pulls a bean out when asked to create a {@link Job}.
26   *
27   * @author Dave Syer
28   *
29   */
30  public class ApplicationContextJobFactory implements JobFactory {
31  
32  	private final Job job;
33  
34  	/**
35  	 * @param jobName the id of the {@link Job} in the application context to be
36  	 * created
37  	 * @param applicationContextFactory a factory for an application context
38  	 * containing a job with the job name provided
39  	 */
40  	public ApplicationContextJobFactory(String jobName, ApplicationContextFactory applicationContextFactory) {
41  		ConfigurableApplicationContext context = applicationContextFactory.createApplicationContext();
42  		this.job = context.getBean(jobName, Job.class);
43  	}
44  
45  	/**
46  	 * Create an {@link ApplicationContext} from the factory provided and pull
47  	 * out a bean with the name given during initialization.
48  	 *
49  	 * @see org.springframework.batch.core.configuration.JobFactory#createJob()
50  	 */
51  	@Override
52  	public final Job createJob() {
53  		return job;
54  	}
55  
56  	/**
57  	 * Just return the name of instance passed in on initialization.
58  	 *
59  	 * @see JobFactory#getJobName()
60  	 */
61  	@Override
62  	public String getJobName() {
63  		return job.getName();
64  	}
65  
66  }