Skip to content

Fix BeanPostProcessorChecker warnings #4520

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

Closed
wants to merge 1 commit into from
Closed
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
@@ -0,0 +1,46 @@
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.configuration.support;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;

public class BatchBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {

private static final String JOB_REGISTRY = "jobRegistry";

private static final String BEAN_POST_PROCESSOR = "jobRegistryBeanPostProcessor";

@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
if (!registry.containsBeanDefinition(JOB_REGISTRY)) {
var beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(MapJobRegistry.class)
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE)
.getBeanDefinition();
registry.registerBeanDefinition(JOB_REGISTRY, beanDefinition);
}

if (!registry.containsBeanDefinition(BEAN_POST_PROCESSOR)) {
var beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(JobRegistryBeanPostProcessor.class)
.addPropertyReference(JOB_REGISTRY, JOB_REGISTRY)
.getBeanDefinition();
registry.registerBeanDefinition(BEAN_POST_PROCESSOR, beanDefinition);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,18 @@
* @since 5.0
*/
@Configuration(proxyBeanMethods = false)
@Import(ScopeConfiguration.class)
@Import({ ScopeConfiguration.class, BatchBeanDefinitionRegistryPostProcessor.class })
public class DefaultBatchConfiguration implements ApplicationContextAware {

@Autowired
protected ApplicationContext applicationContext;

private final JobRegistry jobRegistry = new MapJobRegistry();
@Autowired
protected JobRegistry jobRegistry;

@Autowired
@Deprecated(forRemoval = true)
private JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor;

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Expand Down Expand Up @@ -187,9 +192,17 @@ public JobExplorer jobExplorer() throws BatchConfigurationException {
}
}

@Bean
/**
* Defines a {@link JobRegistry} bean.
* @return a {@link JobRegistry} bean
* @throws BatchConfigurationException if unable to register the bean
* @since 5.0
* @deprecated in favor of bean injection, and declaring a custom bean with name
* {@code jobRegistry}
Comment on lines +200 to +201
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am trying to test this from a user's point of view (which is also what is mentioned in the docs) against this PR but it fails (while it passes on the current main). Here is a patch that slightly modifies the canonical example I used for tests:

test_pr4520.patch

*/
@Deprecated(forRemoval = true)
public JobRegistry jobRegistry() throws BatchConfigurationException {
return this.jobRegistry; // FIXME returning a new instance here does not work
return this.jobRegistry;
}

@Bean
Expand All @@ -214,18 +227,12 @@ public JobOperator jobOperator() throws BatchConfigurationException {
* @return a {@link JobRegistryBeanPostProcessor} bean
* @throws BatchConfigurationException if unable to register the bean
* @since 5.1
* @deprecated in favor of bean injection, and declaring a custom bean with name
* {@code jobRegistryBeanPostProcessor}
*/
@Bean
@Deprecated(forRemoval = true)
public JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor() throws BatchConfigurationException {
JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor = new JobRegistryBeanPostProcessor();
jobRegistryBeanPostProcessor.setJobRegistry(jobRegistry());
try {
jobRegistryBeanPostProcessor.afterPropertiesSet();
return jobRegistryBeanPostProcessor;
}
catch (Exception e) {
throw new BatchConfigurationException("Unable to configure the default job registry BeanPostProcessor", e);
}
return this.jobRegistryBeanPostProcessor;
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public JobRepository jobRepository() {
}

@Bean
public JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor(JobRegistry jobRegistry) {
public static JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor(JobRegistry jobRegistry) {
JobRegistryBeanPostProcessor postProcessor = new JobRegistryBeanPostProcessor();
postProcessor.setJobRegistry(jobRegistry);
return postProcessor;
Expand Down