2. Simple Spring MVC DataSource Bundle

The DataSource bundle creates an in memory HSQL database and creates a connection pool that is exposed as an OSGi DataSource service.

Manifest Configuration

The manifest defines the bundles symbolic name, and imports javax.sql, Spring, HSQL DB, and Commons DBCP.

[Note]Note

If when opening the manifest and viewing the Dependencies tab in the Ecilpse IDE, there isn't an 'Import Bundle' section on the right and an 'Import Library' section underneath it the default Eclipse manifest editor may have been opened. To open the Spring manifest editor, right click, 'Open with'/'Other...', and then choose the 'Bundle Manifest Editor'.

src/main/resources/META-INF/MANIFEST.MF
                    
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Simple Spring MVC Form DataSource Bundle
Bundle-Description: Simple Spring MVC Form DataSource
Bundle-SymbolicName: org.springbyexample.sdms.simpleForm.datasource  1
Bundle-Version: 1.0.0  2
Bundle-Vendor: Spring by Example
Import-Package: javax.sql  3
Import-Library: org.springframework.spring;version="[2.5.5,3.0.0)"  4
Import-Bundle: com.springsource.org.hsqldb;version="[1.8.0,2.0.0)",
 com.springsource.org.apache.commons.dbcp;version="[1.2.2.osgi,1.3.0)"  5
                    
                
1 The symbolic name the bundle is deployed under and another bundle could use to reference it.
2 The version the bundle is deployed under.
3 Imports the package javax.sql.
4 The Import-Library property imports all the related Spring bundles for the latest version of the library that can be resolved based on the version range.
5 Imports the HSQL DB and Commons DBCP bundles, retrieving the newest version possible based on the version ranges specified.

Spring Configuration

By default any spring configuration files in the META-INF/spring will be processed (this is configurable). A connection pool is created using Commons DBCP to an HSQL DB and then the pool is exposed as an OSGi service.

src/main/resources/META-INF/spring/bundle-context.xml
                    
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
		<property name="url" value="jdbc:hsqldb:mem:person" />
		<property name="username" value="sa" />
		<property name="password" value="" />
	</bean>
	
</beans>
                    
                

The Spring Dynamic Modules' namespace is setup as the default namespace for the configuration file. The service element exposes the dataSource bean as an OSGi service under the interface javax.sql.DataSource.

src/main/resources/META-INF/spring/bundle-context-osgi.xml
                    
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/osgi"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:beans="http://www.springframework.org/schema/beans"
             xsi:schemaLocation="http://www.springframework.org/schema/osgi  
                                 http://www.springframework.org/schema/osgi/spring-osgi-1.0.xsd
                                 http://www.springframework.org/schema/beans   
                                 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <service ref="dataSource" interface="javax.sql.DataSource" />

</beans:beans>