Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 26ee9c6

Browse files
committed
Document [Priority]Ordered support for Bean[Factory]PostProcessor
Prior to this commit, it was not clear from the Javadoc for BeanPostProcess and BeanFactoryPostProcessor that such components can be ordered by implementing Ordered or PriorityOrdered. This commit improves the documentation for BPP and BFPP to make this support explicit. Closes spring-projectsgh-23636
1 parent 77b896c commit 26ee9c6

File tree

2 files changed

+54
-22
lines changed

2 files changed

+54
-22
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/config/BeanFactoryPostProcessor.java

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -19,25 +19,42 @@
1919
import org.springframework.beans.BeansException;
2020

2121
/**
22-
* Allows for custom modification of an application context's bean definitions,
23-
* adapting the bean property values of the context's underlying bean factory.
24-
*
25-
* <p>Application contexts can auto-detect BeanFactoryPostProcessor beans in
26-
* their bean definitions and apply them before any other beans get created.
22+
* Factory hook that allows for custom modification of an application context's
23+
* bean definitions, adapting the bean property values of the context's underlying
24+
* bean factory.
2725
*
2826
* <p>Useful for custom config files targeted at system administrators that
29-
* override bean properties configured in the application context.
30-
*
31-
* <p>See PropertyResourceConfigurer and its concrete implementations
32-
* for out-of-the-box solutions that address such configuration needs.
27+
* override bean properties configured in the application context. See
28+
* {@link PropertyResourceConfigurer} and its concrete implementations for
29+
* out-of-the-box solutions that address such configuration needs.
3330
*
34-
* <p>A BeanFactoryPostProcessor may interact with and modify bean
31+
* <p>A {@code BeanFactoryPostProcessor} may interact with and modify bean
3532
* definitions, but never bean instances. Doing so may cause premature bean
3633
* instantiation, violating the container and causing unintended side-effects.
3734
* If bean instance interaction is required, consider implementing
3835
* {@link BeanPostProcessor} instead.
3936
*
37+
* <h3>Registration</h3>
38+
* <p>An {@code ApplicationContext} auto-detects {@code BeanFactoryPostProcessor}
39+
* beans in its bean definitions and applies them before any other beans get created.
40+
* A {@code BeanFactoryPostProcessor} may also be registered programmatically
41+
* with a {@code ConfigurableApplicationContext}.
42+
*
43+
* <h3>Ordering</h3>
44+
* <p>{@code BeanFactoryPostProcessor} beans that are autodetected in an
45+
* {@code ApplicationContext} will be ordered according to
46+
* {@link org.springframework.core.PriorityOrdered} and
47+
* {@link org.springframework.core.Ordered} semantics. In contrast,
48+
* {@code BeanFactoryPostProcessor} beans that are registered programmatically
49+
* with a {@code ConfigurableApplicationContext} will be applied in the order of
50+
* registration; any ordering semantics expressed through implementing the
51+
* {@code PriorityOrdered} or {@code Ordered} interface will be ignored for
52+
* programmatically registered post-processors. Furthermore, the
53+
* {@link org.springframework.core.annotation.Order @Order} annotation is not
54+
* taken into account for {@code BeanFactoryPostProcessor} beans.
55+
*
4056
* @author Juergen Hoeller
57+
* @author Sam Brannen
4158
* @since 06.07.2003
4259
* @see BeanPostProcessor
4360
* @see PropertyResourceConfigurer

spring-beans/src/main/java/org/springframework/beans/factory/config/BeanPostProcessor.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -20,20 +20,35 @@
2020
import org.springframework.lang.Nullable;
2121

2222
/**
23-
* Factory hook that allows for custom modification of new bean instances,
24-
* e.g. checking for marker interfaces or wrapping them with proxies.
25-
*
26-
* <p>ApplicationContexts can autodetect BeanPostProcessor beans in their
27-
* bean definitions and apply them to any beans subsequently created.
28-
* Plain bean factories allow for programmatic registration of post-processors,
29-
* applying to all beans created through this factory.
23+
* Factory hook that allows for custom modification of new bean instances &mdash;
24+
* for example, checking for marker interfaces or wrapping beans with proxies.
3025
*
3126
* <p>Typically, post-processors that populate beans via marker interfaces
3227
* or the like will implement {@link #postProcessBeforeInitialization},
3328
* while post-processors that wrap beans with proxies will normally
3429
* implement {@link #postProcessAfterInitialization}.
3530
*
31+
* <h3>Registration</h3>
32+
* <p>An {@code ApplicationContext} can autodetect {@code BeanPostProcessor} beans
33+
* in its bean definitions and apply those post-processors to any beans subsequently
34+
* created. A plain {@code BeanFactory} allows for programmatic registration of
35+
* post-processors, applying them to all beans created through the bean factory.
36+
*
37+
* <h3>Ordering</h3>
38+
* <p>{@code BeanPostProcessor} beans that are autodetected in an
39+
* {@code ApplicationContext} will be ordered according to
40+
* {@link org.springframework.core.PriorityOrdered} and
41+
* {@link org.springframework.core.Ordered} semantics. In contrast,
42+
* {@code BeanPostProcessor} beans that are registered programmatically with a
43+
* {@code BeanFactory} will be applied in the order of registration; any ordering
44+
* semantics expressed through implementing the
45+
* {@code PriorityOrdered} or {@code Ordered} interface will be ignored for
46+
* programmatically registered post-processors. Furthermore, the
47+
* {@link org.springframework.core.annotation.Order @Order} annotation is not
48+
* taken into account for {@code BeanPostProcessor} beans.
49+
*
3650
* @author Juergen Hoeller
51+
* @author Sam Brannen
3752
* @since 10.10.2003
3853
* @see InstantiationAwareBeanPostProcessor
3954
* @see DestructionAwareBeanPostProcessor
@@ -43,7 +58,7 @@
4358
public interface BeanPostProcessor {
4459

4560
/**
46-
* Apply this BeanPostProcessor to the given new bean instance <i>before</i> any bean
61+
* Apply this {@code BeanPostProcessor} to the given new bean instance <i>before</i> any bean
4762
* initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
4863
* or a custom init-method). The bean will already be populated with property values.
4964
* The returned bean instance may be a wrapper around the original.
@@ -61,7 +76,7 @@ default Object postProcessBeforeInitialization(Object bean, String beanName) thr
6176
}
6277

6378
/**
64-
* Apply this BeanPostProcessor to the given new bean instance <i>after</i> any bean
79+
* Apply this {@code BeanPostProcessor} to the given new bean instance <i>after</i> any bean
6580
* initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
6681
* or a custom init-method). The bean will already be populated with property values.
6782
* The returned bean instance may be a wrapper around the original.
@@ -71,7 +86,7 @@ default Object postProcessBeforeInitialization(Object bean, String beanName) thr
7186
* objects or both through corresponding {@code bean instanceof FactoryBean} checks.
7287
* <p>This callback will also be invoked after a short-circuiting triggered by a
7388
* {@link InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation} method,
74-
* in contrast to all other BeanPostProcessor callbacks.
89+
* in contrast to all other {@code BeanPostProcessor} callbacks.
7590
* <p>The default implementation returns the given {@code bean} as-is.
7691
* @param bean the new bean instance
7792
* @param beanName the name of the bean

0 commit comments

Comments
 (0)