From a4925dabf71134bfb19c266c655940a2ce9d5ad0 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 26 Aug 2014 11:33:09 -0700 Subject: [PATCH] Add property for JNDI DataSource lookup Add `spring.datasource.jndi-name` property to allow a DataSource to be looked up from JNDI as an alternative to defining a URL connection. Fixes gh-989 --- .../jdbc/DataSourceProperties.java | 16 ++++++ .../jdbc/JndiDataSourceAutoConfiguration.java | 52 +++++++++++++++++++ .../main/resources/META-INF/spring.factories | 1 + .../appendix-application-properties.adoc | 1 + .../main/asciidoc/spring-boot-features.adoc | 21 ++++++++ 5 files changed, 91 insertions(+) create mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfiguration.java diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java index de3524f452..e497399285 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java @@ -46,6 +46,8 @@ public class DataSourceProperties implements BeanClassLoaderAware, InitializingB private ClassLoader classLoader; + private String jndiName; + private boolean initialize = true; private String platform = "all"; @@ -152,6 +154,20 @@ public class DataSourceProperties implements BeanClassLoaderAware, InitializingB this.password = password; } + public String getJndiName() { + return this.jndiName; + } + + /** + * Allows the DataSource to be managed by the container and obtained via JNDI. The + * {@code URL}, {@code driverClassName}, {@code username} and {@code password} fields + * will be ignored when using JNDI lookups. + * @param jndiName the JNDI name + */ + public void setJndiName(String jndiName) { + this.jndiName = jndiName; + } + public boolean isInitialize() { return this.initialize; } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfiguration.java new file mode 100644 index 0000000000..735ab89357 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfiguration.java @@ -0,0 +1,52 @@ +/* + * Copyright 2012-2014 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.jdbc; + +import javax.sql.DataSource; + +import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup; + +/** + * {@link EnableAutoConfiguration Auto-configuration} for a JNDI located + * {@link DataSource}. + * + * @author Phillip Webb + * @since 1.2.0 + */ +@Configuration +@AutoConfigureBefore(DataSourceAutoConfiguration.class) +@ConditionalOnClass(DataSource.class) +@ConditionalOnProperty(prefix = DataSourceProperties.PREFIX, name = "jndi-name") +@EnableConfigurationProperties(DataSourceProperties.class) +public class JndiDataSourceAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public DataSource dataSource(DataSourceProperties properties) { + JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup(); + return dataSourceLookup.getDataSource(properties.getJndiName()); + } + +} diff --git a/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories index 7f3580dad7..8f1ba96768 100644 --- a/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories @@ -19,6 +19,7 @@ org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\ org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\ org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\ +org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\ org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration,\ org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration,\ diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 49a2045a4e..2a73dc7843 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -175,6 +175,7 @@ content into your application; rather pick only the properties that you need. spring.datasource.url= spring.datasource.username= spring.datasource.password= + spring.datasource.jndi-name # For JNDI lookup (class, url, username & password are ignored when set) spring.datasource.max-active=100 # Advanced configuration... spring.datasource.max-idle=8 spring.datasource.min-idle=8 diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 9093b2abe2..1774bddbf6 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -1272,6 +1272,27 @@ NOTE: For a pooling `DataSource` to be created we need to be able to verify that `spring.datasource.driverClassName=com.mysql.jdbc.Driver` then that class has to be loadable. + + +[[boot-features-connecting-to-a-jndi-datasource]] +==== Connection to a JNDI DataSource +If you are deploying your Spring Boot application to an Application Server you might want +to configure and manage your DataSource using you Application Servers built in features +and access it using JNDI. + +The `spring.datasource.jndi-name` property can be used as an alternative to the +`spring.datasource.url`, `spring.datasource.username` and `spring.datasource.password` +properties to access the `DataSource` from a specific JNDI location. For example, the +following section in `application.properties` shows how you can access a JBoss AS defined +`DataSource`: + +[source,properties,indent=0] +---- + spring.datasource.jndi-name=java:jboss/datasources/customers +---- + + + [[boot-features-using-jdbc-template]] === Using JdbcTemplate Spring's `JdbcTemplate` and `NamedParameterJdbcTemplate` classes are auto-configured and