Skip to content

Use typesafe variant of ctx::getBean instead #4803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2023 the original author or authors.
* Copyright 2006-2025 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 @@ -173,7 +173,7 @@ private Collection<Job> doLoad(ApplicationContextFactory factory, boolean unregi

if (!autoRegistrationDetected) {

Job job = (Job) context.getBean(name);
Job job = context.getBean(name, Job.class);
String jobName = job.getName();

// On reload try to unregister first
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2023 the original author or authors.
* Copyright 2006-2025 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 @@ -119,19 +119,19 @@ private Object injectDefaults(Object bean) {
JobParserJobFactoryBean fb = (JobParserJobFactoryBean) bean;
JobRepository jobRepository = fb.getJobRepository();
if (jobRepository == null) {
fb.setJobRepository((JobRepository) applicationContext.getBean(DEFAULT_JOB_REPOSITORY_NAME));
fb.setJobRepository(applicationContext.getBean(DEFAULT_JOB_REPOSITORY_NAME, JobRepository.class));
}
}
else if (bean instanceof StepParserStepFactoryBean) {
StepParserStepFactoryBean<?, ?> fb = (StepParserStepFactoryBean<?, ?>) bean;
JobRepository jobRepository = fb.getJobRepository();
if (jobRepository == null) {
fb.setJobRepository((JobRepository) applicationContext.getBean(DEFAULT_JOB_REPOSITORY_NAME));
fb.setJobRepository(applicationContext.getBean(DEFAULT_JOB_REPOSITORY_NAME, JobRepository.class));
}
PlatformTransactionManager transactionManager = fb.getTransactionManager();
if (transactionManager == null && fb.requiresTransactionManager()) {
fb.setTransactionManager(
(PlatformTransactionManager) applicationContext.getBean(DEFAULT_TRANSACTION_MANAGER_NAME));
applicationContext.getBean(DEFAULT_TRANSACTION_MANAGER_NAME, PlatformTransactionManager.class));
}
}
return bean;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2023 the original author or authors.
* Copyright 2006-2025 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 @@ -356,7 +356,7 @@ int start(String jobPath, String jobIdentifier, String[] parameters, Set<String>
}
}
if (job == null) {
job = (Job) context.getBean(jobName);
job = context.getBean(jobName, Job.class);
}

if (opts.contains("-next")) {
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-2025 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 @@ -50,7 +50,7 @@ void testBeanNameWithBeanDefinition() {
context.registerBeanDefinition("bean", new RootBeanDefinition(JobSupport.class, args, null));

context.refresh();
JobSupport configuration = (JobSupport) context.getBean("bean");
JobSupport configuration = context.getBean("bean", JobSupport.class);
assertNotNull(configuration.getName());
assertEquals("foo", configuration.getName());
configuration.setBeanName("bar");
Expand All @@ -66,7 +66,7 @@ void testBeanNameWithParentBeanDefinition() {
context.registerBeanDefinition("parent", new RootBeanDefinition(JobSupport.class, args, null));
context.registerBeanDefinition("bean", new ChildBeanDefinition("parent"));
context.refresh();
JobSupport configuration = (JobSupport) context.getBean("bean");
JobSupport configuration = context.getBean("bean", JobSupport.class);
assertNotNull(configuration.getName());
assertEquals("bar", configuration.getName());
configuration.setBeanName("foo");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2023 the original author or authors.
* Copyright 2006-2025 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 @@ -82,7 +82,7 @@ void testXmlJobScopeWithInheritance() throws Exception {
context = new ClassPathXmlApplicationContext(
"org/springframework/batch/core/configuration/annotation/JobScopeConfigurationTestsInheritance-context.xml");
JobSynchronizationManager.register(jobExecution);
SimpleHolder value = (SimpleHolder) context.getBean("child");
SimpleHolder value = context.getBean("child", SimpleHolder.class);
assertEquals("JOB", value.call());
}

Expand All @@ -97,9 +97,9 @@ void testJobScopeWithProxyTargetClass() throws Exception {
void testStepScopeXmlImportUsingNamespace() throws Exception {
init(JobScopeConfigurationXmlImportUsingNamespace.class);

SimpleHolder value = (SimpleHolder) context.getBean("xmlValue");
SimpleHolder value = context.getBean("xmlValue", SimpleHolder.class);
assertEquals("JOB", value.call());
value = (SimpleHolder) context.getBean("javaValue");
value = context.getBean("javaValue", SimpleHolder.class);
assertEquals("JOB", value.call());
}

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

Expand All @@ -96,9 +96,9 @@ void testStepScopeWithProxyTargetClass() throws Exception {
void testStepScopeXmlImportUsingNamespace() throws Exception {
init(StepScopeConfigurationXmlImportUsingNamespace.class);

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

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2023 the original author or authors.
* Copyright 2006-2025 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 @@ -102,7 +102,7 @@ void testUnregisterOnDestroy() throws Exception {
@Test
void testExecutionWithApplicationContext() throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("test-context.xml", getClass());
MapJobRegistry registry = (MapJobRegistry) context.getBean("registry");
MapJobRegistry registry = context.getBean("registry", MapJobRegistry.class);
Collection<String> configurations = registry.getJobNames();
String[] names = context.getBeanNamesForType(JobSupport.class);
int count = names.length;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2023 the original author or authors.
* Copyright 2006-2025 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 @@ -145,7 +145,7 @@ void testNestedPartitionStepStepReference() throws Throwable {
String stepExecutionName = se.getStepName();
// the partitioned step
if (stepExecutionName.equalsIgnoreCase("j3s1")) {
PartitionStep partitionStep = (PartitionStep) this.applicationContext.getBean(stepExecutionName);
PartitionStep partitionStep = this.applicationContext.getBean(stepExecutionName, PartitionStep.class);
// prove that the reference in the {@link
// TaskExecutorPartitionHandler} is the step configured inline
TaskExecutorPartitionHandler taskExecutorPartitionHandler = accessPrivateField(partitionStep,
Expand Down Expand Up @@ -184,7 +184,7 @@ void testNestedPartitionStep() throws Throwable {
String stepExecutionName = se.getStepName();
if (stepExecutionName.equalsIgnoreCase("j4s1")) { // the partitioned
// step
PartitionStep partitionStep = (PartitionStep) this.applicationContext.getBean(stepExecutionName);
PartitionStep partitionStep = this.applicationContext.getBean(stepExecutionName, PartitionStep.class);

// prove that the reference in the {@link
// TaskExecutorPartitionHandler} is the step configured inline
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2025 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 @@ -45,7 +45,7 @@ class StepListenerInStepParserTests {

@Test
void testListenersAtStepLevel() throws Exception {
Step step = (Step) beanFactory.getBean("s1");
Step step = beanFactory.getBean("s1", Step.class);
List<?> list = getListeners(step);
assertEquals(1, list.size());
assertTrue(list.get(0) instanceof DummyStepExecutionListener);
Expand All @@ -54,15 +54,15 @@ void testListenersAtStepLevel() throws Exception {
@Test
// TODO: BATCH-1689 (expected=BeanCreationException.class)
void testListenersAtStepLevelWrongType() throws Exception {
Step step = (Step) beanFactory.getBean("s2");
Step step = beanFactory.getBean("s2", Step.class);
List<?> list = getListeners(step);
assertEquals(1, list.size());
assertTrue(list.get(0) instanceof DummyChunkListener);
}

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

@Test
void testListenersAtChunkAndStepLevels() throws Exception {
Step step = (Step) beanFactory.getBean("s4");
Step step = beanFactory.getBean("s4", Step.class);
List<?> list = getListeners(step);
assertEquals(2, list.size());
assertTrue(list.get(0) instanceof DummyStepExecutionListener);
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-2025 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 @@ -61,7 +61,7 @@ void testStepNames(Resource resource) throws Exception {
for (String name : stepLocators.keySet()) {
StepLocator stepLocator = stepLocators.get(name);
Collection<String> stepNames = stepLocator.getStepNames();
Job job = (Job) context.getBean(name);
Job job = context.getBean(name, Job.class);
String jobName = job.getName();
assertFalse(stepNames.isEmpty(), "Job has no steps: " + jobName);
for (String registeredName : stepNames) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2023 the original author or authors.
* Copyright 2006-2025 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 @@ -98,7 +98,7 @@ void testStepParserBeanName() {
"org/springframework/batch/core/configuration/xml/StepParserBeanNameTests-context.xml");
Map<String, Step> beans = ctx.getBeansOfType(Step.class);
assertTrue(beans.containsKey("s1"), "'s1' bean not found");
Step s1 = (Step) ctx.getBean("s1");
Step s1 = ctx.getBean("s1", Step.class);
assertEquals("s1", s1.getName(), "wrong name");
}

Expand All @@ -114,7 +114,7 @@ void testStepParserCommitInterval() throws Exception {
"org/springframework/batch/core/configuration/xml/StepParserCommitIntervalTests-context.xml");
Map<String, Step> beans = ctx.getBeansOfType(Step.class);
assertTrue(beans.containsKey("s1"), "'s1' bean not found");
Step s1 = (Step) ctx.getBean("s1");
Step s1 = ctx.getBean("s1", Step.class);
CompletionPolicy completionPolicy = getCompletionPolicy(s1);
assertTrue(completionPolicy instanceof SimpleCompletionPolicy);
assertEquals(25, ReflectionTestUtils.getField(completionPolicy, "chunkSize"));
Expand All @@ -126,7 +126,7 @@ void testStepParserCompletionPolicy() throws Exception {
"org/springframework/batch/core/configuration/xml/StepParserCompletionPolicyTests-context.xml");
Map<String, Step> beans = ctx.getBeansOfType(Step.class);
assertTrue(beans.containsKey("s1"), "'s1' bean not found");
Step s1 = (Step) ctx.getBean("s1");
Step s1 = ctx.getBean("s1", Step.class);
CompletionPolicy completionPolicy = getCompletionPolicy(s1);
assertTrue(completionPolicy instanceof DummyCompletionPolicy);
}
Expand Down Expand Up @@ -212,7 +212,7 @@ private void validateTransactionAttributesInherited(String stepName, Application
@SuppressWarnings("unchecked")
private List<StepExecutionListener> getListeners(String stepName, ApplicationContext ctx) throws Exception {
assertTrue(ctx.containsBean(stepName));
Step step = (Step) ctx.getBean(stepName);
Step step = ctx.getBean(stepName, Step.class);
assertTrue(step instanceof TaskletStep);
Object compositeListener = ReflectionTestUtils.getField(step, "stepExecutionListener");
Object composite = ReflectionTestUtils.getField(compositeListener, "list");
Expand All @@ -236,7 +236,7 @@ private StepExecutionListener getListener(String stepName, ApplicationContext ct

private DefaultTransactionAttribute getTransactionAttribute(ApplicationContext ctx, String stepName) {
assertTrue(ctx.containsBean(stepName));
Step step = (Step) ctx.getBean(stepName);
Step step = ctx.getBean(stepName, Step.class);
assertTrue(step instanceof TaskletStep);
Object transactionAttribute = ReflectionTestUtils.getField(step, "transactionAttribute");
return (DefaultTransactionAttribute) transactionAttribute;
Expand All @@ -252,7 +252,7 @@ void testInheritFromBean() {

private Tasklet getTasklet(String stepName, ApplicationContext ctx) {
assertTrue(ctx.containsBean(stepName));
Step step = (Step) ctx.getBean(stepName);
Step step = ctx.getBean(stepName, Step.class);
assertTrue(step instanceof TaskletStep);
Object tasklet = ReflectionTestUtils.getField(step, "tasklet");
assertTrue(tasklet instanceof Tasklet);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2022 the original author or authors.
* Copyright 2013-2025 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 @@ -46,7 +46,7 @@ class TaskletStepAllowStartIfCompleteTests {
@Test
void test() throws Exception {
// retrieve the step from the context and see that it's allow is set
AbstractStep abstractStep = (AbstractStep) context.getBean("simpleJob.step1");
AbstractStep abstractStep = context.getBean("simpleJob.step1", AbstractStep.class);
assertTrue(abstractStep.isAllowStartIfComplete());
}

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

@Override
protected StepExecutionDao getStepExecutionDao() {
return (StepExecutionDao) applicationContext.getBean("stepExecutionDao");
return applicationContext.getBean("stepExecutionDao", StepExecutionDao.class);
}

@Override
protected JobRepository getJobRepository() {
deleteFromTables("BATCH_JOB_EXECUTION_CONTEXT", "BATCH_STEP_EXECUTION_CONTEXT", "BATCH_STEP_EXECUTION",
"BATCH_JOB_EXECUTION_PARAMS", "BATCH_JOB_EXECUTION", "BATCH_JOB_INSTANCE");
return (JobRepository) applicationContext.getBean("jobRepository");
return applicationContext.getBean("jobRepository", JobRepository.class);
}

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

List<Step> stepsToExecute = new ArrayList<>();
stepsToExecute.add((Step) applicationContext.getBean(stepName));
stepsToExecute.add(applicationContext.getBean(stepName, Step.class));
job.setSteps(stepsToExecute);

JobExecution jobExecution = jobLauncher.run(job,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2023 the original author or authors.
* Copyright 2009-2025 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 @@ -70,7 +70,7 @@ void testReadToExhaustion() throws Exception {
}

protected DataSource getDataSource() {
return (DataSource) ctx.getBean("dataSource");
return ctx.getBean("dataSource", DataSource.class);
}

}
Loading