diff --git a/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java b/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java index 3bd70bc53cb..c3ded8c56fa 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java @@ -130,6 +130,10 @@ import org.springframework.util.StringValueResolver; public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBeanPostProcessor implements InstantiationAwareBeanPostProcessor, BeanFactoryAware, Serializable { + // Defensive reference to JNDI API for JDK 9+ (optional java.naming module) + private static final boolean jndiPresent = ClassUtils.isPresent( + "javax.naming.InitialContext", CommonAnnotationBeanPostProcessor.class.getClassLoader()); + private static final Set> resourceAnnotationTypes = new LinkedHashSet<>(4); @Nullable @@ -151,7 +155,8 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean private boolean alwaysUseJndiLookup = false; - private transient BeanFactory jndiFactory = new SimpleJndiBeanFactory(); + @Nullable + private transient BeanFactory jndiFactory; @Nullable private transient BeanFactory resourceFactory; @@ -175,6 +180,11 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean setOrder(Ordered.LOWEST_PRECEDENCE - 3); setInitAnnotationType(PostConstruct.class); setDestroyAnnotationType(PreDestroy.class); + + // java.naming module present on JDK 9+? + if (jndiPresent) { + this.jndiFactory = new SimpleJndiBeanFactory(); + } } @@ -421,6 +431,7 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean public void releaseTarget(Object target) { } }; + ProxyFactory pf = new ProxyFactory(); pf.setTargetSource(ts); if (element.lookupType.isInterface()) { @@ -441,12 +452,23 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean protected Object getResource(LookupElement element, @Nullable String requestingBeanName) throws NoSuchBeanDefinitionException { + // JNDI lookup to perform? + String jndiName = null; if (StringUtils.hasLength(element.mappedName)) { - return this.jndiFactory.getBean(element.mappedName, element.lookupType); + jndiName = element.mappedName; + } + else if (this.alwaysUseJndiLookup) { + jndiName = element.name; } - if (this.alwaysUseJndiLookup) { - return this.jndiFactory.getBean(element.name, element.lookupType); + if (jndiName != null) { + if (this.jndiFactory == null) { + throw new NoSuchBeanDefinitionException(element.lookupType, + "No JNDI factory configured - specify the 'jndiFactory' property"); + } + return this.jndiFactory.getBean(jndiName, element.lookupType); } + + // Regular resource autowiring if (this.resourceFactory == null) { throw new NoSuchBeanDefinitionException(element.lookupType, "No resource factory configured - specify the 'resourceFactory' property"); diff --git a/spring-core/src/main/java/org/springframework/core/env/AbstractEnvironment.java b/spring-core/src/main/java/org/springframework/core/env/AbstractEnvironment.java index b4dd436d645..c12428dda66 100644 --- a/spring-core/src/main/java/org/springframework/core/env/AbstractEnvironment.java +++ b/spring-core/src/main/java/org/springframework/core/env/AbstractEnvironment.java @@ -47,6 +47,7 @@ import org.springframework.util.StringUtils; * * @author Chris Beams * @author Juergen Hoeller + * @author Phillip Webb * @since 3.1 * @see ConfigurableEnvironment * @see StandardEnvironment @@ -127,6 +128,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment { * {@link #customizePropertySources(MutablePropertySources)} during * construction to allow subclasses to contribute or manipulate * {@link PropertySource} instances as appropriate. + * @param propertySources property sources to use * @since 5.3.4 * @see #customizePropertySources(MutablePropertySources) */ @@ -136,6 +138,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment { customizePropertySources(propertySources); } + /** * Factory method used to create the {@link ConfigurablePropertyResolver} * instance used by the Environment. diff --git a/spring-core/src/main/java/org/springframework/core/env/StandardEnvironment.java b/spring-core/src/main/java/org/springframework/core/env/StandardEnvironment.java index b228de56bb2..9d2a2eb6b88 100644 --- a/spring-core/src/main/java/org/springframework/core/env/StandardEnvironment.java +++ b/spring-core/src/main/java/org/springframework/core/env/StandardEnvironment.java @@ -46,6 +46,7 @@ package org.springframework.core.env; * variable names. * * @author Chris Beams + * @author Phillip Webb * @since 3.1 * @see ConfigurableEnvironment * @see SystemEnvironmentPropertySource @@ -61,13 +62,15 @@ public class StandardEnvironment extends AbstractEnvironment { /** - * Create a new {@code StandardEnvironment} instance. + * Create a new {@code StandardEnvironment} instance with a default + * {@link MutablePropertySources} instance. */ public StandardEnvironment() { } /** - * Create a new {@code StandardEnvironment} instance with a specific {@link MutablePropertySources} instance. + * Create a new {@code StandardEnvironment} instance with a specific + * {@link MutablePropertySources} instance. * @param propertySources property sources to use * @since 5.3.4 */ diff --git a/spring-jcl/src/main/java/org/apache/commons/logging/LogAdapter.java b/spring-jcl/src/main/java/org/apache/commons/logging/LogAdapter.java index c308340714d..5639cc0f6d0 100644 --- a/spring-jcl/src/main/java/org/apache/commons/logging/LogAdapter.java +++ b/spring-jcl/src/main/java/org/apache/commons/logging/LogAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -278,7 +278,7 @@ final class LogAdapter { protected final String name; - protected transient T logger; + protected final transient T logger; public Slf4jLog(T logger) { this.name = logger.getName(); @@ -500,9 +500,9 @@ final class LogAdapter { @SuppressWarnings("serial") private static class JavaUtilLog implements Log, Serializable { - private String name; + private final String name; - private transient java.util.logging.Logger logger; + private final transient java.util.logging.Logger logger; public JavaUtilLog(String name) { this.name = name; diff --git a/spring-web/src/main/java/org/springframework/web/context/support/StandardServletEnvironment.java b/spring-web/src/main/java/org/springframework/web/context/support/StandardServletEnvironment.java index 28cfb4e8c38..d30d96035a0 100644 --- a/spring-web/src/main/java/org/springframework/web/context/support/StandardServletEnvironment.java +++ b/spring-web/src/main/java/org/springframework/web/context/support/StandardServletEnvironment.java @@ -27,6 +27,7 @@ import org.springframework.core.env.StandardEnvironment; import org.springframework.jndi.JndiLocatorDelegate; import org.springframework.jndi.JndiPropertySource; import org.springframework.lang.Nullable; +import org.springframework.util.ClassUtils; import org.springframework.web.context.ConfigurableWebEnvironment; /** @@ -39,6 +40,7 @@ import org.springframework.web.context.ConfigurableWebEnvironment; * documentation for details. * * @author Chris Beams + * @author Juergen Hoeller * @since 3.1 * @see StandardEnvironment */ @@ -54,6 +56,11 @@ public class StandardServletEnvironment extends StandardEnvironment implements C public static final String JNDI_PROPERTY_SOURCE_NAME = "jndiProperties"; + // Defensive reference to JNDI API for JDK 9+ (optional java.naming module) + private static final boolean jndiPresent = ClassUtils.isPresent( + "javax.naming.InitialContext", StandardServletEnvironment.class.getClassLoader()); + + /** * Create a new {@code StandardServletEnvironment} instance. */ @@ -100,7 +107,7 @@ public class StandardServletEnvironment extends StandardEnvironment implements C protected void customizePropertySources(MutablePropertySources propertySources) { propertySources.addLast(new StubPropertySource(SERVLET_CONFIG_PROPERTY_SOURCE_NAME)); propertySources.addLast(new StubPropertySource(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)); - if (JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) { + if (jndiPresent && JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) { propertySources.addLast(new JndiPropertySource(JNDI_PROPERTY_SOURCE_NAME)); } super.customizePropertySources(propertySources); diff --git a/src/checkstyle/checkstyle.xml b/src/checkstyle/checkstyle.xml index 6358bc47e75..2d8a043623c 100644 --- a/src/checkstyle/checkstyle.xml +++ b/src/checkstyle/checkstyle.xml @@ -13,9 +13,7 @@ - - - +