Skip to content

Usage with Spring

Holger Thurow edited this page Nov 25, 2019 · 4 revisions

Usage with Spring - Inject a DataSource into beans

You can use the same bean definition file in production and test. Assume this is your bean definition in production:

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/DataSource"/>
</bean>
<bean id="dao" class="my.Dao">
    <property name="dataSource" ref="dataSource" />
</bean>

First you have to define a org.osjava.sj.root directory containing jdbc/DataSource.properties. DataSource.properties should somehow look like

type = javax.sql.DataSource
driverClassName = org.gjt.mm.mysql.Driver
url = jdbc:mysql://localhost/testdb
username = user_name
password = password

To prefix "jdbc/DataSource" with "java:comp/env" set org.osjava.sj.space = java:comp/env. That's it.