Skip to content

Commit 184ac31

Browse files
committed
Fix job registration in MapJobRegistry
Before this commit, jobs were incorrectly registered by their bean names while they should be registered by their names. Resolves #5122
1 parent 4216a0a commit 184ac31

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/MapJobRegistry.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2025 the original author or authors.
2+
* Copyright 2006-present the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -63,7 +63,14 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
6363
@Override
6464
public void afterSingletonsInstantiated() {
6565
Map<String, Job> jobBeans = this.applicationContext.getBeansOfType(Job.class);
66-
this.map.putAll(jobBeans);
66+
for (Job job : jobBeans.values()) {
67+
try {
68+
register(job);
69+
}
70+
catch (DuplicateJobException e) {
71+
throw new IllegalStateException("Unable to register job " + job.getName(), e);
72+
}
73+
}
6774
}
6875

6976
@Override

spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/JobRegistryIntegrationTests.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,24 @@
1616
package org.springframework.batch.core.configuration.support;
1717

1818
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertThrows;
1920

21+
import org.junit.jupiter.api.Assertions;
2022
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.api.function.Executable;
2124

2225
import org.springframework.batch.core.configuration.DuplicateJobException;
23-
import org.springframework.batch.core.job.Job;
26+
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
27+
import org.springframework.batch.core.job.*;
2428
import org.springframework.batch.core.configuration.JobRegistry;
25-
import org.springframework.batch.core.job.SimpleJob;
29+
import org.springframework.batch.core.job.builder.JobBuilder;
30+
import org.springframework.batch.core.repository.JobRepository;
31+
import org.springframework.batch.core.step.StepSupport;
2632
import org.springframework.beans.factory.annotation.Autowired;
33+
import org.springframework.context.ApplicationContext;
34+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
35+
import org.springframework.context.annotation.Bean;
36+
import org.springframework.context.annotation.Configuration;
2737
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
2838

2939
/**
@@ -44,4 +54,31 @@ void testRegistry() throws DuplicateJobException {
4454
assertEquals(job.getName(), jobRegistry.getJobNames().iterator().next());
4555
}
4656

57+
@Test
58+
void testDuplicateJobRegistration() {
59+
assertThrows(IllegalStateException.class,
60+
() -> new AnnotationConfigApplicationContext(JobConfigurationWithDuplicateJobs.class));
61+
}
62+
63+
@Configuration
64+
@EnableBatchProcessing
65+
static class JobConfigurationWithDuplicateJobs {
66+
67+
@Bean
68+
Job job1() {
69+
return new JobSupport("sameJobNameOnPurpose");
70+
}
71+
72+
@Bean
73+
Job job2() {
74+
return new JobSupport("sameJobNameOnPurpose");
75+
}
76+
77+
@Bean
78+
public JobRegistry jobRegistry() {
79+
return new MapJobRegistry();
80+
}
81+
82+
}
83+
4784
}

0 commit comments

Comments
 (0)