SpringBatch从入门到精通之StepScope作用域和用法详解

目录
  • 1.StepSope 是一种scope
  • 2.StepSope 是一种自定义step
  • 3.如何使用。@Value是支持spel表达式的
    • 3.1 大部分场景是Spel 表达式。在底层reader/process/writer 中使用@Value获取jobParamter/stepContext/jobContext
    • 3.2 SpEL引用bean
    • 3.3 系统属性
    • 3.4 运算符号
  • 4.可能遇到问题
  • 5.StepScope原理
  • 6.自定义一个scope

1.StepSope 是一种scope

在此之前,先说一下IOC容器中几种bean的作用范围:

  • singleton单例模式 – 全局有且仅有一个实例
  • prototype原型模式 – 每次获取Bean的时候会有一个新的实例
  • request – request表示该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前HTTP request内有效
  • session – session作用域表示该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前HTTP session内有效
  • globalsession – global session作用域类似于标准的HTTP Session作用域,不过它仅仅在基于portlet的web应用中才有意义

2.StepSope 是一种自定义step

目的是在每一次启动Job实例时底层单位(RPW或者也可是step)的参数可以灵活修改或者达到可变,因为一个系统里可能有多个job实例,每个job实例对应的参数是不一样的。在step运行期间需要获取的job参数/stepContext/jobContext,因此需要自定义一个作用域,令其与Step的生命周期一致。

使用注解了。那么stepScope修饰的一定是一个@Bean

3.如何使用。@Value是支持spel表达式的

3.1 大部分场景是Spel 表达式。在底层reader/process/writer 中使用@Value获取jobParamter/stepContext/jobContext

  • job参数:#{jobParameters[xy]}
  • job运行上下文:#{jobExecutionContext[xy]}
  • step运行上下文:#{stepExecutionContext[xy]}

3.2 SpEL引用bean

  • bean对象:#{car}
  • bean对象属性:#{car.brand}
  • bean对象方法:#{car.toString()}
  • 静态方法属性:#{T(java.lang.Math).PI}

3.3 系统属性

  • 系统变量:systemProperties
  • 环境变量:#{systemEnvironment['HOME']}

3.4 运算符号

  • if-else 运算符(三目运算符 ?:(temary), ?:(Elvis))
  • 比较运算符(< , > , == , >= , <= , lt , gt , eg , le , ge)
  • 逻辑运算符(and , or , not , |)
  • 正则表达式(#{admin.email matches ‘[a-zA-Z0-9._%±]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}’})
  • 算术运算符(+,-,*,/,%,^(加号还可以用作字符串连接))

4.可能遇到问题

  • 问题: Scope 'step' is not active for the current thread;

Error creating bean with name 'scopedTarget.demo01StepScopeStep': Scope 'step' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No context holder available for step scope

原因:看代码

@Bean
    public Job demo01StepScopeJob(){
        return jobBuilderFactory.get("demo01StepScopeJob")
                .start(demo01StepScopeStepError(null))
                .build();
    }
    /**
    ** 这个时候在 Step (demo01StepScopeStep) 中添加注解stepscope。
    ** Scope 'step' is not active for the current thread. 这个说的也很明白,step还没有装载初始化完成呢。
    ** 所以只有在step激活,即装载成功之后才能获取@Value 这种情况。我们可以把taskle 定义成个bean来获取
    **/
    @Bean
    @StepScope
    public Step demo01StepScopeStepError(@Value("${demo01.param.name}") String paramName){
        return stepBuilderFactory.get("demo01StepScopeStep")
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                        logger.info("=======demo01StepScopeStep======paramName:{}",paramName);
                        return null;
                    }
                }).build();
    }

    // 改造如下

    @Bean
    public Job demo01StepScopeJob(){
        return jobBuilderFactory.get("demo01StepScopeJob")
                .start(demo01StepScopeStep())
                .build();
    }

	//这个时候step上面的bean可以不要。因为stepScope只需要定义需要获取@Value的
    public Step demo01StepScopeStep(){
        return stepBuilderFactory.get("demo01StepScopeStep")
                .tasklet(demo01StepScopeTasklet(null))
                .build();
    }

    @Bean
    @StepScope  //这里的@StepScope 可以有也可以不用。因为@value只是在application.properties中的内容
    public Tasklet demo01StepScopeTasklet(@Value("${demo01.param.name}") String paramName){
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                logger.info("=======demo01StepScopeStep======paramName:{}",paramName);
                return null;
            }
        };
    }

这里面获取的是配置文件的内容。不是step内的特定内容。所以可以直接使用@Value在参数bean里。也可以直接在configuration内。

若使用jobParam参数内的@Value呢?那必须使@StepScope

	@Bean
    @StepScope
    public Tasklet demo01StepScopeTasklet(@Value("#{jobParameters[rnd]} ") String rnd){
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                logger.info("=======demo01StepScopeTasklet======rnd:{}",rnd);
                return null;
            }
        };
    }

5.StepScope原理

org.springframework.batch.core.scope.StepScope.java

看出stepScope实现了BeanFactoryPostProcessor。引入了bpp即实例化之前做扩展。这里是讲stepScope给注册到了spring容器中

获取bean的时候AbstractBeanFactory#doGetBean。判断bean不是mbd.isSingleton(),不是mbd.isPrototype(),若是自定义的即

mbd.getScope()。

那是如何获取的呢StepScope#get(String, ObjectFactory) ->这里面会获取StepContext内容。

stepContext=getContext() ->这里面其实是直接从ThreadLocal中获取内容。获取stepContext(这里面从有jobParam,stepContext)

那我们可以看到这个StepSynchronizationManager已经有获取的。那什么时候注册进去的呢?

AbstractStep#execute中在step的抽象类里面。执行doExecute(stepExecution);之前的方法doExecutionRegistration

这里面将stepExecution添加进ThreadLocal中。这里面可以认为是threadlocal(其实里面是一个栈可以存多个stepExecution!!!,兼容step套step那种的。)

其实这里已经能解答了。但还有个问题。@Value是如何从stepExecution中获取jobParm/stepContext/jobContext的

额外(通过@value是如何在那个阶段获取值的呢):

springboot启动过程中,有两个比较重要的过程,如下: 1 扫描,解析容器中的bean注册到beanFactory上去,就像是信息登记一样。 2 实例化、初始化这些扫描到的bean。

@Value的解析就是在第二个阶段。BeanPostProcessor定义了bean初始化前后用户可以对bean进行操作的接口方法,它的一个重要实现类AutowiredAnnotationBeanPostProcessor正如javadoc所说的那样,为bean中的@Autowired@Value注解的注入功能提供支持。下面是调用链

这里先简单介绍一下图上的几个类的作用。

AbstractAutowireCapableBeanFactory: 提供了bean创建,属性填充,自动装配,初始胡。支持自动装配构造函数,属性按名称和类型装配。实现了AutowireCapableBeanFactory接口定义的createBean方法。

AutowiredAnnotationBeanPostProcessor: 装配bean中使用注解标注的成员变量,setter方法, 任意的配置方法。比较典型的是@Autowired注解和@Value注解。

InjectionMetadata: 类的注入元数据,可能是类的方法或属性等,在AutowiredAnnotationBeanPostProcessor类中被使用。

AutowiredFieldElement: 是AutowiredAnnotationBeanPostProcessor的一个私有内部类,继承InjectionMetadata.InjectedElement,描述注解的字段。

StringValueResolver: 一个定义了处置字符串值的接口,只有一个接口方法resolveStringValue,可以用来解决占位符字符串。本文中的主要实现类在PropertySourcesPlaceholderConfigurer#processProperties方法中通过lamda表达式定义的。供ConfigurableBeanFactory类使用。

PropertySourcesPropertyResolver: 属性资源处理器,主要功能是获取PropertySources属性资源中的配置键值对。

PropertyPlaceholderHelper: 一个工具类,用来处理带有占位符的字符串。形如${name}的字符串在该工具类的帮助下,可以被用户提供的值所替代。替代途经可能通过Properties实例或者PlaceholderResolver(内部定义的接口)。

PropertyPlaceholderConfigurerResolver: 上一行所说的PlaceholderResolver接口的一个实现类,是PropertyPlaceholderConfigurer类的一个私有内部类。实现方法resolvePlaceholder中调用了外部类的resolvePlaceholder方法。

6.自定义一个scope

定义JakcssybinScope

/**
 * 定义在同一个线程内,多次获取同一个bean 获取的是同一个。
 * 如果不用该注解 获取的是不同的bean
 */
public class JackssybinScope implements Scope {

    private final ThreadLocal<Map<String, Object>> threadLoacal = new ThreadLocal<Map<String, Object>>() {
        @Override
        protected Map<String, Object> initialValue() {
            return new HashMap<String, Object>();
        }
    };

    public Object get(String name, ObjectFactory<?> objectFactory) {
        Map<String, Object> scope = threadLoacal.get();
        Object obj = scope.get(name);

        // 不存在则放入ThreadLocal
        if (obj == null) {
            obj = objectFactory.getObject();
            scope.put(name, obj);

            System.out.println("Not exists " + name + "; hashCode: " + obj.hashCode());
        } else {
            System.out.println("Exists " + name + "; hashCode: " + obj.hashCode());
        }

        return obj;
    }

    public Object remove(String name) {
        Map<String, Object> scope = threadLoacal.get();
        return scope.remove(name);
    }

    public String getConversationId() {
        return null;
    }

    public void registerDestructionCallback(String arg0, Runnable arg1) {
    }

    public Object resolveContextualObject(String arg0) {
        return null;
    }

}

注入jackssybinScope

@Component
public class JackssybinBPP implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("==============注册JackssybinBPP=========");
        beanFactory.registerScope("jackssybinScope", new JackssybinScope());
    }
}

引用

@Service
@Scope("jackssybinScope")
public class JackssybinHaveScopeService {
    public String getMessage() {
        return "Hello World!"+this.hashCode();
    }
}

未引用

@Service
public class JackssybinNoScopeService {
    public String getMessage() {
        return "Hello World!"+this.hashCode();
    }
}

测试

@SpringBootTest(classes = {Demo01StepScopeApplication.class})
public class JackssybinScopeTest {
    @Autowired
    ApplicationContext ctx;
    @Test
    public void jackssybinScopetest2(){
        JackssybinHaveScopeService service = ctx.getBean(JackssybinHaveScopeService.class);
        System.out.println(service.getMessage()+"="+service.hashCode());
        JackssybinHaveScopeService service2= ctx.getBean(JackssybinHaveScopeService.class);
        System.out.println(service2.getMessage()+"="+service2.hashCode());
        System.out.println("======================");
        JackssybinNoScopeService service3 = ctx.getBean(JackssybinNoScopeService.class);
        System.out.println(service3.getMessage()+"="+service3.hashCode());
        JackssybinNoScopeService service4= ctx.getBean(JackssybinNoScopeService.class);
        System.out.println(service4.getMessage()+"="+service4.hashCode());
    }
}

结果

Not exists jackssybinHaveScopeService; hashCode: 1842102517
Hello World!1842102517=1842102517
Exists jackssybinHaveScopeService; hashCode: 1842102517
Hello World!1842102517=1842102517
======================
Hello World!728236551=728236551
Hello World!728236551=728236551

结论:

  • jackssybinScope生效了。
  • ctx中的bean默认是单例的。

代码位置: github.com/jackssybin/…

到此这篇关于SpringBatch从入门到精通之StepScope作用域和用法详解的文章就介绍到这了,更多相关SpringBatch StepScope作用域和用法内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • SpringBatch适配器详解

    目录 一.SpringBatch适配器 二.SpringBatch适配器实战(Tasklet举例) 一.SpringBatch适配器 1.SpringBatch分别有读(reader).处理(processor).写(writer).tasklet处理器. 读适配器:ItemReaderAdapter 处理适配器:ItemProcessorAdapter 写适配器:ItemWriterAdapter tasklet适配器:MethodInvokingTaskletAdapter 2.Spring

  • springbatch的封装与使用实例详解

    Spring Batch官网介绍: A lightweight, comprehensive batch framework designed to enable the development of robust batch applications vital for the daily operations of enterprise systems.(一款轻量的.全面的批处理框架,用于开发强大的日常运营的企业级批处理应用程序.) springbatch 主要实现批量数据的处理,我对bat

  • 详解SpringBoot和SpringBatch 使用

    什么是Spring Batch Spring Batch 是一个轻量级的.完善的批处理框架,旨在帮助企业建立健壮.高效的批处理应用.Spring Batch是Spring的一个子项目,使用Java语言并基于Spring框架为基础开发,使的已经使用 Spring 框架的开发者或者企业更容易访问和利用企业服务. Spring Batch 提供了大量可重用的组件,包括了日志.追踪.事务.任务作业统计.任务重启.跳过.重复.资源管理.对于大数据量和高性能的批处理任务,Spring Batch 同样提供了

  • SpringBatch跳过异常和限制方式

    目录 SpringBatch容错处理 1. 案例说明 2. 跳过异常限制 SpringBatch 错误积累 1.如果nextStep在该JOB中还没有配置 SpringBatch容错处理 1. 案例说明 从DB中reader出1000条数据,chunk = 100,当第二个chunk出现NullPointerException或者StringIndexOutOfBoundsException异常.业务要求batch不终了,程序继续执行. 2. 跳过异常限制 下记有两种实现方法. 2.1 skip

  • SpringBatch从入门到精通之StepScope作用域和用法详解

    目录 1.StepSope 是一种scope 2.StepSope 是一种自定义step 3.如何使用.@Value是支持spel表达式的 3.1 大部分场景是Spel 表达式.在底层reader/process/writer 中使用@Value获取jobParamter/stepContext/jobContext 3.2 SpEL引用bean 3.3 系统属性 3.4 运算符号 4.可能遇到问题 5.StepScope原理 6.自定义一个scope 1.StepSope 是一种scope 在

  • python中for循环变量作用域及用法详解

    在讲这个话题前,首先我们来看一道题: 代码1: def foo(): return [lambda x: x**i for i in range(1,5,2)] print([f(3) for f in foo()]) 伙伴们,你们认为这里产生的结果是什么呢?我们再来看下这题的变体: 代码:2 def foo(): functions=[] for i in range(1,5,2): def inside_fun(x): return x ** i functions.append(insid

  • Vue.js slot插槽的作用域插槽用法详解

    目录 没有插槽的情况 Vue2.x 插槽 有插槽的情况 具名插槽 没有slot属性 插槽简单实例应用 作用域插槽 ( 2.1.0 新增 ) Vue3.x 插槽 插槽 作用域插槽 没有插槽的情况 <div id="app"> <child> <span>1111</span> </child> </div> <script> // 注册子组件 Vue.component("child"

  • Angular.js之作用域scope'@','=','&'实例详解

    什么是scope AngularJS 中,作用域是一个指向应用模型的对象,它是表达式的执行环境.作用域有层次结构,这个层次和相应的 DOM 几乎是一样的.作用域能监控表达式和传递事件. 在 HTML 代码中,一旦一个 ng-app 指令被定义,那么一个作用域就产生了,由 ng-app 所生成的作用域比较特殊,它是一个根作用域($rootScope),它是其他所有$Scope 的最顶层. 除了用 ng-app 指令可以产生一个作用域之外,其他的指令如 ng-controller,ng-repeat

  • javascript 作用于作用域链的详解

    javascript 作用于作用域链的详解 一.JavaScript作用域 任何程序设计语言都有作用域的概念,简单的说,作用域就是变量与函数的可访问范围,即作用域控制着变量与函数的可见性和生命周期.在JavaScript中,变量的作用域有全局作用域和局部作用域两种. 全局作用域(Global Scope) 在代码中任何地方都能访问到的对象拥有全局作用域,一般来说一下几种情形拥有全局作用域: (1)最外层函数和在最外层函数外面定义的变量拥有全局作用域, 例如: var authorName="Bu

  • JavaBean四个作用域范围的详解

    JavaBean四个作用域范围的详解 一 说明 使用useBeans的scope属性可以用来指定javabean的作用范围. 二 四个作用范围 三 代码 1.login.jsp <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" %> <% String path = request.getContextPath

  • go语言入门环境搭建及GoLand安装教程详解

    Go(又称 Golang)是 Google 的 Robert Griesemer,Rob Pike 及 Ken Thompson 开发的一种静态强类型.编译型语言.Go 语言语法与 C 相近,但功能上有:内存安全,GC(垃圾回收),结构形态及 CSP-style 并发计算. Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.

  • C++中的STL中map用法详解(零基础入门)

    目录 一.什么是 map ? 二.map的定义 2.1 头文件 2.2 定义 2.3 方法 三.实例讲解 3.1 增加数据 3.2 删除数据 3.3 修改数据 3.4 查找数据 3.5 遍历元素 3.6 其它方法 四.总结 map 在编程中是经常使用的一个容器,本文来讲解一下 STL 中的 map,赶紧来看下吧! 一.什么是 map ? map 是具有唯一键值对的容器,通常使用红黑树实现. map 中的键值对是 key value 的形式,比如:每个身份证号对应一个人名(反过来不成立哦!),其中

  • C语言入门篇--注释,关键字typedef及转义字符详解

    目录 注释 1.注释意义 2.两种注释风格 2.1 C语言注释风格 2.2 C++注释风格 关键字typedef 1.注意 2.用法 语法结构 转义字符 1.转义字符及其含义 2.字面 转 特殊 3.特殊 转 字面 注释 1.注释意义 (1)代码中有不需要的代码可以直接删除,也可以注释掉. (2)有些代码比较难懂可以注释一下. 2.两种注释风格 2.1 C语言注释风格 /*xxxxxx*/ 一次可以注释一行或多行,但不能嵌套注释. eg: #include <stdio.h> int main

  • Android入门教程之ListView的具体使用详解

    目录 ListView 的简单用法 定制 ListView 的界面 提升 ListView 的运行效率 ListView 的点击事件 ListView 的简单用法 在布局中加入 ListView 控件还算简单,先为 ListView 指定一个 id,然后将宽度和高度都设置为 match_parent,这样 ListView 就占满了整个布局的空间 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&

随机推荐