Skip to content

Commit f6b36a6

Browse files
committed
Introduce processInjection() in CommonAnnotationBeanPostProcessor
To align with the existing processInjection() method in AutowiredAnnotationBeanPostProcessor, this commit introduces an analogous method in CommonAnnotationBeanPostProcessor. Closes gh-31956
1 parent 4b6126c commit f6b36a6

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,29 @@ public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, Str
371371
return pvs;
372372
}
373373

374+
/**
375+
* <em>Native</em> processing method for direct calls with an arbitrary target
376+
* instance, resolving all of its fields and methods which are annotated with
377+
* one of the supported 'resource' annotation types.
378+
* @param bean the target instance to process
379+
* @throws BeanCreationException if resource injection failed
380+
* @since 6.1.3
381+
*/
382+
public void processInjection(Object bean) throws BeanCreationException {
383+
Class<?> clazz = bean.getClass();
384+
InjectionMetadata metadata = findResourceMetadata(clazz.getName(), clazz, null);
385+
try {
386+
metadata.inject(bean, null, null);
387+
}
388+
catch (BeanCreationException ex) {
389+
throw ex;
390+
}
391+
catch (Throwable ex) {
392+
throw new BeanCreationException(
393+
"Injection of resource dependencies failed for class [" + clazz + "]", ex);
394+
}
395+
}
396+
374397

375398
private InjectionMetadata findResourceMetadata(String beanName, Class<?> clazz, @Nullable PropertyValues pvs) {
376399
// Fall back to class name as cache key, for backwards compatibility with custom callers.

spring-context/src/test/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessorTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
*
5353
* @author Juergen Hoeller
5454
* @author Chris Beams
55+
* @author Sam Brannen
5556
*/
5657
class CommonAnnotationBeanPostProcessorTests {
5758

@@ -65,6 +66,20 @@ void setup() {
6566
bf.addBeanPostProcessor(bpp);
6667
}
6768

69+
@Test
70+
void processInjection() {
71+
ResourceInjectionBean bean = new ResourceInjectionBean();
72+
assertThat(bean.getTestBean()).isNull();
73+
assertThat(bean.getTestBean2()).isNull();
74+
75+
TestBean tb = new TestBean();
76+
bf.registerSingleton("testBean", tb);
77+
bpp.processInjection(bean);
78+
79+
assertThat(bean.getTestBean()).isSameAs(tb);
80+
assertThat(bean.getTestBean2()).isSameAs(tb);
81+
}
82+
6883
@Test
6984
void postConstructAndPreDestroy() {
7085
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(AnnotatedInitDestroyBean.class));

0 commit comments

Comments
 (0)