TechnoBuzz

A Techno Blog, mainly about Java

SourceBeat Chapter

Based on post a few months back on a positive experience with the Spring Live chapter 2 [tss] , thought I’d finally try it out. [forum] [wiki FAQ]
JAVA_HOME is C:\j2sdk1.4.2_05\
ANT_HOME is C:\apache\apache-ant-1.6.2
CATALINA_HOME is C:\apache\jakarta-tomcat-5.0.27
PATH has C:\apache\apache-ant-1.6.2\bin;C:\j2sdk1.4.2_05\bin; C:\apache\jakarta-tomcat-5.0.27\bin

https://equinox.dev.java.net/files/documents/1901/5728/equinox-1.0.zip extract to C:\sourcebeat\

C:\sourcebeat\equinox>ant new -Dapp.name=myusers

copy  C:\sourcebeat\myusers to CATALINA_HOME\webapps

CATALINA_HOME\conf\tomcat-users I added
  <user username="admin" password="admin" roles="manager"/>

C:\apache\jakarta-tomcat-5.0.27\webapps\myusers>ant list

C:\apache\jakarta-tomcat-5.0.27\webapps\myusers>ant deploy

went into manager http://localhost:8080/manager and add myusers directory

http://localhost:8080/myusers/&nbsp; , get page with:
"Equinox is a lightweight version of AppFuse. I was inspired to create it when looking at the struts-blank and webapp-minimal applications that ship with Struts and Spring, respectively. These "starter" apps were not robust enough for me, and I wanted something like AppFuse, only simpler. "

Code up UserDAO ,  User, User.hbm.xml   [Spring live source code]

Here, I found I must change the url  in the WEB-INF/applicationContext.xml which I changed to
<property name="url"><value>jdbc:hsqldb:C:/apache/jakarta-tomcat-5.0.27/webapps/myusers/db/appfuse</value></property>

Also, in this file set the MappingResources property value to indicate the location of
org/appfuse/model/User.hbm.xml

C:\apache\jakarta-tomcat-5.0.27\webapps\myusers>ant deploy reload

C:\apache\jakarta-tomcat-5.0.27\webapps\myusers>ant browse

The place where you configure a Java EE app to use Spring is in the web.xml file. What you do is
set the listener :
<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

The next step is configuring spring with hibernate. We will look in the applicationContext.xml
The session factory bean that has a dependency on data source bean:

<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
        <property name="dataSource"><ref local="dataSource"/></property>
        <property name="mappingResources">
            <list>
                <value>org/appfuse/model/User.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">net.sf.hibernate.dialect.HSQLDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
        </props>
        </property>
    </bean>

The data source bean:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName"><value>org.hsqldb.jdbcDriver</value></property>
        <property name="url"><value>jdbc:hsqldb:C:/apache/jakarta-tomcat-5.0.27/webapps/myusers/db/appfuse</value></property>
        <property name="username"><value>sa</value></property>
        <property name="password"><value></value></property>
    </bean>

The transaction manager bean which depends on the session factory:

<bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
        <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>

Now we code up UserDAOHibernate  [Spring live source code]

In applicationContext.xml we create a  relationship to the UserDAOHibernate :

<bean id="userDAO" class="org.appfuse.dao.hibernate.UserDAOHibernate"> <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean>

[myusers w/eclipse]

Some Spring links:

 

Advertisements

July 12, 2005 - Posted by | IoC

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: