diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/EnableBatchProcessing.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/EnableBatchProcessing.java index d8545cc3b..aaf15b2dc 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/EnableBatchProcessing.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/EnableBatchProcessing.java @@ -21,67 +21,73 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import org.springframework.batch.core.configuration.JobRegistry; +import org.springframework.batch.core.configuration.support.ApplicationContextFactory; +import org.springframework.batch.core.configuration.support.AutomaticJobRegistrar; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.batch.core.repository.JobRepository; import org.springframework.context.annotation.Import; +import org.springframework.transaction.PlatformTransactionManager; /** *

* Enable Spring Batch features and provide a base configuration for setting up batch jobs in an @Configuration * class, roughly equivalent to using the {@code } XML namespace. - * + * *

  * @Configuration
  * @EnableBatchProcessing
  * @Import(DataSourceCnfiguration.class)
  * public class AppConfig {
- * 
+ *
  * 	@Autowired
  * 	private JobBuilderFactory jobs;
- * 
+ *
  * 	@Bean
  * 	public Job job() {
  * 		return jobs.get("myJob").start(step1()).next(step2()).build();
  * 	}
- * 
+ *
  * 	@Bean
  *    protected Step step1() {
  *       ...
  *    }
- * 
+ *
  * 	@Bean
  *    protected Step step2() {
  *     ...
  *    }
  * }
  * 
- * + * * The user has to provide a {@link DataSource} as a bean in the context, or else implement {@link BatchConfigurer} in * the configuration class itself, e.g. - * + * *
  * @Configuration
  * @EnableBatchProcessing
  * public class AppConfig extends DefaultBatchConfigurer {
- * 
+ *
  *    @Bean
  *    public Job job() {
  *       ...
  *    }
- * 
+ *
  *    @Override
  *    protected JobRepository createJobRepository() {
  *       ...
  *    }
- *  
+ *
  *  ...
- *  
+ *
  * }
  * 
- * + * * Note that only one of your configuration classes needs to have the @EnableBatchProcessing * annotation. Once you have an @EnableBatchProcessing class in your configuration you will have an * instance of {@link StepScope} so your beans inside steps can have @Scope("step"). You will also be * able to @Autowired some useful stuff into your context: - * + * * - * + * * If the configuration is specified as modular=true then the context will also contain an * {@link AutomaticJobRegistrar}. The job registrar is useful for modularizing your configuration if there are multiple * jobs. It works by creating separate child application contexts containing job configurations and registering those * jobs. The jobs can then create steps and other dependent components without needing to worry about bean definition * name clashes. Beans of type {@link ApplicationContextFactory} will be registered automatically with the job * registrar. Example: - * + * *
  * @Configuration
  * @EnableBatchProcessing(modular=true)
  * public class AppConfig {
- * 
+ *
  *    @Bean
  *    public ApplicationContextFactory someJobs() {
  *       return new GenericApplicationContextFactory(SomeJobConfiguration.class);
  *    }
- * 
+ *
  *    @Bean
  *    public ApplicationContextFactory moreJobs() {
  *       return new GenericApplicationContextFactory(MoreJobConfiguration.class);
  *    }
- *  
+ *
  *  ...
- *  
+ *
  * }
  * 
- * + * * Note that a modular parent context in general should not itself contain @Bean definitions for job, * especially if a {@link BatchConfigurer} is provided, because cyclic configuration dependencies are otherwise likely * to develop. - * + * *

- * + * *

* For reference, the first example above can be compared to the following Spring XML configuration: - * + * *

  * {@code
  * 
@@ -144,9 +150,9 @@ import org.springframework.context.annotation.Import;
  * 
  * }
  * 
- * + * * @author Dave Syer - * + * */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/StepScope.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/StepScope.java index 20e348258..8ac7895b9 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/StepScope.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/StepScope.java @@ -13,7 +13,7 @@ import org.springframework.context.annotation.ScopedProxyMode; * explicitly on every bean definition. Use this on any @Bean that needs to inject @Values from the step * context, and any bean that needs to share a lifecycle with a step execution (e.g. an ItemStream). E.g. *

- * + * *
  * @Bean
  * @StepScope
@@ -22,13 +22,13 @@ import org.springframework.context.annotation.ScopedProxyMode;
  * 	return new SimpleCallable(value);
  * }
  * 
- * - *

Marking a @Bean as @StepScope is quivalent to marking it as @Scope(value="step", proxyMode=INTERFACES)

- * + * + *

Marking a @Bean as @StepScope is equivalent to marking it as @Scope(value="step", proxyMode=INTERFACES)

+ * * @author Dave Syer - * + * * @Since 2.2 - * + * */ @Scope(value = "step") @Retention(RetentionPolicy.RUNTIME) @@ -37,9 +37,9 @@ public @interface StepScope { /** * Set the proxy mode to use (defaults to INTERFACES). - * + * * @see Scope#proxyMode() - * + * * @return the proxy mode to use */ ScopedProxyMode proxyMode() default ScopedProxyMode.INTERFACES; diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/StepScopeConfigurationTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/StepScopeConfigurationTests.java index e5b0a8f0a..0d95a5672 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/StepScopeConfigurationTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/StepScopeConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2011 the original author or authors. + * Copyright 2006-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,7 +38,7 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author Dave Syer - * + * */ public class StepScopeConfigurationTests { @@ -107,7 +107,6 @@ public class StepScopeConfigurationTests { System.arraycopy(config, 0, configs, 1, config.length); configs[0] = DataSourceConfiguration.class; AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); - // context.setScopeMetadataResolver(new AnnotationScopeMetadataResolver(ScopedProxyMode.TARGET_CLASS)); context.register(configs); context.refresh(); this.context = context; @@ -126,7 +125,7 @@ public class StepScopeConfigurationTests { context.close(); } } - + public static class SimpleCallable implements Callable { private final String value; @@ -142,7 +141,7 @@ public class StepScopeConfigurationTests { public static class SimpleHolder { private final String value; - + protected SimpleHolder() { value = ""; }