类 UserCredentialsDataSourceAdapter
- java.lang.Object
- org.springframework.jdbc.datasource.DelegatingDataSource
- org.springframework.jdbc.datasource.UserCredentialsDataSourceAdapter
- 所有已实现的接口:
Wrapper
,CommonDataSource
,DataSource
,InitializingBean
public class UserCredentialsDataSourceAdapter extends DelegatingDataSource
An adapter for a target JDBCDataSource
, applying the specified user credentials to every standardgetConnection()
call, implicitly invokinggetConnection(username, password)
on the target. All other methods simply delegate to the corresponding methods of the target DataSource.Can be used to proxy a target JNDI DataSource that does not have user credentials configured. Client code can work with this DataSource as usual, using the standard
getConnection()
call.In the following example, client code can simply transparently work with the preconfigured "myDataSource", implicitly accessing "myTargetDataSource" with the specified user credentials.
<bean id="myTargetDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/jdbc/myds"/> </bean> <bean id="myDataSource" class="org.springframework.jdbc.datasource.UserCredentialsDataSourceAdapter"> <property name="targetDataSource" ref="myTargetDataSource"/> <property name="username" value="myusername"/> <property name="password" value="mypassword"/> </bean>
If the "username" is empty, this proxy will simply delegate to the standard
getConnection()
method of the target DataSource. This can be used to keep a UserCredentialsDataSourceAdapter bean definition just for the option of implicitly passing in user credentials if the particular target DataSource requires it.- 从以下版本开始:
- 1.0.2
- 作者:
- Juergen Hoeller
- 另请参阅:
getConnection()
构造器概要
构造器 构造器 说明 UserCredentialsDataSourceAdapter()
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 protected Connection
doGetConnection(String username, String password)
This implementation delegates to thegetConnection(username, password)
method of the target DataSource, passing in the specified user credentials.Connection
getConnection()
Determine whether there are currently thread-bound credentials, using them if available, falling back to the statically specified username and password (i.e. values of the bean properties) else.Connection
getConnection(String username, String password)
Simply delegates todoGetConnection(String, String)
, keeping the given user credentials as-is.void
removeCredentialsFromCurrentThread()
Remove any user credentials for this proxy from the current thread.void
setCatalog(String catalog)
Specify a database catalog to be applied to each retrieved Connection.void
setCredentialsForCurrentThread(String username, String password)
Set user credententials for this proxy and the current thread.void
setPassword(String password)
Set the default user's password that this adapter should use for retrieving Connections.void
setSchema(String schema)
Specify a database schema to be applied to each retrieved Connection.void
setUsername(String username)
Set the default username that this adapter should use for retrieving Connections.从类继承的方法 org.springframework.jdbc.datasource.DelegatingDataSource
afterPropertiesSet, getLoginTimeout, getLogWriter, getParentLogger, getTargetDataSource, isWrapperFor, setLoginTimeout, setLogWriter, setTargetDataSource, unwrap
从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
从接口继承的方法 javax.sql.CommonDataSource
createShardingKeyBuilder
从接口继承的方法 javax.sql.DataSource
createConnectionBuilder
构造器详细资料
UserCredentialsDataSourceAdapter
public UserCredentialsDataSourceAdapter()
方法详细资料
setUsername
public void setUsername(String username)
Set the default username that this adapter should use for retrieving Connections.Default is no specific user. Note that an explicitly specified username will always override any username/password specified at the DataSource level.
setPassword
public void setPassword(String password)
Set the default user's password that this adapter should use for retrieving Connections.Default is no specific password. Note that an explicitly specified username will always override any username/password specified at the DataSource level.
setCatalog
public void setCatalog(String catalog)
Specify a database catalog to be applied to each retrieved Connection.- 从以下版本开始:
- 4.3.2
- 另请参阅:
Connection.setCatalog(java.lang.String)
setSchema
public void setSchema(String schema)
Specify a database schema to be applied to each retrieved Connection.- 从以下版本开始:
- 4.3.2
- 另请参阅:
Connection.setSchema(java.lang.String)
setCredentialsForCurrentThread
public void setCredentialsForCurrentThread(String username, String password)
Set user credententials for this proxy and the current thread. The given username and password will be applied to all subsequentgetConnection()
calls on this DataSource proxy.This will override any statically specified user credentials, that is, values of the "username" and "password" bean properties.
- 参数:
username
- the username to applypassword
- the password to apply- 另请参阅:
removeCredentialsFromCurrentThread()
removeCredentialsFromCurrentThread
public void removeCredentialsFromCurrentThread()
Remove any user credentials for this proxy from the current thread. Statically specified user credentials apply again afterwards.
getConnection
@UsesJava7 public Connection getConnection() throws SQLException
Determine whether there are currently thread-bound credentials, using them if available, falling back to the statically specified username and password (i.e. values of the bean properties) else.Delegates to
doGetConnection(String, String)
with the determined credentials as parameters.- 指定者:
getConnection
在接口中DataSource
- 覆盖:
getConnection
在类中DelegatingDataSource
- 抛出:
SQLException
getConnection
public Connection getConnection(String username, String password) throws SQLException
Simply delegates todoGetConnection(String, String)
, keeping the given user credentials as-is.- 指定者:
getConnection
在接口中DataSource
- 覆盖:
getConnection
在类中DelegatingDataSource
- 抛出:
SQLException
doGetConnection
protected Connection doGetConnection(String username, String password) throws SQLException
This implementation delegates to thegetConnection(username, password)
method of the target DataSource, passing in the specified user credentials. If the specified username is empty, it will simply delegate to the standardgetConnection()
method of the target DataSource.- 参数:
username
- the username to usepassword
- the password to use- 返回:
- the Connection
- 抛出:
SQLException
- 另请参阅:
DataSource.getConnection(String, String)
,DataSource.getConnection()