Skip to content

Commit 86b010a

Browse files
committed
Defensive reference to JNDI API for JDK 9+ (optional java.naming module)
Closes gh-27483
1 parent 2feedb9 commit 86b010a

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

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

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -146,22 +146,27 @@
146146
public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBeanPostProcessor
147147
implements InstantiationAwareBeanPostProcessor, BeanFactoryAware, Serializable {
148148

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+
149155
@Nullable
150156
private static final Class<? extends Annotation> webServiceRefClass;
151157

152158
@Nullable
153159
private static final Class<? extends Annotation> ejbClass;
154160

155-
private static final Set<Class<? extends Annotation>> resourceAnnotationTypes = new LinkedHashSet<>(4);
156-
157161
static {
158-
webServiceRefClass = loadAnnotationType("javax.xml.ws.WebServiceRef");
159-
ejbClass = loadAnnotationType("javax.ejb.EJB");
160-
161162
resourceAnnotationTypes.add(Resource.class);
163+
164+
webServiceRefClass = loadAnnotationType("javax.xml.ws.WebServiceRef");
162165
if (webServiceRefClass != null) {
163166
resourceAnnotationTypes.add(webServiceRefClass);
164167
}
168+
169+
ejbClass = loadAnnotationType("javax.ejb.EJB");
165170
if (ejbClass != null) {
166171
resourceAnnotationTypes.add(ejbClass);
167172
}
@@ -174,7 +179,8 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean
174179

175180
private boolean alwaysUseJndiLookup = false;
176181

177-
private transient BeanFactory jndiFactory = new SimpleJndiBeanFactory();
182+
@Nullable
183+
private transient BeanFactory jndiFactory;
178184

179185
@Nullable
180186
private transient BeanFactory resourceFactory;
@@ -199,6 +205,11 @@ public CommonAnnotationBeanPostProcessor() {
199205
setInitAnnotationType(PostConstruct.class);
200206
setDestroyAnnotationType(PreDestroy.class);
201207
ignoreResourceType("javax.xml.ws.WebServiceContext");
208+
209+
// java.naming module present on JDK 9+?
210+
if (jndiPresent) {
211+
this.jndiFactory = new SimpleJndiBeanFactory();
212+
}
202213
}
203214

204215

@@ -464,6 +475,7 @@ public Object getTarget() {
464475
public void releaseTarget(Object target) {
465476
}
466477
};
478+
467479
ProxyFactory pf = new ProxyFactory();
468480
pf.setTargetSource(ts);
469481
if (element.lookupType.isInterface()) {
@@ -484,12 +496,23 @@ public void releaseTarget(Object target) {
484496
protected Object getResource(LookupElement element, @Nullable String requestingBeanName)
485497
throws NoSuchBeanDefinitionException {
486498

499+
// JNDI lookup to perform?
500+
String jndiName = null;
487501
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;
489506
}
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);
492513
}
514+
515+
// Regular resource autowiring
493516
if (this.resourceFactory == null) {
494517
throw new NoSuchBeanDefinitionException(element.lookupType,
495518
"No resource factory configured - specify the 'resourceFactory' property");

spring-web/src/main/java/org/springframework/web/context/support/StandardServletEnvironment.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.jndi.JndiLocatorDelegate;
2828
import org.springframework.jndi.JndiPropertySource;
2929
import org.springframework.lang.Nullable;
30+
import org.springframework.util.ClassUtils;
3031
import org.springframework.web.context.ConfigurableWebEnvironment;
3132

3233
/**
@@ -39,6 +40,7 @@
3940
* documentation for details.
4041
*
4142
* @author Chris Beams
43+
* @author Juergen Hoeller
4244
* @since 3.1
4345
* @see StandardEnvironment
4446
*/
@@ -54,6 +56,11 @@ public class StandardServletEnvironment extends StandardEnvironment implements C
5456
public static final String JNDI_PROPERTY_SOURCE_NAME = "jndiProperties";
5557

5658

59+
// Defensive reference to JNDI API for JDK 9+ (optional java.naming module)
60+
private static final boolean jndiPresent = ClassUtils.isPresent(
61+
"javax.naming.InitialContext", StandardServletEnvironment.class.getClassLoader());
62+
63+
5764
/**
5865
* Create a new {@code StandardServletEnvironment} instance.
5966
*/
@@ -100,7 +107,7 @@ protected StandardServletEnvironment(MutablePropertySources propertySources) {
100107
protected void customizePropertySources(MutablePropertySources propertySources) {
101108
propertySources.addLast(new StubPropertySource(SERVLET_CONFIG_PROPERTY_SOURCE_NAME));
102109
propertySources.addLast(new StubPropertySource(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME));
103-
if (JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) {
110+
if (jndiPresent && JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) {
104111
propertySources.addLast(new JndiPropertySource(JNDI_PROPERTY_SOURCE_NAME));
105112
}
106113
super.customizePropertySources(propertySources);

0 commit comments

Comments
 (0)