Skip to content

Commit

Permalink
Fix TARGET_NAME_PREFIX in StepScope
Browse files Browse the repository at this point in the history
Resolves #3936
  • Loading branch information
farnetto authored and fmbenhassine committed Feb 21, 2023
1 parent ab9dbc3 commit 5fae331
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2013 the original author or authors.
* Copyright 2006-2023 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.
Expand Down Expand Up @@ -63,7 +63,7 @@
*/
public class StepScope extends BatchScopeSupport {

private static final String TARGET_NAME_PREFIX = "stepScopedTarget.";
private static final String TARGET_NAME_PREFIX = "scopedTarget.";

private Log logger = LogFactory.getLog(getClass());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2022 the original author or authors.
* Copyright 2006-2023 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.
Expand All @@ -16,9 +16,12 @@

package org.springframework.batch.core.configuration.annotation;

import java.util.concurrent.Callable;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.scope.context.ChunkContext;
Expand All @@ -38,8 +41,6 @@
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.lang.Nullable;

import java.util.concurrent.Callable;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -101,6 +102,19 @@ void testStepScopeXmlImportUsingNamespace() throws Exception {
assertEquals("STEP", value.call());
}

/**
* @see org.springframework.batch.core.configuration.xml.CoreNamespaceUtils#autoregisterBeansForNamespace
*/
@Test
public void testStepScopeUsingNamespaceAutoregisterBeans() throws Exception {
init(StepScopeConfigurationTestsUsingNamespaceAutoregisterBeans.class);

ISimpleHolder value = (ISimpleHolder) context.getBean("xmlValue");
assertEquals("STEP", value.call());
value = (ISimpleHolder) context.getBean("javaValue");
assertEquals("STEP", value.call());
}

@Test
void testStepScopeWithProxyTargetClassInjected() throws Exception {
init(StepScopeConfigurationInjectingProxy.class);
Expand Down Expand Up @@ -198,7 +212,13 @@ public String call() throws Exception {

}

public static class SimpleHolder {
public static interface ISimpleHolder {

String call() throws Exception;

}

public static class SimpleHolder implements ISimpleHolder {

private final String value;

Expand Down Expand Up @@ -243,6 +263,18 @@ protected SimpleHolder javaValue(@Value("#{stepExecution.stepName}") final Strin

}

@Configuration
@ImportResource("org/springframework/batch/core/configuration/annotation/StepScopeConfigurationTestsUsingNamespaceAutoregisterBeans-context.xml")
public static class StepScopeConfigurationTestsUsingNamespaceAutoregisterBeans {

@Bean
@StepScope
protected SimpleHolder javaValue(@Value("#{stepExecution.stepName}") final String value) {
return new SimpleHolder(value);
}

}

@Configuration
@EnableBatchProcessing
public static class StepScopeConfigurationInjectingProxy {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:batch="http://www.springframework.org/schema/batch"
xsi:schemaLocation="
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/batch https://www.springframework.org/schema/batch/spring-batch.xsd">

<bean id="tasklet"
class="org.springframework.batch.core.configuration.annotation.StepScopeConfigurationTests$TaskletSupport" />

<batch:job id="job">
<batch:step id="step1">
<batch:tasklet ref="tasklet" />
</batch:step>
</batch:job>

<bean id="xmlValue"
class="org.springframework.batch.core.configuration.annotation.StepScopeConfigurationTests.SimpleHolder"
scope="step">
<constructor-arg value="#{stepExecution.stepName}" />
</bean>

<batch:job-repository id="jobRepository" />

<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>

0 comments on commit 5fae331

Please sign in to comment.