diff --git a/spring-context/src/main/java/org/springframework/jndi/JndiLocatorDelegate.java b/spring-context/src/main/java/org/springframework/jndi/JndiLocatorDelegate.java index 2aa2552024..53aaebb76c 100644 --- a/spring-context/src/main/java/org/springframework/jndi/JndiLocatorDelegate.java +++ b/spring-context/src/main/java/org/springframework/jndi/JndiLocatorDelegate.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2016 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. @@ -19,6 +19,8 @@ package org.springframework.jndi; import javax.naming.InitialContext; import javax.naming.NamingException; +import org.springframework.core.SpringProperties; + /** * {@link JndiLocatorSupport} subclass with public lookup methods, * for convenient use as a delegate. @@ -28,6 +30,29 @@ import javax.naming.NamingException; */ public class JndiLocatorDelegate extends JndiLocatorSupport { + /** + * System property that instructs Spring to ignore a default JNDI environment, i.e. + * to always return {@code false} from {@link #isDefaultJndiEnvironmentAvailable()}. + *

The default is "false", allowing for regular default JNDI access e.g. in + * {@link JndiPropertySource}. Switching this flag to {@code true} is an optimization + * for scenarios where nothing is ever to be found for such JNDI fallback searches + * to begin with, avoiding the repeated JNDI lookup overhead. + *

Note that this flag just affects JNDI fallback searches, not explicitly configured + * JNDI lookups such as for a {@code DataSource} or some other environment resource. + * The flag literally just affects code which attempts JNDI searches based on the + * {@code JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()} check: in particular, + * {@code StandardServletEnvironment} and {@code StandardPortletEnvironment}. + * @since 4.3 + * @see #isDefaultJndiEnvironmentAvailable() + * @see JndiPropertySource + */ + public static final String IGNORE_JNDI_PROPERTY_NAME = "spring.jndi.ignore"; + + + private static final boolean shouldIgnoreDefaultJndiEnvironment = + SpringProperties.getFlag(IGNORE_JNDI_PROPERTY_NAME); + + @Override public Object lookup(String jndiName) throws NamingException { return super.lookup(jndiName); @@ -57,6 +82,9 @@ public class JndiLocatorDelegate extends JndiLocatorSupport { * {@code false} if not */ public static boolean isDefaultJndiEnvironmentAvailable() { + if (shouldIgnoreDefaultJndiEnvironment) { + return false; + } try { new InitialContext().getEnvironment(); return true;