Java Reference
In-Depth Information
is not the appropriate location for its details to be stored. Moreover, a well-behaved web appli-
cation will draw its database configuration from a JNDI-provided
DataSource
object so that
connection details can be uniformly managed at deployment time.
Spring allows data sources to be managed centrally as beans, and if a
JndiObjectFactoryBean
bean is used, it can in turn draw its details from JNDI. The
LocalSessionFactoryBean
therefore provides a
dataSource
property into which the appro-
priate Spring
DataSource
bean can be injected.
Typically, to manage a data source from within the Spring configuration, but without
deferring the details to a JNDI resource, you would use the
DriverManagerDataSource
bean
(see Listing C-4).
Listing C-4.
Configuring a Typical
BasicDataSource
Bean
<bean id="sampleDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
destroy-method="close">
<property name="driverClassName">
<value>org.hsqldb.jdbcDriver</value>
</property>
<property name="url">
<value>
jdbc:hsqldb:file:/spring/db/springdb;SHUTDOWN=true
</value>
</property>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
Alternatively, if the data source resources are to be drawn from an existing JNDI-accessi-
ble data source, then the Spring
JndiObjectFactoryBean
should be used to represent the data
source (see Listing C-5).
Listing C-5.
Configuring a Typical
JndiObjectFactoryBean
<bean id="sampleDataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/spring"/>
</bean>
It is not just the connection details that can be migrated from the Hibernate configuration
file into the Spring configuration. The property attributes and the mappings (class names or
mapping file names) can also be assigned during the configuration of a
LocalSessionFactory
bean (see Listing C-6).
Search WWH ::
Custom Search