1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
23
23
24
24
import org .springframework .beans .factory .annotation .AnnotatedBeanDefinition ;
25
25
import org .springframework .beans .factory .annotation .AutowiredAnnotationBeanPostProcessor ;
26
+ import org .springframework .beans .factory .annotation .InitDestroyAnnotationBeanPostProcessor ;
26
27
import org .springframework .beans .factory .config .BeanDefinition ;
27
28
import org .springframework .beans .factory .config .BeanDefinitionHolder ;
28
29
import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
@@ -91,11 +92,17 @@ public abstract class AnnotationConfigUtils {
91
92
"org.springframework.context.annotation.internalRequiredAnnotationProcessor" ;
92
93
93
94
/**
94
- * The bean name of the internally managed JSR-250 annotation processor.
95
+ * The bean name of the internally managed common annotation processor.
95
96
*/
96
97
public static final String COMMON_ANNOTATION_PROCESSOR_BEAN_NAME =
97
98
"org.springframework.context.annotation.internalCommonAnnotationProcessor" ;
98
99
100
+ /**
101
+ * The bean name of the internally managed JSR-250 annotation processor.
102
+ */
103
+ private static final String JSR250_ANNOTATION_PROCESSOR_BEAN_NAME =
104
+ "org.springframework.context.annotation.internalJsr250AnnotationProcessor" ;
105
+
99
106
/**
100
107
* The bean name of the internally managed JPA annotation processor.
101
108
*/
@@ -117,16 +124,18 @@ public abstract class AnnotationConfigUtils {
117
124
public static final String EVENT_LISTENER_FACTORY_BEAN_NAME =
118
125
"org.springframework.context.event.internalEventListenerFactory" ;
119
126
120
- private static final boolean jsr250Present ;
121
127
122
- private static final boolean jpaPresent ;
128
+ private static final ClassLoader classLoader = AnnotationConfigUtils . class . getClassLoader () ;
123
129
124
- static {
125
- ClassLoader classLoader = AnnotationConfigUtils .class .getClassLoader ();
126
- jsr250Present = ClassUtils .isPresent ("jakarta.annotation.Resource" , classLoader );
127
- jpaPresent = ClassUtils .isPresent ("jakarta.persistence.EntityManagerFactory" , classLoader ) &&
128
- ClassUtils .isPresent (PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME , classLoader );
129
- }
130
+ private static final boolean jakartaAnnotationsPresent =
131
+ ClassUtils .isPresent ("jakarta.annotation.PostConstruct" , classLoader );
132
+
133
+ private static final boolean jsr250Present =
134
+ ClassUtils .isPresent ("javax.annotation.PostConstruct" , classLoader );
135
+
136
+ private static final boolean jpaPresent =
137
+ ClassUtils .isPresent ("jakarta.persistence.EntityManagerFactory" , classLoader ) &&
138
+ ClassUtils .isPresent (PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME , classLoader );
130
139
131
140
132
141
/**
@@ -172,13 +181,28 @@ public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
172
181
beanDefs .add (registerPostProcessor (registry , def , AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME ));
173
182
}
174
183
175
- // Check for JSR-250 support, and if present add the CommonAnnotationBeanPostProcessor.
176
- if (jsr250Present && !registry .containsBeanDefinition (COMMON_ANNOTATION_PROCESSOR_BEAN_NAME )) {
184
+ // Check for Jakarta Annotations support, and if present add the CommonAnnotationBeanPostProcessor.
185
+ if (jakartaAnnotationsPresent && !registry .containsBeanDefinition (COMMON_ANNOTATION_PROCESSOR_BEAN_NAME )) {
177
186
RootBeanDefinition def = new RootBeanDefinition (CommonAnnotationBeanPostProcessor .class );
178
187
def .setSource (source );
179
188
beanDefs .add (registerPostProcessor (registry , def , COMMON_ANNOTATION_PROCESSOR_BEAN_NAME ));
180
189
}
181
190
191
+ // Check for JSR-250 support, and if present add an InitDestroyAnnotationBeanPostProcessor
192
+ // for the javax variant of PostConstruct/PreDestroy.
193
+ if (jsr250Present && !registry .containsBeanDefinition (JSR250_ANNOTATION_PROCESSOR_BEAN_NAME )) {
194
+ try {
195
+ RootBeanDefinition def = new RootBeanDefinition (InitDestroyAnnotationBeanPostProcessor .class );
196
+ def .getPropertyValues ().add ("initAnnotationType" , classLoader .loadClass ("javax.annotation.PostConstruct" ));
197
+ def .getPropertyValues ().add ("destroyAnnotationType" , classLoader .loadClass ("javax.annotation.PreDestroy" ));
198
+ def .setSource (source );
199
+ beanDefs .add (registerPostProcessor (registry , def , JSR250_ANNOTATION_PROCESSOR_BEAN_NAME ));
200
+ }
201
+ catch (ClassNotFoundException ex ) {
202
+ // Failed to load javax variants of the annotation types -> ignore.
203
+ }
204
+ }
205
+
182
206
// Check for JPA support, and if present add the PersistenceAnnotationBeanPostProcessor.
183
207
if (jpaPresent && !registry .containsBeanDefinition (PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME )) {
184
208
RootBeanDefinition def = new RootBeanDefinition ();
0 commit comments