Skip to content

Add BeanPostProcessors in bulk #24756

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
Expand Up @@ -18,6 +18,7 @@

import java.beans.PropertyEditor;
import java.security.AccessControlContext;
import java.util.List;

import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
Expand Down Expand Up @@ -244,6 +245,20 @@ public interface ConfigurableBeanFactory extends HierarchicalBeanFactory, Single
*/
void addBeanPostProcessor(BeanPostProcessor beanPostProcessor);

/**
* Add new BeanPostProcessors that will get applied to beans created
* by this factory. To be invoked during factory configuration.
* <p>Note: Post-processors submitted here will be applied in the order of
* registration; any ordering semantics expressed through implementing the
* {@link org.springframework.core.Ordered} interface will be ignored. Note
* that autodetected post-processors (e.g. as beans in an ApplicationContext)
* will always be applied after programmatically registered ones.
* @param beanPostProcessors the post-processors to register
*/
default void addBeanPostProcessors(List<BeanPostProcessor> beanPostProcessors) {
beanPostProcessors.forEach(this::addBeanPostProcessor);
}

/**
* Return the current number of registered BeanPostProcessors, if any.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,23 @@ public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
this.beanPostProcessors.add(beanPostProcessor);
}

@Override
public void addBeanPostProcessors(List<BeanPostProcessor> beanPostProcessors) {
this.beanPostProcessors.removeAll(beanPostProcessors);

for (BeanPostProcessor beanPostProcessor : beanPostProcessors) {
Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null");
// Track whether it is instantiation/destruction aware
if (beanPostProcessor instanceof InstantiationAwareBeanPostProcessor) {
this.hasInstantiationAwareBeanPostProcessors = true;
}
if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) {
this.hasDestructionAwareBeanPostProcessors = true;
}
}
this.beanPostProcessors.addAll(beanPostProcessors);
}

@Override
public int getBeanPostProcessorCount() {
return this.beanPostProcessors.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {

// First, register the BeanPostProcessors that implement PriorityOrdered.
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
beanFactory.addBeanPostProcessors(priorityOrderedPostProcessors);

// Next, register the BeanPostProcessors that implement Ordered.
List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
Expand All @@ -232,7 +232,7 @@ else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
}
}
sortPostProcessors(orderedPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, orderedPostProcessors);
beanFactory.addBeanPostProcessors(orderedPostProcessors);

// Now, register all regular BeanPostProcessors.
List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
Expand All @@ -243,11 +243,11 @@ else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
internalPostProcessors.add(pp);
}
}
registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
beanFactory.addBeanPostProcessors(nonOrderedPostProcessors);

// Finally, re-register all internal BeanPostProcessors.
sortPostProcessors(internalPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, internalPostProcessors);
beanFactory.addBeanPostProcessors(internalPostProcessors);

// Re-register post-processor for detecting inner beans as ApplicationListeners,
// moving it to the end of the processor chain (for picking up proxies etc).
Expand Down Expand Up @@ -287,18 +287,6 @@ private static void invokeBeanFactoryPostProcessors(
}
}

/**
* Register the given BeanPostProcessor beans.
*/
private static void registerBeanPostProcessors(
ConfigurableListableBeanFactory beanFactory, List<BeanPostProcessor> postProcessors) {

for (BeanPostProcessor postProcessor : postProcessors) {
beanFactory.addBeanPostProcessor(postProcessor);
}
}


/**
* BeanPostProcessor that logs an info message when a bean is created during
* BeanPostProcessor instantiation, i.e. when a bean is not eligible for
Expand Down