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
This commit is contained in:
parent
8656402ca6
commit
a4925dabf7
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
|
@ -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,\
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue