Skip to content

Commit 701d53d

Browse files
committed
Use typesafe variant of ctx::getBean instead
Signed-off-by: Yanming Zhou <[email protected]>
1 parent 2bd5b84 commit 701d53d

22 files changed

+63
-63
lines changed

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/DefaultJobLoader.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2023 the original author or authors.
2+
* Copyright 2006-2025 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.
@@ -173,7 +173,7 @@ private Collection<Job> doLoad(ApplicationContextFactory factory, boolean unregi
173173

174174
if (!autoRegistrationDetected) {
175175

176-
Job job = (Job) context.getBean(name);
176+
Job job = context.getBean(name, Job.class);
177177
String jobName = job.getName();
178178

179179
// On reload try to unregister first

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/CoreNamespacePostProcessor.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2023 the original author or authors.
2+
* Copyright 2006-2025 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.
@@ -119,19 +119,19 @@ private Object injectDefaults(Object bean) {
119119
JobParserJobFactoryBean fb = (JobParserJobFactoryBean) bean;
120120
JobRepository jobRepository = fb.getJobRepository();
121121
if (jobRepository == null) {
122-
fb.setJobRepository((JobRepository) applicationContext.getBean(DEFAULT_JOB_REPOSITORY_NAME));
122+
fb.setJobRepository(applicationContext.getBean(DEFAULT_JOB_REPOSITORY_NAME, JobRepository.class));
123123
}
124124
}
125125
else if (bean instanceof StepParserStepFactoryBean) {
126126
StepParserStepFactoryBean<?, ?> fb = (StepParserStepFactoryBean<?, ?>) bean;
127127
JobRepository jobRepository = fb.getJobRepository();
128128
if (jobRepository == null) {
129-
fb.setJobRepository((JobRepository) applicationContext.getBean(DEFAULT_JOB_REPOSITORY_NAME));
129+
fb.setJobRepository(applicationContext.getBean(DEFAULT_JOB_REPOSITORY_NAME, JobRepository.class));
130130
}
131131
PlatformTransactionManager transactionManager = fb.getTransactionManager();
132132
if (transactionManager == null && fb.requiresTransactionManager()) {
133133
fb.setTransactionManager(
134-
(PlatformTransactionManager) applicationContext.getBean(DEFAULT_TRANSACTION_MANAGER_NAME));
134+
applicationContext.getBean(DEFAULT_TRANSACTION_MANAGER_NAME, PlatformTransactionManager.class));
135135
}
136136
}
137137
return bean;

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/CommandLineJobRunner.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2023 the original author or authors.
2+
* Copyright 2006-2025 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.
@@ -356,7 +356,7 @@ int start(String jobPath, String jobIdentifier, String[] parameters, Set<String>
356356
}
357357
}
358358
if (job == null) {
359-
job = (Job) context.getBean(jobName);
359+
job = context.getBean(jobName, Job.class);
360360
}
361361

362362
if (opts.contains("-next")) {

Diff for: spring-batch-core/src/test/java/org/springframework/batch/core/SpringBeanJobTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2025 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.
@@ -50,7 +50,7 @@ void testBeanNameWithBeanDefinition() {
5050
context.registerBeanDefinition("bean", new RootBeanDefinition(JobSupport.class, args, null));
5151

5252
context.refresh();
53-
JobSupport configuration = (JobSupport) context.getBean("bean");
53+
JobSupport configuration = context.getBean("bean", JobSupport.class);
5454
assertNotNull(configuration.getName());
5555
assertEquals("foo", configuration.getName());
5656
configuration.setBeanName("bar");
@@ -66,7 +66,7 @@ void testBeanNameWithParentBeanDefinition() {
6666
context.registerBeanDefinition("parent", new RootBeanDefinition(JobSupport.class, args, null));
6767
context.registerBeanDefinition("bean", new ChildBeanDefinition("parent"));
6868
context.refresh();
69-
JobSupport configuration = (JobSupport) context.getBean("bean");
69+
JobSupport configuration = context.getBean("bean", JobSupport.class);
7070
assertNotNull(configuration.getName());
7171
assertEquals("bar", configuration.getName());
7272
configuration.setBeanName("foo");

Diff for: spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/JobScopeConfigurationTests.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2023 the original author or authors.
2+
* Copyright 2006-2025 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.
@@ -82,7 +82,7 @@ void testXmlJobScopeWithInheritance() throws Exception {
8282
context = new ClassPathXmlApplicationContext(
8383
"org/springframework/batch/core/configuration/annotation/JobScopeConfigurationTestsInheritance-context.xml");
8484
JobSynchronizationManager.register(jobExecution);
85-
SimpleHolder value = (SimpleHolder) context.getBean("child");
85+
SimpleHolder value = context.getBean("child", SimpleHolder.class);
8686
assertEquals("JOB", value.call());
8787
}
8888

@@ -97,9 +97,9 @@ void testJobScopeWithProxyTargetClass() throws Exception {
9797
void testStepScopeXmlImportUsingNamespace() throws Exception {
9898
init(JobScopeConfigurationXmlImportUsingNamespace.class);
9999

100-
SimpleHolder value = (SimpleHolder) context.getBean("xmlValue");
100+
SimpleHolder value = context.getBean("xmlValue", SimpleHolder.class);
101101
assertEquals("JOB", value.call());
102-
value = (SimpleHolder) context.getBean("javaValue");
102+
value = context.getBean("javaValue", SimpleHolder.class);
103103
assertEquals("JOB", value.call());
104104
}
105105

Diff for: spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/StepScopeConfigurationTests.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2023 the original author or authors.
2+
* Copyright 2006-2025 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.
@@ -81,7 +81,7 @@ void testXmlStepScopeWithInheritance() throws Exception {
8181
context = new ClassPathXmlApplicationContext(
8282
"org/springframework/batch/core/configuration/annotation/StepScopeConfigurationTestsInheritance-context.xml");
8383
StepSynchronizationManager.register(stepExecution);
84-
SimpleHolder value = (SimpleHolder) context.getBean("child");
84+
SimpleHolder value = context.getBean("child", SimpleHolder.class);
8585
assertEquals("STEP", value.call());
8686
}
8787

@@ -96,9 +96,9 @@ void testStepScopeWithProxyTargetClass() throws Exception {
9696
void testStepScopeXmlImportUsingNamespace() throws Exception {
9797
init(StepScopeConfigurationXmlImportUsingNamespace.class);
9898

99-
SimpleHolder value = (SimpleHolder) context.getBean("xmlValue");
99+
SimpleHolder value = context.getBean("xmlValue", SimpleHolder.class);
100100
assertEquals("STEP", value.call());
101-
value = (SimpleHolder) context.getBean("javaValue");
101+
value = context.getBean("javaValue", SimpleHolder.class);
102102
assertEquals("STEP", value.call());
103103
}
104104

@@ -109,9 +109,9 @@ void testStepScopeXmlImportUsingNamespace() throws Exception {
109109
public void testStepScopeUsingNamespaceAutoregisterBeans() throws Exception {
110110
init(StepScopeConfigurationTestsUsingNamespaceAutoregisterBeans.class);
111111

112-
ISimpleHolder value = (ISimpleHolder) context.getBean("xmlValue");
112+
ISimpleHolder value = context.getBean("xmlValue", ISimpleHolder.class);
113113
assertEquals("STEP", value.call());
114-
value = (ISimpleHolder) context.getBean("javaValue");
114+
value = context.getBean("javaValue", ISimpleHolder.class);
115115
assertEquals("STEP", value.call());
116116
}
117117

Diff for: spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/JobRegistryBeanPostProcessorTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2023 the original author or authors.
2+
* Copyright 2006-2025 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.
@@ -102,7 +102,7 @@ void testUnregisterOnDestroy() throws Exception {
102102
@Test
103103
void testExecutionWithApplicationContext() throws Exception {
104104
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("test-context.xml", getClass());
105-
MapJobRegistry registry = (MapJobRegistry) context.getBean("registry");
105+
MapJobRegistry registry = context.getBean("registry", MapJobRegistry.class);
106106
Collection<String> configurations = registry.getJobNames();
107107
String[] names = context.getBeanNamesForType(JobSupport.class);
108108
int count = names.length;

Diff for: spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/PartitionStepParserTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2023 the original author or authors.
2+
* Copyright 2006-2025 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.
@@ -145,7 +145,7 @@ void testNestedPartitionStepStepReference() throws Throwable {
145145
String stepExecutionName = se.getStepName();
146146
// the partitioned step
147147
if (stepExecutionName.equalsIgnoreCase("j3s1")) {
148-
PartitionStep partitionStep = (PartitionStep) this.applicationContext.getBean(stepExecutionName);
148+
PartitionStep partitionStep = this.applicationContext.getBean(stepExecutionName, PartitionStep.class);
149149
// prove that the reference in the {@link
150150
// TaskExecutorPartitionHandler} is the step configured inline
151151
TaskExecutorPartitionHandler taskExecutorPartitionHandler = accessPrivateField(partitionStep,
@@ -184,7 +184,7 @@ void testNestedPartitionStep() throws Throwable {
184184
String stepExecutionName = se.getStepName();
185185
if (stepExecutionName.equalsIgnoreCase("j4s1")) { // the partitioned
186186
// step
187-
PartitionStep partitionStep = (PartitionStep) this.applicationContext.getBean(stepExecutionName);
187+
PartitionStep partitionStep = this.applicationContext.getBean(stepExecutionName, PartitionStep.class);
188188

189189
// prove that the reference in the {@link
190190
// TaskExecutorPartitionHandler} is the step configured inline

Diff for: spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/StepListenerInStepParserTests.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -45,7 +45,7 @@ class StepListenerInStepParserTests {
4545

4646
@Test
4747
void testListenersAtStepLevel() throws Exception {
48-
Step step = (Step) beanFactory.getBean("s1");
48+
Step step = beanFactory.getBean("s1", Step.class);
4949
List<?> list = getListeners(step);
5050
assertEquals(1, list.size());
5151
assertTrue(list.get(0) instanceof DummyStepExecutionListener);
@@ -54,15 +54,15 @@ void testListenersAtStepLevel() throws Exception {
5454
@Test
5555
// TODO: BATCH-1689 (expected=BeanCreationException.class)
5656
void testListenersAtStepLevelWrongType() throws Exception {
57-
Step step = (Step) beanFactory.getBean("s2");
57+
Step step = beanFactory.getBean("s2", Step.class);
5858
List<?> list = getListeners(step);
5959
assertEquals(1, list.size());
6060
assertTrue(list.get(0) instanceof DummyChunkListener);
6161
}
6262

6363
@Test
6464
void testListenersAtTaskletAndStepLevels() throws Exception {
65-
Step step = (Step) beanFactory.getBean("s3");
65+
Step step = beanFactory.getBean("s3", Step.class);
6666
List<?> list = getListeners(step);
6767
assertEquals(2, list.size());
6868
assertTrue(list.get(0) instanceof DummyStepExecutionListener);
@@ -71,7 +71,7 @@ void testListenersAtTaskletAndStepLevels() throws Exception {
7171

7272
@Test
7373
void testListenersAtChunkAndStepLevels() throws Exception {
74-
Step step = (Step) beanFactory.getBean("s4");
74+
Step step = beanFactory.getBean("s4", Step.class);
7575
List<?> list = getListeners(step);
7676
assertEquals(2, list.size());
7777
assertTrue(list.get(0) instanceof DummyStepExecutionListener);

Diff for: spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/StepNameTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2025 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.
@@ -61,7 +61,7 @@ void testStepNames(Resource resource) throws Exception {
6161
for (String name : stepLocators.keySet()) {
6262
StepLocator stepLocator = stepLocators.get(name);
6363
Collection<String> stepNames = stepLocator.getStepNames();
64-
Job job = (Job) context.getBean(name);
64+
Job job = context.getBean(name, Job.class);
6565
String jobName = job.getName();
6666
assertFalse(stepNames.isEmpty(), "Job has no steps: " + jobName);
6767
for (String registeredName : stepNames) {

Diff for: spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/StepParserTests.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2023 the original author or authors.
2+
* Copyright 2006-2025 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.
@@ -98,7 +98,7 @@ void testStepParserBeanName() {
9898
"org/springframework/batch/core/configuration/xml/StepParserBeanNameTests-context.xml");
9999
Map<String, Step> beans = ctx.getBeansOfType(Step.class);
100100
assertTrue(beans.containsKey("s1"), "'s1' bean not found");
101-
Step s1 = (Step) ctx.getBean("s1");
101+
Step s1 = ctx.getBean("s1", Step.class);
102102
assertEquals("s1", s1.getName(), "wrong name");
103103
}
104104

@@ -114,7 +114,7 @@ void testStepParserCommitInterval() throws Exception {
114114
"org/springframework/batch/core/configuration/xml/StepParserCommitIntervalTests-context.xml");
115115
Map<String, Step> beans = ctx.getBeansOfType(Step.class);
116116
assertTrue(beans.containsKey("s1"), "'s1' bean not found");
117-
Step s1 = (Step) ctx.getBean("s1");
117+
Step s1 = ctx.getBean("s1", Step.class);
118118
CompletionPolicy completionPolicy = getCompletionPolicy(s1);
119119
assertTrue(completionPolicy instanceof SimpleCompletionPolicy);
120120
assertEquals(25, ReflectionTestUtils.getField(completionPolicy, "chunkSize"));
@@ -126,7 +126,7 @@ void testStepParserCompletionPolicy() throws Exception {
126126
"org/springframework/batch/core/configuration/xml/StepParserCompletionPolicyTests-context.xml");
127127
Map<String, Step> beans = ctx.getBeansOfType(Step.class);
128128
assertTrue(beans.containsKey("s1"), "'s1' bean not found");
129-
Step s1 = (Step) ctx.getBean("s1");
129+
Step s1 = ctx.getBean("s1", Step.class);
130130
CompletionPolicy completionPolicy = getCompletionPolicy(s1);
131131
assertTrue(completionPolicy instanceof DummyCompletionPolicy);
132132
}
@@ -212,7 +212,7 @@ private void validateTransactionAttributesInherited(String stepName, Application
212212
@SuppressWarnings("unchecked")
213213
private List<StepExecutionListener> getListeners(String stepName, ApplicationContext ctx) throws Exception {
214214
assertTrue(ctx.containsBean(stepName));
215-
Step step = (Step) ctx.getBean(stepName);
215+
Step step = ctx.getBean(stepName, Step.class);
216216
assertTrue(step instanceof TaskletStep);
217217
Object compositeListener = ReflectionTestUtils.getField(step, "stepExecutionListener");
218218
Object composite = ReflectionTestUtils.getField(compositeListener, "list");
@@ -236,7 +236,7 @@ private StepExecutionListener getListener(String stepName, ApplicationContext ct
236236

237237
private DefaultTransactionAttribute getTransactionAttribute(ApplicationContext ctx, String stepName) {
238238
assertTrue(ctx.containsBean(stepName));
239-
Step step = (Step) ctx.getBean(stepName);
239+
Step step = ctx.getBean(stepName, Step.class);
240240
assertTrue(step instanceof TaskletStep);
241241
Object transactionAttribute = ReflectionTestUtils.getField(step, "transactionAttribute");
242242
return (DefaultTransactionAttribute) transactionAttribute;
@@ -252,7 +252,7 @@ void testInheritFromBean() {
252252

253253
private Tasklet getTasklet(String stepName, ApplicationContext ctx) {
254254
assertTrue(ctx.containsBean(stepName));
255-
Step step = (Step) ctx.getBean(stepName);
255+
Step step = ctx.getBean(stepName, Step.class);
256256
assertTrue(step instanceof TaskletStep);
257257
Object tasklet = ReflectionTestUtils.getField(step, "tasklet");
258258
assertTrue(tasklet instanceof Tasklet);

Diff for: spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/TaskletStepAllowStartIfCompleteTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2022 the original author or authors.
2+
* Copyright 2013-2025 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.
@@ -46,7 +46,7 @@ class TaskletStepAllowStartIfCompleteTests {
4646
@Test
4747
void test() throws Exception {
4848
// retrieve the step from the context and see that it's allow is set
49-
AbstractStep abstractStep = (AbstractStep) context.getBean("simpleJob.step1");
49+
AbstractStep abstractStep = context.getBean("simpleJob.step1", AbstractStep.class);
5050
assertTrue(abstractStep.isAllowStartIfComplete());
5151
}
5252

Diff for: spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/JdbcStepExecutionDaoTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2023 the original author or authors.
2+
* Copyright 2008-2025 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.
@@ -31,14 +31,14 @@ class JdbcStepExecutionDaoTests extends AbstractStepExecutionDaoTests {
3131

3232
@Override
3333
protected StepExecutionDao getStepExecutionDao() {
34-
return (StepExecutionDao) applicationContext.getBean("stepExecutionDao");
34+
return applicationContext.getBean("stepExecutionDao", StepExecutionDao.class);
3535
}
3636

3737
@Override
3838
protected JobRepository getJobRepository() {
3939
deleteFromTables("BATCH_JOB_EXECUTION_CONTEXT", "BATCH_STEP_EXECUTION_CONTEXT", "BATCH_STEP_EXECUTION",
4040
"BATCH_JOB_EXECUTION_PARAMS", "BATCH_JOB_EXECUTION", "BATCH_JOB_INSTANCE");
41-
return (JobRepository) applicationContext.getBean("jobRepository");
41+
return applicationContext.getBean("jobRepository", JobRepository.class);
4242
}
4343

4444
/**

Diff for: spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantExceptionClassesTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2022 the original author or authors.
2+
* Copyright 2009-2025 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.
@@ -339,7 +339,7 @@ private StepExecution launchStep(String stepName) throws Exception {
339339
job.setJobRepository(jobRepository);
340340

341341
List<Step> stepsToExecute = new ArrayList<>();
342-
stepsToExecute.add((Step) applicationContext.getBean(stepName));
342+
stepsToExecute.add(applicationContext.getBean(stepName, Step.class));
343343
job.setSteps(stepsToExecute);
344344

345345
JobExecution jobExecution = jobLauncher.run(job,

Diff for: spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/AbstractDatabaseItemStreamItemReaderTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2023 the original author or authors.
2+
* Copyright 2009-2025 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.
@@ -70,7 +70,7 @@ void testReadToExhaustion() throws Exception {
7070
}
7171

7272
protected DataSource getDataSource() {
73-
return (DataSource) ctx.getBean("dataSource");
73+
return ctx.getBean("dataSource", DataSource.class);
7474
}
7575

7676
}

0 commit comments

Comments
 (0)