1
1
/*
2
- * Copyright 2002-2020 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.
146
146
public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBeanPostProcessor
147
147
implements InstantiationAwareBeanPostProcessor , BeanFactoryAware , Serializable {
148
148
149
+ // Defensive reference to JNDI API for JDK 9+ (optional java.naming module)
150
+ private static final boolean jndiPresent = ClassUtils .isPresent (
151
+ "javax.naming.InitialContext" , CommonAnnotationBeanPostProcessor .class .getClassLoader ());
152
+
153
+ private static final Set <Class <? extends Annotation >> resourceAnnotationTypes = new LinkedHashSet <>(4 );
154
+
149
155
@ Nullable
150
156
private static final Class <? extends Annotation > webServiceRefClass ;
151
157
152
158
@ Nullable
153
159
private static final Class <? extends Annotation > ejbClass ;
154
160
155
- private static final Set <Class <? extends Annotation >> resourceAnnotationTypes = new LinkedHashSet <>(4 );
156
-
157
161
static {
158
- webServiceRefClass = loadAnnotationType ("javax.xml.ws.WebServiceRef" );
159
- ejbClass = loadAnnotationType ("javax.ejb.EJB" );
160
-
161
162
resourceAnnotationTypes .add (Resource .class );
163
+
164
+ webServiceRefClass = loadAnnotationType ("javax.xml.ws.WebServiceRef" );
162
165
if (webServiceRefClass != null ) {
163
166
resourceAnnotationTypes .add (webServiceRefClass );
164
167
}
168
+
169
+ ejbClass = loadAnnotationType ("javax.ejb.EJB" );
165
170
if (ejbClass != null ) {
166
171
resourceAnnotationTypes .add (ejbClass );
167
172
}
@@ -174,7 +179,8 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean
174
179
175
180
private boolean alwaysUseJndiLookup = false ;
176
181
177
- private transient BeanFactory jndiFactory = new SimpleJndiBeanFactory ();
182
+ @ Nullable
183
+ private transient BeanFactory jndiFactory ;
178
184
179
185
@ Nullable
180
186
private transient BeanFactory resourceFactory ;
@@ -199,6 +205,11 @@ public CommonAnnotationBeanPostProcessor() {
199
205
setInitAnnotationType (PostConstruct .class );
200
206
setDestroyAnnotationType (PreDestroy .class );
201
207
ignoreResourceType ("javax.xml.ws.WebServiceContext" );
208
+
209
+ // java.naming module present on JDK 9+?
210
+ if (jndiPresent ) {
211
+ this .jndiFactory = new SimpleJndiBeanFactory ();
212
+ }
202
213
}
203
214
204
215
@@ -464,6 +475,7 @@ public Object getTarget() {
464
475
public void releaseTarget (Object target ) {
465
476
}
466
477
};
478
+
467
479
ProxyFactory pf = new ProxyFactory ();
468
480
pf .setTargetSource (ts );
469
481
if (element .lookupType .isInterface ()) {
@@ -484,12 +496,23 @@ public void releaseTarget(Object target) {
484
496
protected Object getResource (LookupElement element , @ Nullable String requestingBeanName )
485
497
throws NoSuchBeanDefinitionException {
486
498
499
+ // JNDI lookup to perform?
500
+ String jndiName = null ;
487
501
if (StringUtils .hasLength (element .mappedName )) {
488
- return this .jndiFactory .getBean (element .mappedName , element .lookupType );
502
+ jndiName = element .mappedName ;
503
+ }
504
+ else if (this .alwaysUseJndiLookup ) {
505
+ jndiName = element .name ;
489
506
}
490
- if (this .alwaysUseJndiLookup ) {
491
- return this .jndiFactory .getBean (element .name , element .lookupType );
507
+ if (jndiName != null ) {
508
+ if (this .jndiFactory == null ) {
509
+ throw new NoSuchBeanDefinitionException (element .lookupType ,
510
+ "No JNDI factory configured - specify the 'jndiFactory' property" );
511
+ }
512
+ return this .jndiFactory .getBean (jndiName , element .lookupType );
492
513
}
514
+
515
+ // Regular resource autowiring
493
516
if (this .resourceFactory == null ) {
494
517
throw new NoSuchBeanDefinitionException (element .lookupType ,
495
518
"No resource factory configured - specify the 'resourceFactory' property" );
0 commit comments