public class SqlMapClientFactoryBean extends java.lang.Object implements FactoryBean<com.ibatis.sqlmap.client.SqlMapClient>, InitializingBean
FactoryBeanthat creates an iBATIS
SqlMapClient. This is the usual way to set up a shared iBATIS SqlMapClient in a Spring application context; the SqlMapClient can then be passed to iBATIS-based DAOs via dependency injection.
JtaTransactionManager can be
used for transaction demarcation in combination with a SqlMapClient,
with JTA only necessary for transactions which span multiple databases.
Allows for specifying a DataSource at the SqlMapClient level. This is preferable to per-DAO DataSource references, as it allows for lazy loading and avoids repeated DataSource references in every DAO.
Note: As of Spring 2.5.5, this class (finally) requires iBATIS 2.3 or higher. The new "mappingLocations" feature requires iBATIS 2.3.2.
|Constructor and Description|
|Modifier and Type||Method and Description|
Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).
Apply the given iBATIS TransactionConfig to the SqlMapClient.
Build a SqlMapClient instance based on the given standard configuration.
Return the LobHandler for the currently configured iBATIS SqlMapClient, to be used by TypeHandler implementations like ClobStringTypeHandler.
Return an instance (possibly shared or independent) of the object managed by this factory.
Return the type of object that this FactoryBean creates, or
Is the object managed by this factory a singleton? That is, will
Set the location of the iBATIS SqlMapClient config file.
Set multiple locations of iBATIS SqlMapClient config files that are going to be merged into one unified configuration at runtime.
Set the DataSource to be used by iBATIS SQL Maps.
Set the LobHandler to be used by the SqlMapClient.
Set locations of iBATIS sql-map mapping files that are going to be merged into the SqlMapClient configuration at runtime.
Set optional properties to be passed into the SqlMapClientBuilder, as alternative to a
Set the iBATIS TransactionConfig class to use.
Set properties to be passed to the TransactionConfig instance used by this SqlMapClient.
Set whether to use a transaction-aware DataSource for the SqlMapClient, i.e.
public static LobHandler getConfigTimeLobHandler()
This instance will be set before initialization of the corresponding SqlMapClient, and reset immediately afterwards. It is thus only available during configuration.
public void setConfigLocation(Resource configLocation)
public void setConfigLocations(Resource configLocations)
public void setMappingLocations(Resource mappingLocations)
This is an alternative to specifying "<sqlMap>" entries in a sql-map-client config file. This property being based on Spring's resource abstraction also allows for specifying resource patterns here: e.g. "/myApp/*-map.xml".
Note that this feature requires iBATIS 2.3.2; it will not work with any previous iBATIS version.
public void setSqlMapClientProperties(java.util.Properties sqlMapClientProperties)
<properties>tag in the sql-map-config.xml file. Will be used to resolve placeholders in the config file.
public void setDataSource(javax.sql.DataSource dataSource)
If specified, this will override corresponding settings in the SqlMapClient properties. Usually, you will specify DataSource and transaction configuration either here or in SqlMapClient properties.
Specifying a DataSource for the SqlMapClient rather than for each individual DAO allows for lazy loading, for example when using PaginatedList results.
With a DataSource passed in here, you don't need to specify one for each DAO. Passing the SqlMapClient to the DAOs is enough, as it already carries a DataSource. Thus, it's recommended to specify the DataSource at this central location only.
Thanks to Brandon Goodin from the iBATIS team for the hint on how to make this work with Spring's integration strategy!
public void setUseTransactionAwareDataSource(boolean useTransactionAwareDataSource)
Default is "true": When the SqlMapClient performs direct database operations outside of Spring's SqlMapClientTemplate (for example, lazy loading or direct SqlMapClient access), it will still participate in active Spring-managed transactions.
As a further effect, using a transaction-aware DataSource will apply remaining transaction timeouts to all created JDBC Statements. This means that all operations performed by the SqlMapClient will automatically participate in Spring-managed transaction timeouts.
Turn this flag off to get raw DataSource handling, without Spring transaction checks. Operations on Spring's SqlMapClientTemplate will still detect Spring-managed transactions, but lazy loading or direct SqlMapClient access won't.
public void setTransactionConfigClass(java.lang.Class transactionConfigClass)
Will only get applied when using a Spring-managed DataSource. An instance of this class will get populated with the given DataSource and initialized with the given properties.
The default ExternalTransactionConfig is appropriate if there is external transaction management that the SqlMapClient should participate in: be it Spring transaction management, EJB CMT or plain JTA. This should be the typical scenario. If there is no active transaction, SqlMapClient operations will execute SQL statements non-transactionally.
JdbcTransactionConfig or JtaTransactionConfig is only necessary when using the iBATIS SqlMapTransactionManager API instead of external transactions. If there is no explicit transaction, SqlMapClient operations will automatically start a transaction for their own scope (in contrast to the external transaction mode, see above).
It is strongly recommended to use iBATIS SQL Maps with Spring transaction management (or EJB CMT). In this case, the default ExternalTransactionConfig is fine. Lazy loading and SQL Maps operations without explicit transaction demarcation will execute non-transactionally.
Even with Spring transaction management, it might be desirable to specify JdbcTransactionConfig: This will still participate in existing Spring-managed transactions, but lazy loading and operations without explicit transaction demaration will execute in their own auto-started transactions. However, this is usually not necessary.
public void setTransactionConfigProperties(java.util.Properties transactionConfigProperties)
public void setLobHandler(LobHandler lobHandler)
public void afterPropertiesSet() throws java.lang.Exception
This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.
protected com.ibatis.sqlmap.client.SqlMapClient buildSqlMapClient(Resource configLocations, Resource mappingLocations, java.util.Properties properties) throws java.io.IOException
The default implementation uses the standard iBATIS
API to build a SqlMapClient instance based on an InputStream (if possible,
on iBATIS 2.3 and higher) or on a Reader (on iBATIS up to version 2.2).
configLocations- the config files to load from
properties- the SqlMapClient properties (if any)
java.io.IOException- if loading the config file failed
protected void applyTransactionConfig(com.ibatis.sqlmap.client.SqlMapClient sqlMapClient, com.ibatis.sqlmap.engine.transaction.TransactionConfig transactionConfig)
The default implementation casts to ExtendedSqlMapClient, retrieves the maximum number of concurrent transactions from the SqlMapExecutorDelegate, and sets an iBATIS TransactionManager with the given TransactionConfig.
sqlMapClient- the SqlMapClient to apply the TransactionConfig to
transactionConfig- the iBATIS TransactionConfig to apply
public com.ibatis.sqlmap.client.SqlMapClient getObject()
As with a
BeanFactory, this allows support for both the
Singleton and Prototype design pattern.
If this FactoryBean is not fully initialized yet at the time of
the call (for example because it is involved in a circular reference),
throw a corresponding
As of Spring 2.0, FactoryBeans are allowed to return
objects. The factory will consider this as normal value to be used; it
will not throw a FactoryBeanNotInitializedException in this case anymore.
FactoryBean implementations are encouraged to throw
FactoryBeanNotInitializedException themselves now, as appropriate.
public java.lang.Class<? extends com.ibatis.sqlmap.client.SqlMapClient> getObjectType()
nullif not known in advance.
This allows one to check for specific types of beans without instantiating objects, for example on autowiring.
In the case of implementations that are creating a singleton object, this method should try to avoid singleton creation as far as possible; it should rather estimate the type in advance. For prototypes, returning a meaningful type here is advisable too.
This method can be called before this FactoryBean has been fully initialized. It must not rely on state created during initialization; of course, it can still use such state if available.
NOTE: Autowiring will simply ignore FactoryBeans that return
null here. Therefore it is highly recommended to implement
this method properly, using the current state of the FactoryBean.
nullif not known at the time of the call
public boolean isSingleton()
FactoryBean.getObject()always return the same object (a reference that can be cached)?
NOTE: If a FactoryBean indicates to hold a singleton object,
the object returned from
getObject() might get cached
by the owning BeanFactory. Hence, do not return
unless the FactoryBean always exposes the same reference.
The singleton status of the FactoryBean itself will generally be provided by the owning BeanFactory; usually, it has to be defined as singleton there.
NOTE: This method returning
false does not
necessarily indicate that returned objects are independent instances.
An implementation of the extended
may explicitly indicate independent instances through its
SmartFactoryBean.isPrototype() method. Plain
implementations which do not implement this extended interface are
simply assumed to always return independent instances if the
isSingleton() implementation returns