Skip to content

Commit 4d7fa9a

Browse files
committed
Merge branch '5.3.x'
# Conflicts: # spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java
2 parents 1e6c0d2 + 0404456 commit 4d7fa9a

File tree

6 files changed

+47
-14
lines changed

6 files changed

+47
-14
lines changed

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@
130130
public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBeanPostProcessor
131131
implements InstantiationAwareBeanPostProcessor, BeanFactoryAware, Serializable {
132132

133+
// Defensive reference to JNDI API for JDK 9+ (optional java.naming module)
134+
private static final boolean jndiPresent = ClassUtils.isPresent(
135+
"javax.naming.InitialContext", CommonAnnotationBeanPostProcessor.class.getClassLoader());
136+
133137
private static final Set<Class<? extends Annotation>> resourceAnnotationTypes = new LinkedHashSet<>(4);
134138

135139
@Nullable
@@ -151,7 +155,8 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean
151155

152156
private boolean alwaysUseJndiLookup = false;
153157

154-
private transient BeanFactory jndiFactory = new SimpleJndiBeanFactory();
158+
@Nullable
159+
private transient BeanFactory jndiFactory;
155160

156161
@Nullable
157162
private transient BeanFactory resourceFactory;
@@ -175,6 +180,11 @@ public CommonAnnotationBeanPostProcessor() {
175180
setOrder(Ordered.LOWEST_PRECEDENCE - 3);
176181
setInitAnnotationType(PostConstruct.class);
177182
setDestroyAnnotationType(PreDestroy.class);
183+
184+
// java.naming module present on JDK 9+?
185+
if (jndiPresent) {
186+
this.jndiFactory = new SimpleJndiBeanFactory();
187+
}
178188
}
179189

180190

@@ -421,6 +431,7 @@ public Object getTarget() {
421431
public void releaseTarget(Object target) {
422432
}
423433
};
434+
424435
ProxyFactory pf = new ProxyFactory();
425436
pf.setTargetSource(ts);
426437
if (element.lookupType.isInterface()) {
@@ -441,12 +452,23 @@ public void releaseTarget(Object target) {
441452
protected Object getResource(LookupElement element, @Nullable String requestingBeanName)
442453
throws NoSuchBeanDefinitionException {
443454

455+
// JNDI lookup to perform?
456+
String jndiName = null;
444457
if (StringUtils.hasLength(element.mappedName)) {
445-
return this.jndiFactory.getBean(element.mappedName, element.lookupType);
458+
jndiName = element.mappedName;
459+
}
460+
else if (this.alwaysUseJndiLookup) {
461+
jndiName = element.name;
446462
}
447-
if (this.alwaysUseJndiLookup) {
448-
return this.jndiFactory.getBean(element.name, element.lookupType);
463+
if (jndiName != null) {
464+
if (this.jndiFactory == null) {
465+
throw new NoSuchBeanDefinitionException(element.lookupType,
466+
"No JNDI factory configured - specify the 'jndiFactory' property");
467+
}
468+
return this.jndiFactory.getBean(jndiName, element.lookupType);
449469
}
470+
471+
// Regular resource autowiring
450472
if (this.resourceFactory == null) {
451473
throw new NoSuchBeanDefinitionException(element.lookupType,
452474
"No resource factory configured - specify the 'resourceFactory' property");

spring-core/src/main/java/org/springframework/core/env/AbstractEnvironment.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
*
4848
* @author Chris Beams
4949
* @author Juergen Hoeller
50+
* @author Phillip Webb
5051
* @since 3.1
5152
* @see ConfigurableEnvironment
5253
* @see StandardEnvironment
@@ -127,6 +128,7 @@ public AbstractEnvironment() {
127128
* {@link #customizePropertySources(MutablePropertySources)} during
128129
* construction to allow subclasses to contribute or manipulate
129130
* {@link PropertySource} instances as appropriate.
131+
* @param propertySources property sources to use
130132
* @since 5.3.4
131133
* @see #customizePropertySources(MutablePropertySources)
132134
*/
@@ -136,6 +138,7 @@ protected AbstractEnvironment(MutablePropertySources propertySources) {
136138
customizePropertySources(propertySources);
137139
}
138140

141+
139142
/**
140143
* Factory method used to create the {@link ConfigurablePropertyResolver}
141144
* instance used by the Environment.

spring-core/src/main/java/org/springframework/core/env/StandardEnvironment.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* variable names.
4747
*
4848
* @author Chris Beams
49+
* @author Phillip Webb
4950
* @since 3.1
5051
* @see ConfigurableEnvironment
5152
* @see SystemEnvironmentPropertySource
@@ -61,13 +62,15 @@ public class StandardEnvironment extends AbstractEnvironment {
6162

6263

6364
/**
64-
* Create a new {@code StandardEnvironment} instance.
65+
* Create a new {@code StandardEnvironment} instance with a default
66+
* {@link MutablePropertySources} instance.
6567
*/
6668
public StandardEnvironment() {
6769
}
6870

6971
/**
70-
* Create a new {@code StandardEnvironment} instance with a specific {@link MutablePropertySources} instance.
72+
* Create a new {@code StandardEnvironment} instance with a specific
73+
* {@link MutablePropertySources} instance.
7174
* @param propertySources property sources to use
7275
* @since 5.3.4
7376
*/

spring-jcl/src/main/java/org/apache/commons/logging/LogAdapter.java

Lines changed: 4 additions & 4 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.
@@ -278,7 +278,7 @@ private static class Slf4jLog<T extends Logger> implements Log, Serializable {
278278

279279
protected final String name;
280280

281-
protected transient T logger;
281+
protected final transient T logger;
282282

283283
public Slf4jLog(T logger) {
284284
this.name = logger.getName();
@@ -500,9 +500,9 @@ protected Object readResolve() {
500500
@SuppressWarnings("serial")
501501
private static class JavaUtilLog implements Log, Serializable {
502502

503-
private String name;
503+
private final String name;
504504

505-
private transient java.util.logging.Logger logger;
505+
private final transient java.util.logging.Logger logger;
506506

507507
public JavaUtilLog(String name) {
508508
this.name = name;

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);

src/checkstyle/checkstyle.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
<property name="headerCopyrightPattern" value="20\d\d-20\d\d"/>
1414
<property name="packageInfoHeaderType" value="none"/>
1515
</module>
16-
<module name="com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck">
17-
<property name="lineSeparator" value="lf"/>
18-
</module>
16+
<module name="com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck"/>
1917

2018
<!-- TreeWalker Checks -->
2119
<module name="com.puppycrawl.tools.checkstyle.TreeWalker">

0 commit comments

Comments
 (0)