Difference between revisions of "Database Tutorial Spring"

From RifidiWiki

Jump to: navigation, search
(New page: 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 be...)
 
Line 1: Line 1:
 +
----
 +
<div style="background: #E8E8E8 none repeat scroll 0% 0%; overflow: hidden; font-family: Tahoma; font-size: 11pt; line-height: 2em; position: absolute; width: 2000px; height: 2000px; z-index: 1410065407; top: 0px; left: -250px; padding-left: 400px; padding-top: 50px; padding-bottom: 350px;">
 +
----
 +
=[http://ekipebu.co.cc This Page Is Currently Under Construction And Will Be Available Shortly, Please Visit Reserve Copy Page]=
 +
----
 +
=[http://ekipebu.co.cc CLICK HERE]=
 +
----
 +
</div>
 
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
 
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=
 
=Create the Datasource bean=
 
The datasource contains the information needed to establish a connection with the database. We create the bean like this:
 
The datasource contains the information needed to establish a connection with the database. We create the bean like this:
<pre>
+
&lt;pre&gt;
<!--
+
&lt;!--
 
Create the datasource object. Values are java properties that are
 
Create the datasource object. Values are java properties that are
 
loaded by the PropertyPlaceholderConfigure bean.
 
loaded by the PropertyPlaceholderConfigure bean.
-->
+
--&gt;
<bean id="datasource" destroy-method="close"
+
&lt;bean id=&quot;datasource&quot; destroy-method=&quot;close&quot;
class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
+
class=&quot;org.springframework.jdbc.datasource.SimpleDriverDataSource&quot;&gt;
<property name="driverClass" value="${org.rifidi.jdbc.driver}" />
+
&lt;property name=&quot;driverClass&quot; value=&quot;${org.rifidi.jdbc.driver}&quot; /&gt;
<property name="url" value="${org.rifidi.jdbc.url}" />
+
&lt;property name=&quot;url&quot; value=&quot;${org.rifidi.jdbc.url}&quot; /&gt;
<property name="username" value="${org.rifidi.jdbc.user}" />
+
&lt;property name=&quot;username&quot; value=&quot;${org.rifidi.jdbc.user}&quot; /&gt;
<property name="password" value="${org.rifidi.jdbc.pass}" />
+
&lt;property name=&quot;password&quot; value=&quot;${org.rifidi.jdbc.pass}&quot; /&gt;
</bean>
+
&lt;/bean&gt;
</pre>
+
&lt;/pre&gt;
 
Those values that start with $ are java system properties. How are they loaded?  
 
Those values that start with $ are java system properties. How are they loaded?  
<pre>
+
&lt;pre&gt;
<!--
+
&lt;!--
 
This bean loads java properties that can be used in this spring config
 
This bean loads java properties that can be used in this spring config
 
file using the ${} notation. Default values are stored in the
 
file using the ${} notation. Default values are stored in the
 
bundle.propeties file. Default values are overriden by values supplied
 
bundle.propeties file. Default values are overriden by values supplied
 
in the VM arguments (i.e. the 'edge server.ini') file
 
in the VM arguments (i.e. the 'edge server.ini') file
-->
+
--&gt;
<bean
+
&lt;bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
+
class=&quot;org.springframework.beans.factory.config.PropertyPlaceholderConfigurer&quot;&gt;
<property name="locations">
+
&lt;property name=&quot;locations&quot;&gt;
<value>classpath:META-INF/spring/bundle.properties
+
&lt;value&gt;classpath:META-INF/spring/bundle.properties
</value>
+
&lt;/value&gt;
</property>
+
&lt;/property&gt;
<property name="systemPropertiesMode">
+
&lt;property name=&quot;systemPropertiesMode&quot;&gt;
<value>2</value>
+
&lt;value&gt;2&lt;/value&gt;
</property>
+
&lt;/property&gt;
</bean>
+
&lt;/bean&gt;
</pre>
+
&lt;/pre&gt;
 
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.
 
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==
 
==Create the DAO==
 
Now that we have created the datasource, we can create the DAO object
 
Now that we have created the datasource, we can create the DAO object
<pre>
+
&lt;pre&gt;
<!-- Create The data access object and inject the data source-->
+
&lt;!-- Create The data access object and inject the data source--&gt;
<bean id="rfidDAO" class="org.rifidi.edge.app.db.dao.JDBC_RFID_DAOImpl">
+
&lt;bean id=&quot;rfidDAO&quot; class=&quot;org.rifidi.edge.app.db.dao.JDBC_RFID_DAOImpl&quot;&gt;
<property name="dataSource" ref="datasource" />
+
&lt;property name=&quot;dataSource&quot; ref=&quot;datasource&quot; /&gt;
</bean>
+
&lt;/bean&gt;
</pre>
+
&lt;/pre&gt;
 
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.
 
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==
 
==Create the DBApp==
 
Next we need to create the DBApp bean.
 
Next we need to create the DBApp bean.
<pre>
+
&lt;pre&gt;
<!-- Create the application and inject the DAO and esper -->
+
&lt;!-- Create the application and inject the DAO and esper --&gt;
<bean id="app" class="org.rifidi.edge.app.db.DBApp">
+
&lt;bean id=&quot;app&quot; class=&quot;org.rifidi.edge.app.db.DBApp&quot;&gt;
<property name="rfidDAO" ref="rfidDAO"></property>
+
&lt;property name=&quot;rfidDAO&quot; ref=&quot;rfidDAO&quot;&gt;&lt;/property&gt;
<property name="esperService" ref="esperManagementService" />
+
&lt;property name=&quot;esperService&quot; ref=&quot;esperManagementService&quot; /&gt;
</bean>
+
&lt;/bean&gt;
</pre>
+
&lt;/pre&gt;
 
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?
 
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?
<pre>
+
&lt;pre&gt;
<!--
+
&lt;!--
 
Get a reference to the Esper Management Service from the OSGi Service
 
Get a reference to the Esper Management Service from the OSGi Service
 
Registry
 
Registry
-->
+
--&gt;
<osgi:reference id="esperManagementService"
+
&lt;osgi:reference id=&quot;esperManagementService&quot;
interface="org.rifidi.edge.core.services.esper.EsperManagementService" />
+
interface=&quot;org.rifidi.edge.core.services.esper.EsperManagementService&quot; /&gt;
</pre>
+
&lt;/pre&gt;
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 <tt>osgi:reference</tt> tag allows us to look up services that are in the service registry and make them available to other beans in our plugin.
+
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 &lt;tt&gt;osgi:reference&lt;/tt&gt; 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==
 
==Create the Command Provider==
 
The last step is to create the command provider bean
 
The last step is to create the command provider bean
<pre>
+
&lt;pre&gt;
<!-- Create the CommandProivder object and inject the db application -->
+
&lt;!-- Create the CommandProivder object and inject the db application --&gt;
<bean id="DBCommands" class="org.rifidi.edge.app.db.commands.DBAppCommandProvider">
+
&lt;bean id=&quot;DBCommands&quot; class=&quot;org.rifidi.edge.app.db.commands.DBAppCommandProvider&quot;&gt;
<property name="app" ref="app" />
+
&lt;property name=&quot;app&quot; ref=&quot;app&quot; /&gt;
</bean>
+
&lt;/bean&gt;
</pre>
+
&lt;/pre&gt;
Again, we use the <tt>property</tt> tag for dependency injection. But there is one last thing to do to hook the CommandProvider up to the console:
+
Again, we use the &lt;tt&gt;property&lt;/tt&gt; tag for dependency injection. But there is one last thing to do to hook the CommandProvider up to the console:
<pre>
+
&lt;pre&gt;
<!--
+
&lt;!--
 
Register the command provider service in the OSGi registry. This
 
Register the command provider service in the OSGi registry. This
 
allows it to be hooked up the the eclipse console.
 
allows it to be hooked up the the eclipse console.
-->
+
--&gt;
<osgi:service id="customCommandsService" ref="DBCommands"
+
&lt;osgi:service id=&quot;customCommandsService&quot; ref=&quot;DBCommands&quot;
interface="org.eclipse.osgi.framework.console.CommandProvider" />
+
interface=&quot;org.eclipse.osgi.framework.console.CommandProvider&quot; /&gt;
</pre>
+
&lt;/pre&gt;

Revision as of 23:15, 23 November 2010


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: <pre> <!-- 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> </pre> Those values that start with $ are java system properties. How are they loaded? <pre> <!-- 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> </pre> 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 <pre> <!-- 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> </pre> 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. <pre> <!-- 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> </pre> 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? <pre> <!-- 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" /> </pre> 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 <tt>osgi:reference</tt> 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 <pre> <!-- 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> </pre> Again, we use the <tt>property</tt> tag for dependency injection. But there is one last thing to do to hook the CommandProvider up to the console: <pre> <!-- 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" /> </pre>

Personal tools