Spring Batch原始碼閱讀-初始化(三)
阿新 • • 發佈:2021-10-21
例子
@SpringBootApplication //可選引數預先初始化還是延遲初始化 @EnableBatchProcessing(modular = true) public class SpringBatchDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringBatchDemoApplication.class, args); } }
我們使用spring batch 使用了 @EnableBatchProcessing 此註解
@EnableBatchProcessing作用
我們開啟原始碼可以發現使用Import註解 import註解使用可以檢視https://www.cnblogs.com/LQBlog/p/15410425.html
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Import(BatchConfigurationSelector.class)//<1> public @interface EnableBatchProcessing { /** * Indicate whether the configuration is going to be modularized into multiple application contexts. If true then * you should not create any @Bean Job definitions in this context, but rather supply them in separate (child) * contexts through an {@link ApplicationContextFactory}. * * @return boolean indicating whether the configuration is going to be * modularized into multiple application contexts. Defaults to false. */ boolean modular() default false; }
<1>
public class BatchConfigurationSelector implementsImportSelector { @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { Class<?> annotationType = EnableBatchProcessing.class; //獲取註解元資料 AnnotationAttributes attributes = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes( annotationType.getName(), false)); Assert.notNull(attributes, String.format("@%s is not present on importing class '%s' as expected", annotationType.getSimpleName(), importingClassMetadata.getClassName())); String[] imports; //是實時初始化 延時延遲初始化 if (attributes.containsKey("modular") && attributes.getBoolean("modular")) { //<2> 匯入ModularBatchConfiguration類 imports = new String[] { ModularBatchConfiguration.class.getName() }; } else { //是延遲初始化 點進去看程式碼可以看到get相關物件都是建立代理 imports = new String[] { SimpleBatchConfiguration.class.getName() }; } return imports; }
<2>
//proxyBeanMethods = false @Bean建立的物件將不使用代理 @Configuration(proxyBeanMethods = false) public class ModularBatchConfiguration extends AbstractBatchConfiguration { @Autowired private ApplicationContext context; @Autowired(required = false) private Collection<BatchConfigurer> configurers; private AutomaticJobRegistrar registrar = new AutomaticJobRegistrar(); /** * 獲得jobRepository 主要是用來管理 job執行期間相關元資料的 * 比如redis,檔案,h2庫 預設我們都是用的mysql * 可參考:org.springframework.batch.core.repository.support.SimpleJobRepository * @return * @throws Exception */ @Override @Bean public JobRepository jobRepository() throws Exception { return getConfigurer(configurers).getJobRepository(); } /** * 獲得JobLauncher * JobLauncher主要job的容器 主要是管理job的啟動 * @return * @throws Exception */ @Override @Bean public JobLauncher jobLauncher() throws Exception { return getConfigurer(configurers).getJobLauncher(); } /** * job 執行期間的提交和關閉事物使用的事物管理器 * @return * @throws Exception */ @Override @Bean public PlatformTransactionManager transactionManager() throws Exception { return getConfigurer(configurers).getTransactionManager(); } /** * 與JobRepository 後續看原始碼再看具體用來做啥 * @return * @throws Exception */ @Override @Bean public JobExplorer jobExplorer() throws Exception { return getConfigurer(configurers).getJobExplorer(); } /** * JobRepository的容器 * 內部可以通過star方法 在spring容器裡面查詢job並註冊到JobRegistry * 通過stop清空 主要是委託給JobLoader 預設實現是在spring 容器查詢 * 比如我們可以實現從mysql查詢 * @return * @throws Exception */ @Bean public AutomaticJobRegistrar jobRegistrar() throws Exception { registrar.setJobLoader(new DefaultJobLoader(jobRegistry())); for (ApplicationContextFactory factory : context.getBeansOfType(ApplicationContextFactory.class).values()) { registrar.addApplicationContextFactory(factory); } return registrar; } }