Database Tutorial Spring

From RifidiWiki

Jump to: navigation, search

The spring configuration file is the glue that binds all the classes that we have together. It handles dependency injection and interaction with the OSGi registry

Create the Datasource bean

The datasource contains the information needed to establish a connection with the database. We create the bean like this:

	<!--
		Create the datasource object. Values are java properties that are
		loaded by the PropertyPlaceholderConfigure bean.
	-->
	<bean id="datasource" destroy-method="close"
		class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
		<property name="driverClass" value="${org.rifidi.jdbc.driver}" />
		<property name="url" value="${org.rifidi.jdbc.url}" />
		<property name="username" value="${org.rifidi.jdbc.user}" />
		<property name="password" value="${org.rifidi.jdbc.pass}" />
	</bean>

Those values that start with $ are java system properties. How are they loaded?

	<!--
		This bean loads java properties that can be used in this spring config
		file using the ${} notation. Default values are stored in the
		bundle.propeties file. Default values are overriden by values supplied
		in the VM arguments (i.e. the 'edge server.ini') file
	-->
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>classpath:META-INF/spring/bundle.properties
			</value>
		</property>
		<property name="systemPropertiesMode">
			<value>2</value>
		</property>
	</bean>

This creates a spring bean that loads java system properties. We can specify these properties via VM arguments when we start up the edge server (look at all those properties that start with -D in the 'edge server.ini' file in an edge server installation). Just to make things extra convenient, we can specify default values in a file if we want to. This is what the bundle.properties file is.

Create the DAO

Now that we have created the datasource, we can create the DAO object

	<!-- Create The data access object and inject the data source-->
	<bean id="rfidDAO" class="org.rifidi.edge.app.db.dao.JDBC_RFID_DAOImpl">
		<property name="dataSource" ref="datasource" />
	</bean>

Notice that we have injected the data source into the dao (using the property tag). When injecting one bean into another, we just need to make sure the ref attribute in the bean matches up with the id attribute of the dependency bean.

Create the DBApp

Next we need to create the DBApp bean.

	<!-- Create the application and inject the DAO and esper -->
	<bean id="app" class="org.rifidi.edge.app.db.DBApp">
		<property name="rfidDAO" ref="rfidDAO"></property>
		<property name="esperService" ref="esperManagementService" />
	</bean>

It should be clear how the DAO is injected at this point. We created it in the previous step. But how does spring know about the 'esperManagementService' bean?

	<!--
		Get a reference to the Esper Management Service from the OSGi Service
		Registry
	-->
	<osgi:reference id="esperManagementService"
		interface="org.rifidi.edge.core.services.esper.EsperManagementService" />

The EsperManagementService is created by the edge server core and exposed as a service for edge server applications to use by placing it in the OSGi service registry. The osgi:reference tag allows us to look up services that are in the service registry and make them available to other beans in our plugin.

Create the Command Provider

The last step is to create the command provider bean

	<!-- Create the CommandProivder object and inject the db application -->
	<bean id="DBCommands" class="org.rifidi.edge.app.db.commands.DBAppCommandProvider">
		<property name="app" ref="app" />
	</bean>

Again, we use the property tag for dependency injection. But there is one last thing to do to hook the CommandProvider up to the console:

	<!--
		Register the command provider service in the OSGi registry. This
		allows it to be hooked up the the eclipse console.
	-->
	<osgi:service id="customCommandsService" ref="DBCommands"
		interface="org.eclipse.osgi.framework.console.CommandProvider" />
Personal tools