You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Meindert <me...@pastelebusiness.com> on 2007/04/12 12:03:43 UTC

converting iBATIS framework DAOs to the Spring Framework

Hi all.


I've documented the process of converting jpetshop5, and would like to know
if it is right?

Also I don't know what to do with the com.ibatis.dao.client.DaoException
thrown in SequenceSqlMapDao?

I'm going to add the transaction on OrderService now.

 


Steps to convert jpetshop5 to ibatis-spring 


 

The DAO iBATIS has been depreciated and it is recommended to replace this
with the spring framework <http://www.springframework.org/> . This document
describes the steps taken to convert from ibatis dao to spring dao. I work
on the project in netbeans so some terminology used might be incorrect for
other IDE's

 

Prerequisite is to have a project similar to jpetstore5, and have it
connected to a 'normal' database

(There is support for hssqldb in the DaoConfig class what we are going to
throw away.)

 


Lib jar files;


Remove the old ibatis jar files from the class path and create the reference
to the new ibatis and spring jars

*	Remove ibatis-common-2.jar
*	Remove ibatis-dao-2.jar
*	Remove ibatis-sqlmap-2.jar
*	Add ibatis-2.3.0.677.jar
*	Add spring.jar
*	Add commons-dbcp-1.2.1.jar
*	Add commons-pool-1.2.jar
*	Add commons-collections.jar

 


Com.ibatis.jpetstore.persistence.sqlmapdao


Change the BaseSqlMapDao to;

package com.ibatis.jpetstore.persistence.sqlmapdao;

import org.springframework.orm.ibatis.SqlMapClientTemplate;

public class BaseSqlMapDao extends SqlMapClientTemplate {

    protected static final int PAGE_SIZE = 4;

}

All the other SqlMapDao files;

Remove the public AccountSqlMapDao(DaoManager daoManager)  methode

Remove import com.ibatis.dao.client.DaoManager;


Com.ibatis.jpetstore.service


The DAO will be injected by spring, thus the constructor can be deleted

Example for AccountService, remove the public AccountService()methode

(also remove import com.ibatis.dao.client.DaoManager;)

There is a transaction in OrderService.java, this transaction must be
handled by Spring, remove it here for now. Also remove it out of the
constructor;

public OrderService(ItemDao itemDao, OrderDao orderDao, SequenceDao
sequenceDao) {

    this.itemDao = itemDao;

    this.orderDao = orderDao;

    this.sequenceDao = sequenceDao;

  }

 


The Link between spring and ibatis


Add the SpringInit.java class to a new package com.listeners

This class will be instantiated on load of the website and will pass the
reference to the spring managed objects


Com.ibatis.jpetstore.presentation


 

The presentation layer must connect with the spring managed service layer

So replace;

public AccountBean() {

    this(new AccountService(), new CatalogService());

  }

With

public AccountBean() {

     this((AccountService)
SpringInit.getApplicationContext().getBean("accountService"), 

          (CatalogService)
SpringInit.getApplicationContext().getBean("catalogService"));

  }

And add import com.listeners.SpringInit;

Repeat for the other classes

 


Configuration


First setup the listeners to have SpringInit initiated on loading of the
site

Add to web.xml (after <description> tag)

<context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>/WEB-INF/spring.xml</param-value>

</context-param>

    

<listener>

 
<listener-class>org.springframework.web.context.ContextLoaderListener</liste
ner-class>

</listener>

    

<listener>

 <listener-class>com.listeners.SpringInit</listener-class>

</listener>

 

As you can see it is looking for /WEB-INF/spring.xml create this file.
Attached is the one I created for jpetstore5

 


Cleanup


Throw away DaoConfig.java and dao.xml out of
com.ibatis.jpetstore.persistence, and the reference to it in the Service
classes

 


RE: converting iBATIS framework DAOs to the Spring Framework

Posted by Poitras Christian <Ch...@ircm.qc.ca>.
One danger of adding cglib to your classpath is that lazy objects
created by iBATIS will be harder to serialize.
An example of serialization of "lazy" objects given by Paul here.
http://opensource.atlassian.com/confluence/oss/display/IBATIS/Lazy+loadi
ng+issues
 
Beside this problem, I don't see anything else that could cause a
problem.
 
Christian

________________________________

From: Meindert [mailto:meindert@pastelebusiness.com] 
Sent: Monday, 16 April 2007 04:32
To: user-java@ibatis.apache.org
Subject: RE: converting iBATIS framework DAOs to the Spring Framework



Hi Chris,

 

I did see that in the error message :-).  Do you have any additional
information about why would I need to add CGLIB to enable transactions?

It does give that kind io info on the Project Information;

It is used by AOP, testing, data access frameworks to generate dynamic
proxy objects and intercept field access.

 

Was just wondering if there any dangers or problems if I include this
library?

Should I maybe rather instead of 'generate dynamic proxy objects'
rather add the proxy interface.

That said I don't have a clue what it that means.. I assume I've got to
add something to the spring config file?

 

Sorry about all the questions, but would like to do this simple and
right.

 

Kind Regards

 Meindert

 

 

________________________________

From: Poitras Christian [mailto:Christian.Poitras@ircm.qc.ca] 
Sent: 13 April 2007 06:08 PM
To: user-java@ibatis.apache.org
Subject: RE: converting iBATIS framework DAOs to the Spring Framework

 

Download CGLIB (at least version 2.1.3)

http://sourceforge.net/project/showfiles.php?group_id=56933

 

Christian

 

________________________________

From: Meindert [mailto:meindert@pastelebusiness.com] 
Sent: Friday, 13 April 2007 12:03
To: user-java@ibatis.apache.org; lmeadors@apache.org
Subject: RE: converting iBATIS framework DAOs to the Spring Framework

Hi All,

 

I had a look at spring transactions and can get the annotation working
for the dao layer but not for the service layer?

 

This is the xml I added in the spring config file to enable spring
annotations (Java1.5+ required), 

<!-- TRANSACTIONS -->

    <bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
>

        <property name="dataSource">

            <ref bean="dataSource"/>

        </property>

    </bean>

    

    <bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoPro
xyCreator"/>

    

    <bean
class="org.springframework.transaction.interceptor.TransactionAttributeS
ourceAdvisor">

        <property name="transactionInterceptor" ref="txInterceptor"/>

    </bean>

    

    <bean id="txInterceptor"
class="org.springframework.transaction.interceptor.TransactionIntercepto
r">

        <property name="transactionManager" ref="txManager"/>

        <property name="transactionAttributeSource">

            <bean
class="org.springframework.transaction.annotation.AnnotationTransactionA
ttributeSource"/>

        </property>

    </bean>

 

Step 2, Tested it on the dao layer by modifying updateAccount

@Transactional

public void updateAccount(Account account) {

    update("updateAccount", account);

    int i = 5/0;  //division by zero, expect rollback of account changes

    update("updateProfile", account);

 

 }

This works! But if I move the annotation to the service layer like this

@Transactional

public void updateAccount(Account account) {

     accountDao.updateAccount(account);

}

I get

org.springframework.aop.framework.AopConfigException: Cannot proxy
target class because CGLIB2 is not available. Add CGLIB to the class
path or specify proxy interfaces.

 

Kind Regards

 Meindert

 

-----Original Message-----
From: larry.meadors@gmail.com [mailto:larry.meadors@gmail.com] On Behalf
Of Larry Meadors
Sent: 12 April 2007 04:14 PM
To: user-java@ibatis.apache.org
Subject: Re: converting iBATIS framework DAOs to the Spring Framework

 

Thanks a TON for doing this, it's really appreciated!

 

Here is the WIKI Page: http://tinyurl.com/ysvjw5

 

I just converted it to WIKI format and corrected some simple stuff.

 

I was curious why if we are starting with jpetshop5, why not use the

hsqldb stuff that is there?

 

Also, I'd suggest that we use constructor injection instead of setter

injection - that way, we only need to remove the default constructors

from the service classes (eg., public AccountService() {...}, and the

related imports. It also prevents people from replacing the dao

instances (well, not entirely, but they have to do a bit more work).

 

Larry


RE: converting iBATIS framework DAOs to the Spring Framework

Posted by Meindert <me...@pastelebusiness.com>.
Ok thanks!

I had to change the header for the spring config file to make that work:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"

"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

To

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:aop="http://www.springframework.org/schema/aop"

       xmlns:tx="http://www.springframework.org/schema/tx"

       xsi:schemaLocation="

       http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

       http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd

       http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

 

Meindert

 

  _____  

From: Tom Duffey [mailto:tduffey@utilivisor.com] 
Sent: 16 April 2007 05:01 PM
To: user-java@ibatis.apache.org
Cc: lmeadors@apache.org
Subject: Re: converting iBATIS framework DAOs to the Spring Framework

 

See below.

 

On Apr 16, 2007, at 9:46 AM, Meindert wrote: 



Thus larry could you add the following to finish the converting from
IbatisDao to springDao with ibatis sqlmaps?

3) Spring config

Add the following to the spring config file;

<!-- TRANSACTIONS -->

<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource">

<ref bean="dataSource"/>

</property>

</bean>

<bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCr
eator"/>

<bean
class="org.springframework.transaction.interceptor.TransactionAttributeSourc
eAdvisor">

<property name="transactionInterceptor" ref="txInterceptor"/>

</bean>

<bean id="txInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">

<property name="transactionManager" ref="txManager"/>

<property name="transactionAttributeSource">

<bean
class="org.springframework.transaction.annotation.AnnotationTransactionAttri
buteSource"/>

</property>

</bean>

You can simplify this to:

 

<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource">

<ref bean="dataSource"/>

</property>

</bean>

 

followed by:

 

<tx:annotation-driven transaction-manager="txManager"/>

 

You can even get rid of the transaction-manager="txManager" part if you
rename your DataSourceTransactionManager bean to "transactionManager," e.g.,

 

<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

...

</bean>

 

See: http://tinyurl.com/yqncrj

 

Tom


Re: converting iBATIS framework DAOs to the Spring Framework

Posted by Tom Duffey <td...@utilivisor.com>.
See below.

On Apr 16, 2007, at 9:46 AM, Meindert wrote:
> Thus larry could you add the following to finish the converting  
> from IbatisDao to springDao with ibatis sqlmaps?
>
> 3) Spring config
>
> Add the following to the spring config file;
>
> <!-- TRANSACTIONS -->
>
>     <bean id="txManager"   
> class="org.springframework.jdbc.datasource.DataSourceTransactionManage 
> r">
>
>         <property name="dataSource">
>
>             <ref bean="dataSource"/>
>
>         </property>
>
>     </bean>
>
>
>
>     <bean  
> class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoP 
> roxyCreator"/>
>
>
>
>     <bean  
> class="org.springframework.transaction.interceptor.TransactionAttribut 
> eSourceAdvisor">
>
>         <property name="transactionInterceptor" ref="txInterceptor"/>
>
>     </bean>
>
>
>
>     <bean id="txInterceptor"  
> class="org.springframework.transaction.interceptor.TransactionIntercep 
> tor">
>
>         <property name="transactionManager" ref="txManager"/>
>
>         <property name="transactionAttributeSource">
>
>             <bean  
> class="org.springframework.transaction.annotation.AnnotationTransactio 
> nAttributeSource"/>
>
>         </property>
>
>     </bean>
You can simplify this to:

>     <bean id="txManager"   
> class="org.springframework.jdbc.datasource.DataSourceTransactionManage 
> r">
>         <property name="dataSource">
>
>             <ref bean="dataSource"/>
>
>         </property>
>
>     </bean>

followed by:

<tx:annotation-driven transaction-manager="txManager"/>

You can even get rid of the transaction-manager="txManager" part if  
you rename your DataSourceTransactionManager bean to  
"transactionManager," e.g.,

<bean id="transactionManager"  
class="org.springframework.jdbc.datasource.DataSourceTransactionManager" 
 >
...
</bean>

See: http://tinyurl.com/yqncrj

Tom

RE: converting iBATIS framework DAOs to the Spring Framework

Posted by Meindert <me...@pastelebusiness.com>.
Ok, have to stay on that track then ;-) 

After adding the jar from sourceforce (cglib-2.2_beta1.jar) 

I get the message;

org.springframework.beans.factory.BeanCreationException: Error creating bean
with name 'accountService' defined in ServletContext resource
[/WEB-INF/spring.xml]: Initialization of bean failed; nested exception is
java.lang.NoClassDefFoundError: org/objectweb/asm/Type

Not giving up that easy I tried the jar what is shipped with
'spring-framework-2.0.3-with-dependencies' in the lib folder under cglib. It
is called cglib-nodep-2.1_3.jar

This gave a simple error about 'Superclass has no null constructors but no
arguments were given', added a empty constructor and Walla, it works

 

Thus larry could you add the following to finish the converting from
IbatisDao to springDao with ibatis sqlmaps?

1) Add cglib-nodep-2.1_3.jar to the classpath

 

2) OrderService.java

Add the annotation for spring to implement transactions, meaning 

at @Transactional just above the insertOrder method

at @Transactional(readOnly=true)just above the getOrder method

Add a empty constructor;

public OrderService() {

}

 

3) Spring config

Add the following to the spring config file;

<!-- TRANSACTIONS -->

    <bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <property name="dataSource">

            <ref bean="dataSource"/>

        </property>

    </bean>

    

    <bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCr
eator"/>

    

    <bean
class="org.springframework.transaction.interceptor.TransactionAttributeSourc
eAdvisor">

        <property name="transactionInterceptor" ref="txInterceptor"/>

    </bean>

    

    <bean id="txInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">

        <property name="transactionManager" ref="txManager"/>

        <property name="transactionAttributeSource">

            <bean
class="org.springframework.transaction.annotation.AnnotationTransactionAttri
buteSource"/>

        </property>

    </bean>

 

 


Re: converting iBATIS framework DAOs to the Spring Framework

Posted by Tom Duffey <td...@utilivisor.com>.
On Apr 16, 2007, at 8:27 AM, Meindert wrote:

> I don't think that adding cglib will fix it, the service classes  
> seem to
> need an interface to be able to generate the proxy.
> Please correct me if I'm wrong.

Yes, it should fix the problem.  Spring typically uses JDK-based  
proxies when the target class implements some interface(s).   
Otherwise, CGLIB must be used.  A more detailed description is here:

http://static.springframework.org/spring/docs/2.0.x/reference/aop- 
api.html#aop-pfb-proxy-types

and also see section 7.5.5 that follows.

Tom

> -----Original Message-----
> From: larry.meadors@gmail.com [mailto:larry.meadors@gmail.com] On  
> Behalf Of
> Larry Meadors
> Sent: 16 April 2007 03:10 PM
> To: user-java@ibatis.apache.org
> Subject: Re: converting iBATIS framework DAOs to the Spring Framework
>
> Hmm, I think I'd rather add cglib than give up transactions in the
> service layer.
>
> It's another jar, but reduces complexity (that's unusual when you
> *add* a jar), and puts transaction management where it belongs, IMO.
>
> Larry
>
>
> On 4/16/07, Meindert <me...@pastelebusiness.com> wrote:
>> Had a closer look at it and the logical reason the proxy could not be
>> generated is the fact the service classes (in JPetstore) don't use an
>> interface.
>>
>> I would like to keep it that way, because the reason's I heard for  
>> giving
> it
>> an interface are not convincing (reasons like 'it is the right  
>> thing to
> do',
>> 'easy interchange of implementations') .
>>
>> So (for now) I would say that it is not possible to have a  
>> transaction in
>> the business logic (if the business logic doesn't implement a  
>> interface).
>


RE: converting iBATIS framework DAOs to the Spring Framework

Posted by Meindert <me...@pastelebusiness.com>.
I don't think that adding cglib will fix it, the service classes seem to
need an interface to be able to generate the proxy.
Please correct me if I'm wrong.

-----Original Message-----
From: larry.meadors@gmail.com [mailto:larry.meadors@gmail.com] On Behalf Of
Larry Meadors
Sent: 16 April 2007 03:10 PM
To: user-java@ibatis.apache.org
Subject: Re: converting iBATIS framework DAOs to the Spring Framework

Hmm, I think I'd rather add cglib than give up transactions in the
service layer.

It's another jar, but reduces complexity (that's unusual when you
*add* a jar), and puts transaction management where it belongs, IMO.

Larry


On 4/16/07, Meindert <me...@pastelebusiness.com> wrote:
> Had a closer look at it and the logical reason the proxy could not be
> generated is the fact the service classes (in JPetstore) don't use an
> interface.
>
> I would like to keep it that way, because the reason's I heard for giving
it
> an interface are not convincing (reasons like 'it is the right thing to
do',
> 'easy interchange of implementations') .
>
> So (for now) I would say that it is not possible to have a transaction in
> the business logic (if the business logic doesn't implement a interface).


Re: converting iBATIS framework DAOs to the Spring Framework

Posted by Larry Meadors <lm...@apache.org>.
Hmm, I think I'd rather add cglib than give up transactions in the
service layer.

It's another jar, but reduces complexity (that's unusual when you
*add* a jar), and puts transaction management where it belongs, IMO.

Larry


On 4/16/07, Meindert <me...@pastelebusiness.com> wrote:
> Had a closer look at it and the logical reason the proxy could not be
> generated is the fact the service classes (in JPetstore) don't use an
> interface.
>
> I would like to keep it that way, because the reason's I heard for giving it
> an interface are not convincing (reasons like 'it is the right thing to do',
> 'easy interchange of implementations') .
>
> So (for now) I would say that it is not possible to have a transaction in
> the business logic (if the business logic doesn't implement a interface).

RE: converting iBATIS framework DAOs to the Spring Framework

Posted by Meindert <me...@pastelebusiness.com>.
Hi Christian,

 

Had a closer look at it and the logical reason the proxy could not be
generated is the fact the service classes (in JPetstore) don't use an
interface.

I would like to keep it that way, because the reason's I heard for giving it
an interface are not convincing (reasons like 'it is the right thing to do',
'easy interchange of implementations') .

So (for now) I would say that it is not possible to have a transaction in
the business logic (if the business logic doesn't implement a interface).

 

Kind Regards

 Meindert

 

  _____  

From: Poitras Christian [mailto:Christian.Poitras@ircm.qc.ca] 
Sent: 13 April 2007 06:08 PM
To: user-java@ibatis.apache.org
Subject: RE: converting iBATIS framework DAOs to the Spring Framework

 

Download CGLIB (at least version 2.1.3)

http://sourceforge.net/project/showfiles.php?group_id=56933

 

Christian

 

  _____  

From: Meindert [mailto:meindert@pastelebusiness.com] 
Sent: Friday, 13 April 2007 12:03
To: user-java@ibatis.apache.org; lmeadors@apache.org
Subject: RE: converting iBATIS framework DAOs to the Spring Framework

Hi All,

 

I had a look at spring transactions and can get the annotation working for
the dao layer but not for the service layer?

 

This is the xml I added in the spring config file to enable spring
annotations (Java1.5+ required), 

<!-- TRANSACTIONS -->

    <bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <property name="dataSource">

            <ref bean="dataSource"/>

        </property>

    </bean>

    

    <bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCr
eator"/>

    

    <bean
class="org.springframework.transaction.interceptor.TransactionAttributeSourc
eAdvisor">

        <property name="transactionInterceptor" ref="txInterceptor"/>

    </bean>

    

    <bean id="txInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">

        <property name="transactionManager" ref="txManager"/>

        <property name="transactionAttributeSource">

            <bean
class="org.springframework.transaction.annotation.AnnotationTransactionAttri
buteSource"/>

        </property>

    </bean>

 

Step 2, Tested it on the dao layer by modifying updateAccount

@Transactional

public void updateAccount(Account account) {

    update("updateAccount", account);

    int i = 5/0;  //division by zero, expect rollback of account changes

    update("updateProfile", account);

 

 }

This works! But if I move the annotation to the service layer like this

@Transactional

public void updateAccount(Account account) {

     accountDao.updateAccount(account);

}

I get

org.springframework.aop.framework.AopConfigException: Cannot proxy target
class because CGLIB2 is not available. Add CGLIB to the class path or
specify proxy interfaces.

 

Kind Regards

 Meindert

 

-----Original Message-----
From: larry.meadors@gmail.com [mailto:larry.meadors@gmail.com] On Behalf Of
Larry Meadors
Sent: 12 April 2007 04:14 PM
To: user-java@ibatis.apache.org
Subject: Re: converting iBATIS framework DAOs to the Spring Framework

 

Thanks a TON for doing this, it's really appreciated!

 

Here is the WIKI Page: http://tinyurl.com/ysvjw5

 

I just converted it to WIKI format and corrected some simple stuff.

 

I was curious why if we are starting with jpetshop5, why not use the

hsqldb stuff that is there?

 

Also, I'd suggest that we use constructor injection instead of setter

injection - that way, we only need to remove the default constructors

from the service classes (eg., public AccountService() {...}, and the

related imports. It also prevents people from replacing the dao

instances (well, not entirely, but they have to do a bit more work).

 

Larry


RE: converting iBATIS framework DAOs to the Spring Framework

Posted by Meindert <me...@pastelebusiness.com>.
Hi Chris,

 

I did see that in the error message :-).  Do you have any additional
information about why would I need to add CGLIB to enable transactions?

It does give that kind io info on the Project Information;

It is used by AOP, testing, data access frameworks to generate dynamic proxy
objects and intercept field access.

 

Was just wondering if there any dangers or problems if I include this
library?

Should I maybe rather instead of 'generate dynamic proxy objects'  rather
add the proxy interface.

That said I don't have a clue what it that means.. I assume I've got to add
something to the spring config file?

 

Sorry about all the questions, but would like to do this simple and right.

 

Kind Regards

 Meindert

 

 

  _____  

From: Poitras Christian [mailto:Christian.Poitras@ircm.qc.ca] 
Sent: 13 April 2007 06:08 PM
To: user-java@ibatis.apache.org
Subject: RE: converting iBATIS framework DAOs to the Spring Framework

 

Download CGLIB (at least version 2.1.3)

http://sourceforge.net/project/showfiles.php?group_id=56933

 

Christian

 

  _____  

From: Meindert [mailto:meindert@pastelebusiness.com] 
Sent: Friday, 13 April 2007 12:03
To: user-java@ibatis.apache.org; lmeadors@apache.org
Subject: RE: converting iBATIS framework DAOs to the Spring Framework

Hi All,

 

I had a look at spring transactions and can get the annotation working for
the dao layer but not for the service layer?

 

This is the xml I added in the spring config file to enable spring
annotations (Java1.5+ required), 

<!-- TRANSACTIONS -->

    <bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <property name="dataSource">

            <ref bean="dataSource"/>

        </property>

    </bean>

    

    <bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCr
eator"/>

    

    <bean
class="org.springframework.transaction.interceptor.TransactionAttributeSourc
eAdvisor">

        <property name="transactionInterceptor" ref="txInterceptor"/>

    </bean>

    

    <bean id="txInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">

        <property name="transactionManager" ref="txManager"/>

        <property name="transactionAttributeSource">

            <bean
class="org.springframework.transaction.annotation.AnnotationTransactionAttri
buteSource"/>

        </property>

    </bean>

 

Step 2, Tested it on the dao layer by modifying updateAccount

@Transactional

public void updateAccount(Account account) {

    update("updateAccount", account);

    int i = 5/0;  //division by zero, expect rollback of account changes

    update("updateProfile", account);

 

 }

This works! But if I move the annotation to the service layer like this

@Transactional

public void updateAccount(Account account) {

     accountDao.updateAccount(account);

}

I get

org.springframework.aop.framework.AopConfigException: Cannot proxy target
class because CGLIB2 is not available. Add CGLIB to the class path or
specify proxy interfaces.

 

Kind Regards

 Meindert

 

-----Original Message-----
From: larry.meadors@gmail.com [mailto:larry.meadors@gmail.com] On Behalf Of
Larry Meadors
Sent: 12 April 2007 04:14 PM
To: user-java@ibatis.apache.org
Subject: Re: converting iBATIS framework DAOs to the Spring Framework

 

Thanks a TON for doing this, it's really appreciated!

 

Here is the WIKI Page: http://tinyurl.com/ysvjw5

 

I just converted it to WIKI format and corrected some simple stuff.

 

I was curious why if we are starting with jpetshop5, why not use the

hsqldb stuff that is there?

 

Also, I'd suggest that we use constructor injection instead of setter

injection - that way, we only need to remove the default constructors

from the service classes (eg., public AccountService() {...}, and the

related imports. It also prevents people from replacing the dao

instances (well, not entirely, but they have to do a bit more work).

 

Larry


RE: converting iBATIS framework DAOs to the Spring Framework

Posted by Poitras Christian <Ch...@ircm.qc.ca>.
Download CGLIB (at least version 2.1.3)
http://sourceforge.net/project/showfiles.php?group_id=56933
 
Christian


________________________________

From: Meindert [mailto:meindert@pastelebusiness.com] 
Sent: Friday, 13 April 2007 12:03
To: user-java@ibatis.apache.org; lmeadors@apache.org
Subject: RE: converting iBATIS framework DAOs to the Spring Framework



Hi All,

 

I had a look at spring transactions and can get the annotation working
for the dao layer but not for the service layer?

 

This is the xml I added in the spring config file to enable spring
annotations (Java1.5+ required), 

<!-- TRANSACTIONS -->

    <bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
>

        <property name="dataSource">

            <ref bean="dataSource"/>

        </property>

    </bean>

    

    <bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoPro
xyCreator"/>

    

    <bean
class="org.springframework.transaction.interceptor.TransactionAttributeS
ourceAdvisor">

        <property name="transactionInterceptor" ref="txInterceptor"/>

    </bean>

    

    <bean id="txInterceptor"
class="org.springframework.transaction.interceptor.TransactionIntercepto
r">

        <property name="transactionManager" ref="txManager"/>

        <property name="transactionAttributeSource">

            <bean
class="org.springframework.transaction.annotation.AnnotationTransactionA
ttributeSource"/>

        </property>

    </bean>

 

Step 2, Tested it on the dao layer by modifying updateAccount

@Transactional

public void updateAccount(Account account) {

    update("updateAccount", account);

    int i = 5/0;  //division by zero, expect rollback of account changes

    update("updateProfile", account);

 

 }

This works! But if I move the annotation to the service layer like this

@Transactional

public void updateAccount(Account account) {

     accountDao.updateAccount(account);

}

I get

org.springframework.aop.framework.AopConfigException: Cannot proxy
target class because CGLIB2 is not available. Add CGLIB to the class
path or specify proxy interfaces.

 

Kind Regards

 Meindert

 

-----Original Message-----
From: larry.meadors@gmail.com [mailto:larry.meadors@gmail.com] On Behalf
Of Larry Meadors
Sent: 12 April 2007 04:14 PM
To: user-java@ibatis.apache.org
Subject: Re: converting iBATIS framework DAOs to the Spring Framework

 

Thanks a TON for doing this, it's really appreciated!

 

Here is the WIKI Page: http://tinyurl.com/ysvjw5

 

I just converted it to WIKI format and corrected some simple stuff.

 

I was curious why if we are starting with jpetshop5, why not use the

hsqldb stuff that is there?

 

Also, I'd suggest that we use constructor injection instead of setter

injection - that way, we only need to remove the default constructors

from the service classes (eg., public AccountService() {...}, and the

related imports. It also prevents people from replacing the dao

instances (well, not entirely, but they have to do a bit more work).

 

Larry


RE: converting iBATIS framework DAOs to the Spring Framework

Posted by Meindert <me...@pastelebusiness.com>.
Hi All,

 

I had a look at spring transactions and can get the annotation working for
the dao layer but not for the service layer?

 

This is the xml I added in the spring config file to enable spring
annotations (Java1.5+ required), 

<!-- TRANSACTIONS -->

    <bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <property name="dataSource">

            <ref bean="dataSource"/>

        </property>

    </bean>

    

    <bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCr
eator"/>

    

    <bean
class="org.springframework.transaction.interceptor.TransactionAttributeSourc
eAdvisor">

        <property name="transactionInterceptor" ref="txInterceptor"/>

    </bean>

    

    <bean id="txInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">

        <property name="transactionManager" ref="txManager"/>

        <property name="transactionAttributeSource">

            <bean
class="org.springframework.transaction.annotation.AnnotationTransactionAttri
buteSource"/>

        </property>

    </bean>

 

Step 2, Tested it on the dao layer by modifying updateAccount

@Transactional

public void updateAccount(Account account) {

    update("updateAccount", account);

    int i = 5/0;  //division by zero, expect rollback of account changes

    update("updateProfile", account);

 

 }

This works! But if I move the annotation to the service layer like this

@Transactional

public void updateAccount(Account account) {

     accountDao.updateAccount(account);

}

I get

org.springframework.aop.framework.AopConfigException: Cannot proxy target
class because CGLIB2 is not available. Add CGLIB to the class path or
specify proxy interfaces.

 

Kind Regards

 Meindert

 

-----Original Message-----
From: larry.meadors@gmail.com [mailto:larry.meadors@gmail.com] On Behalf Of
Larry Meadors
Sent: 12 April 2007 04:14 PM
To: user-java@ibatis.apache.org
Subject: Re: converting iBATIS framework DAOs to the Spring Framework

 

Thanks a TON for doing this, it's really appreciated!

 

Here is the WIKI Page: http://tinyurl.com/ysvjw5

 

I just converted it to WIKI format and corrected some simple stuff.

 

I was curious why if we are starting with jpetshop5, why not use the

hsqldb stuff that is there?

 

Also, I'd suggest that we use constructor injection instead of setter

injection - that way, we only need to remove the default constructors

from the service classes (eg., public AccountService() {...}, and the

related imports. It also prevents people from replacing the dao

instances (well, not entirely, but they have to do a bit more work).

 

Larry


RE: converting iBATIS framework DAOs to the Spring Framework

Posted by Meindert <me...@pastelebusiness.com>.
Thanks Larry,

 

>Also, I'd suggest that we use constructor injection instead of setter

>injection - that way, we only need to remove the default constructors

>from the service classes (eg., public AccountService() {...}, and the

>related imports. 

 

I though I was doing this? It gave a minor error because the DaoManager is
part of the constructor for OrderService:

Com.ibatis.jpetstore.service

The DAO will be injected by spring, thus the constructor can be deleted

Example for AccountService, remove the public AccountService()methode

(also remove import com.ibatis.dao.client.DaoManager;)

 

 

>I was curious why if we are starting with jpetshop5, why not use the

>hsqldb stuff that is there?

Just kept is simple, was only interested into the conversion to spring. Also
didn't know where to put it :-)

 

Kind Regards

 Meindert

 

 

-----Original Message-----
From: larry.meadors@gmail.com [mailto:larry.meadors@gmail.com] On Behalf Of
Larry Meadors
Sent: 12 April 2007 04:14 PM
To: user-java@ibatis.apache.org
Subject: Re: converting iBATIS framework DAOs to the Spring Framework

 

Thanks a TON for doing this, it's really appreciated!

 

Here is the WIKI Page: http://tinyurl.com/ysvjw5

 

I just converted it to WIKI format and corrected some simple stuff.

 

I was curious why if we are starting with jpetshop5, why not use the

hsqldb stuff that is there?

 

Also, I'd suggest that we use constructor injection instead of setter

injection - that way, we only need to remove the default constructors

from the service classes (eg., public AccountService() {...}, and the

related imports. It also prevents people from replacing the dao

instances (well, not entirely, but they have to do a bit more work).

 

Larry


Re: converting iBATIS framework DAOs to the Spring Framework

Posted by Larry Meadors <lm...@apache.org>.
Thanks a TON for doing this, it's really appreciated!

Here is the WIKI Page: http://tinyurl.com/ysvjw5

I just converted it to WIKI format and corrected some simple stuff.

I was curious why if we are starting with jpetshop5, why not use the
hsqldb stuff that is there?

Also, I'd suggest that we use constructor injection instead of setter
injection - that way, we only need to remove the default constructors
from the service classes (eg., public AccountService() {...}, and the
related imports. It also prevents people from replacing the dao
instances (well, not entirely, but they have to do a bit more work).

Larry

Re: converting iBATIS framework DAOs to the Spring Framework

Posted by Larry Meadors <lm...@apache.org>.
You guys rock! :)

Thanks for taking this on, and getting it documented. It will be a
major help for both new and existing users.

Larry


On 4/13/07, Poitras Christian <Ch...@ircm.qc.ca> wrote:
> In fact, this was just a recommandation to cut Spring xml file size and
> management. Stripes is not required in any way.
> I only use 1 class in Stripes and only 2 of my classes have any link
> with this.
>
> I personnaly recommand to convert to Spring without Stripes first! Once
> it is done and it works, then you can check out if Stripes can reduce
> management of Spring config files.
>
> I'll add a supplement wiki page to indicate what are the advantages of
> using Stripes to reduce Spring config file.
>
> Christian
>
> -----Original Message-----
> From: Meindert [mailto:meindert@pastelebusiness.com]
> Sent: Friday, 13 April 2007 09:59
> To: user-java@ibatis.apache.org
> Subject: RE: converting iBATIS framework DAOs to the Spring Framework
>
> Another framework? How much work is it to convert an existing project
> (like jpetstore)?
>
> -----Original Message-----
> From: Poitras Christian [mailto:Christian.Poitras@ircm.qc.ca]
> Sent: 12 April 2007 03:57 PM
> To: user-java@ibatis.apache.org; lmeadors@apache.org
> Subject: RE: converting iBATIS framework DAOs to the Spring Framework
>
> I also suggest to use Stripes to cut the size of Spring config file.
>
> By adding @SpringBean annotation in front of properties (event private),
> Stripes will fill these properties by simulating the auto-wire process
> of Spring (even if no setter/getter/constructor exists).
>
> For instance, if I have a class like this
> /**
>  * Manage DataFile.
>  *
>  * @author poitrac
>  */
> public class DataFileManagement {
>     public DataFileManagement() {
>         super();
>     }
>     /**
>      * For DataFile management.
>      */
>     @SpringBean private DataFileDao dataFileDao;
>     /**
>      * Hides DataFiles.
>      * @param dataFileList DataFiles to hide from search requests.
>      */
>     @Transactional
>     public synchronized void hide(List<DataFile> dataFileList) {
>         dataFileDao.hide(dataFileList);
>     }
> }
>
> Then I have no special configuration to do in spring.xml beside
> declaring the bean.
> <bean id="dataFileManagement"
> class="ca.qc.ircm.management.DataFileManagement"/>
>
> It's possible to use the feature without using the web framework of
> Stripes. To do so, you juste need to add a PostProcessor.
> /**
>  * Injection bean dependencies based on Annotation.
>  *
>  * @author poitrac
>  */
> public class AnnotationInjector implements BeanPostProcessor,
> ApplicationContextAware, Ordered {
>     public AnnotationInjector() {
>         super();
>     }
>     /**
>      * ApplicationContext for bean injection.
>      */
>     private ApplicationContext applicationContext;
>     /* (non-Javadoc)
>      * @see
> org.springframework.beans.factory.config.BeanPostProcessor#postProcessBe
> foreInitialization(java.lang.Object, java.lang.String)
>      */
>     public Object postProcessBeforeInitialization(Object bean, String
> beanName) throws BeansException {
>         return bean;
>     }
>     /* (non-Javadoc)
>      * @see
> org.springframework.beans.factory.config.BeanPostProcessor#postProcessAf
> terInitialization(java.lang.Object, java.lang.String)
>      */
>     public Object postProcessAfterInitialization(Object bean, String
> beanName) throws BeansException {
>         SpringHelper.injectBeans(bean, applicationContext);
>         return bean;
>     }
>     /* (non-Javadoc)
>      * @see org.springframework.core.Ordered#getOrder()
>      */
>     public int getOrder() {
>         return Ordered.HIGHEST_PRECEDENCE;
>     }
>     /*
>      *  (non-Javadoc)
>      * @see
> org.springframework.context.ApplicationContextAware#setApplicationContex
> t(org.springframework.context.ApplicationContext)
>      */
>     public void setApplicationContext(ApplicationContext
> applicationContext) {
>         this.applicationContext = applicationContext;
>     }
> }
>
> Here I use HIGHEST_PRECEDENCE since Stripes requires to set properties
> (without setters) before it is enhanced by cglib.
>
> I prefer to just declare my beans in Spring and update my code and not
> the xml file (I always forget about updating xml anyway...).
>
>
> Christian
>
>
> -----Original Message-----
> From: larry.meadors@gmail.com [mailto:larry.meadors@gmail.com] On Behalf
> Of Larry Meadors
> Sent: Thursday, 12 April 2007 07:35
> To: user-java@ibatis.apache.org
> Subject: Re: converting iBATIS framework DAOs to the Spring Framework
>
> I'll add this to the wiki, and clean up anything I see in it that looks
> odd.
>
> Larry
>
>
> On 4/12/07, Meindert <me...@pastelebusiness.com> wrote:
> >
> >
> >
> > Hi all.
> >
> > I've documented the process of converting jpetshop5, and would like to
>
> > know if it is right?
> >
> > Also I don't know what to do with the
> > com.ibatis.dao.client.DaoException thrown in SequenceSqlMapDao?
> >
> > I'm going to add the transaction on OrderService now.
> >
> >
> > Steps to convert jpetshop5 to ibatis-spring
> >
> >
> >
> > The DAO iBATIS has been depreciated and it is recommended to replace
> > this with the spring framework. This document describes the steps
> > taken to convert from ibatis dao to spring dao. I work on the project
> > in netbeans so some terminology used might be incorrect for other
> > IDE's
> >
> >
> >
> > Prerequisite is to have a project similar to jpetstore5, and have it
> > connected to a 'normal' database
> >
> > (There is support for hssqldb in the DaoConfig class what we are going
>
> > to throw away.)
> >
> >
> > Lib jar files;
> >
> > Remove the old ibatis jar files from the class path and create the
> > reference to the new ibatis and spring jars
> >
> > Remove ibatis-common-2.jar
> > Remove ibatis-dao-2.jar
> > Remove ibatis-sqlmap-2.jar
> > Add ibatis-2.3.0.677.jar
> > Add spring.jar
> > Add commons-dbcp-1.2.1.jar
> > Add commons-pool-1.2.jar
> > Add commons-collections.jar
> >
> >
> > Com.ibatis.jpetstore.persistence.sqlmapdao
> >
> > Change the BaseSqlMapDao to;
> >
> > package com.ibatis.jpetstore.persistence.sqlmapdao;
> >
> > import org.springframework.orm.ibatis.SqlMapClientTemplate;
> >
> > public class BaseSqlMapDao extends SqlMapClientTemplate {
> >
> >     protected static final int PAGE_SIZE = 4;
> >
> > }
> >
> > All the other SqlMapDao files;
> >
> > Remove the public AccountSqlMapDao(DaoManager daoManager)  methode
> >
> > Remove import com.ibatis.dao.client.DaoManager;
> > Com.ibatis.jpetstore.service
> >
> > The DAO will be injected by spring, thus the constructor can be
> > deleted
> >
> > Example for AccountService, remove the public AccountService()methode
> >
> > (also remove import com.ibatis.dao.client.DaoManager;)
> >
> > There is a transaction in OrderService.java, this transaction must be
> > handled by Spring, remove it here for now. Also remove it out of the
> > constructor;
> >
> > public OrderService(ItemDao itemDao, OrderDao orderDao, SequenceDao
> > sequenceDao) {
> >
> >     this.itemDao = itemDao;
> >
> >     this.orderDao = orderDao;
> >
> >     this.sequenceDao = sequenceDao;
> >
> >   }
> >
> >
> > The Link between spring and ibatis
> >
> > Add the SpringInit.java class to a new package com.listeners
> >
> > This class will be instantiated on load of the website and will pass
> > the reference to the spring managed objects
> > Com.ibatis.jpetstore.presentation
> >
> >
> >
> > The presentation layer must connect with the spring managed service
> > layer
> >
> > So replace;
> >
> > public AccountBean() {
> >
> >     this(new AccountService(), new CatalogService());
> >
> >   }
> >
> > With
> >
> > public AccountBean() {
> >
> >      this((AccountService)
> > SpringInit.getApplicationContext().getBean("accountService"),
> >
> >           (CatalogService)
> > SpringInit.getApplicationContext().getBean("catalogService"));
> >
> >   }
> >
> > And add import com.listeners.SpringInit;
> >
> > Repeat for the other classes
> >
> >
> > Configuration
> >
> > First setup the listeners to have SpringInit initiated on loading of
> > the site
> >
> > Add to web.xml (after <description> tag)
> >
> > <context-param>
> >
> >         <param-name>contextConfigLocation</param-name>
> >
> >         <param-value>/WEB-INF/spring.xml</param-value>
> >
> > </context-param>
> >
> >
> >
> > <listener>
> >
> > <listener-class>org.springframework.web.context.ContextLoaderListener<
> > /listener-class>
> >
> > </listener>
> >
> >
> >
> > <listener>
> >
> >  <listener-class>com.listeners.SpringInit</listener-class>
> >
> > </listener>
> >
> >
> >
> > As you can see it is looking for /WEB-INF/spring.xml create this file.
> > Attached is the one I created for jpetstore5
> >
> >
> > Cleanup
> >
> > Throw away DaoConfig.java and dao.xml out of
> > com.ibatis.jpetstore.persistence, and the reference to it in the
> > Service classes
> >
> >
> >
>
>

RE: converting iBATIS framework DAOs to the Spring Framework

Posted by Poitras Christian <Ch...@ircm.qc.ca>.
In fact, this was just a recommandation to cut Spring xml file size and
management. Stripes is not required in any way.
I only use 1 class in Stripes and only 2 of my classes have any link
with this.

I personnaly recommand to convert to Spring without Stripes first! Once
it is done and it works, then you can check out if Stripes can reduce
management of Spring config files.

I'll add a supplement wiki page to indicate what are the advantages of
using Stripes to reduce Spring config file.

Christian

-----Original Message-----
From: Meindert [mailto:meindert@pastelebusiness.com] 
Sent: Friday, 13 April 2007 09:59
To: user-java@ibatis.apache.org
Subject: RE: converting iBATIS framework DAOs to the Spring Framework

Another framework? How much work is it to convert an existing project
(like jpetstore)?

-----Original Message-----
From: Poitras Christian [mailto:Christian.Poitras@ircm.qc.ca]
Sent: 12 April 2007 03:57 PM
To: user-java@ibatis.apache.org; lmeadors@apache.org
Subject: RE: converting iBATIS framework DAOs to the Spring Framework

I also suggest to use Stripes to cut the size of Spring config file.

By adding @SpringBean annotation in front of properties (event private),
Stripes will fill these properties by simulating the auto-wire process
of Spring (even if no setter/getter/constructor exists).

For instance, if I have a class like this
/**
 * Manage DataFile.
 *
 * @author poitrac
 */
public class DataFileManagement {
    public DataFileManagement() {
        super();
    }
    /**
     * For DataFile management.
     */
    @SpringBean private DataFileDao dataFileDao;
    /**
     * Hides DataFiles.
     * @param dataFileList DataFiles to hide from search requests.
     */
    @Transactional
    public synchronized void hide(List<DataFile> dataFileList) {
        dataFileDao.hide(dataFileList);
    }
}

Then I have no special configuration to do in spring.xml beside
declaring the bean.
<bean id="dataFileManagement"
class="ca.qc.ircm.management.DataFileManagement"/>

It's possible to use the feature without using the web framework of
Stripes. To do so, you juste need to add a PostProcessor.
/**
 * Injection bean dependencies based on Annotation.
 *
 * @author poitrac
 */
public class AnnotationInjector implements BeanPostProcessor,
ApplicationContextAware, Ordered {
    public AnnotationInjector() {
        super();
    }
    /**
     * ApplicationContext for bean injection.
     */
    private ApplicationContext applicationContext;
    /* (non-Javadoc)
     * @see
org.springframework.beans.factory.config.BeanPostProcessor#postProcessBe
foreInitialization(java.lang.Object, java.lang.String)
     */
    public Object postProcessBeforeInitialization(Object bean, String
beanName) throws BeansException {
        return bean;
    }
    /* (non-Javadoc)
     * @see
org.springframework.beans.factory.config.BeanPostProcessor#postProcessAf
terInitialization(java.lang.Object, java.lang.String)
     */
    public Object postProcessAfterInitialization(Object bean, String
beanName) throws BeansException {
        SpringHelper.injectBeans(bean, applicationContext);
        return bean;
    }
    /* (non-Javadoc)
     * @see org.springframework.core.Ordered#getOrder()
     */
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE;
    }
    /*
     *  (non-Javadoc)
     * @see
org.springframework.context.ApplicationContextAware#setApplicationContex
t(org.springframework.context.ApplicationContext)
     */
    public void setApplicationContext(ApplicationContext
applicationContext) {
        this.applicationContext = applicationContext;
    }
}

Here I use HIGHEST_PRECEDENCE since Stripes requires to set properties
(without setters) before it is enhanced by cglib.

I prefer to just declare my beans in Spring and update my code and not
the xml file (I always forget about updating xml anyway...).


Christian


-----Original Message-----
From: larry.meadors@gmail.com [mailto:larry.meadors@gmail.com] On Behalf
Of Larry Meadors
Sent: Thursday, 12 April 2007 07:35
To: user-java@ibatis.apache.org
Subject: Re: converting iBATIS framework DAOs to the Spring Framework

I'll add this to the wiki, and clean up anything I see in it that looks
odd.

Larry


On 4/12/07, Meindert <me...@pastelebusiness.com> wrote:
>
>
>
> Hi all.
>
> I've documented the process of converting jpetshop5, and would like to

> know if it is right?
>
> Also I don't know what to do with the
> com.ibatis.dao.client.DaoException thrown in SequenceSqlMapDao?
>
> I'm going to add the transaction on OrderService now.
>
>
> Steps to convert jpetshop5 to ibatis-spring
>
>
>
> The DAO iBATIS has been depreciated and it is recommended to replace 
> this with the spring framework. This document describes the steps 
> taken to convert from ibatis dao to spring dao. I work on the project 
> in netbeans so some terminology used might be incorrect for other 
> IDE's
>
>
>
> Prerequisite is to have a project similar to jpetstore5, and have it 
> connected to a 'normal' database
>
> (There is support for hssqldb in the DaoConfig class what we are going

> to throw away.)
>
>
> Lib jar files;
>
> Remove the old ibatis jar files from the class path and create the 
> reference to the new ibatis and spring jars
>
> Remove ibatis-common-2.jar
> Remove ibatis-dao-2.jar
> Remove ibatis-sqlmap-2.jar
> Add ibatis-2.3.0.677.jar
> Add spring.jar
> Add commons-dbcp-1.2.1.jar
> Add commons-pool-1.2.jar
> Add commons-collections.jar
>
>
> Com.ibatis.jpetstore.persistence.sqlmapdao
>
> Change the BaseSqlMapDao to;
>
> package com.ibatis.jpetstore.persistence.sqlmapdao;
>
> import org.springframework.orm.ibatis.SqlMapClientTemplate;
>
> public class BaseSqlMapDao extends SqlMapClientTemplate {
>
>     protected static final int PAGE_SIZE = 4;
>
> }
>
> All the other SqlMapDao files;
>
> Remove the public AccountSqlMapDao(DaoManager daoManager)  methode
>
> Remove import com.ibatis.dao.client.DaoManager; 
> Com.ibatis.jpetstore.service
>
> The DAO will be injected by spring, thus the constructor can be 
> deleted
>
> Example for AccountService, remove the public AccountService()methode
>
> (also remove import com.ibatis.dao.client.DaoManager;)
>
> There is a transaction in OrderService.java, this transaction must be 
> handled by Spring, remove it here for now. Also remove it out of the 
> constructor;
>
> public OrderService(ItemDao itemDao, OrderDao orderDao, SequenceDao
> sequenceDao) {
>
>     this.itemDao = itemDao;
>
>     this.orderDao = orderDao;
>
>     this.sequenceDao = sequenceDao;
>
>   }
>
>
> The Link between spring and ibatis
>
> Add the SpringInit.java class to a new package com.listeners
>
> This class will be instantiated on load of the website and will pass 
> the reference to the spring managed objects 
> Com.ibatis.jpetstore.presentation
>
>
>
> The presentation layer must connect with the spring managed service 
> layer
>
> So replace;
>
> public AccountBean() {
>
>     this(new AccountService(), new CatalogService());
>
>   }
>
> With
>
> public AccountBean() {
>
>      this((AccountService)
> SpringInit.getApplicationContext().getBean("accountService"),
>
>           (CatalogService)
> SpringInit.getApplicationContext().getBean("catalogService"));
>
>   }
>
> And add import com.listeners.SpringInit;
>
> Repeat for the other classes
>
>
> Configuration
>
> First setup the listeners to have SpringInit initiated on loading of 
> the site
>
> Add to web.xml (after <description> tag)
>
> <context-param>
>
>         <param-name>contextConfigLocation</param-name>
>
>         <param-value>/WEB-INF/spring.xml</param-value>
>
> </context-param>
>
>
>
> <listener>
>
> <listener-class>org.springframework.web.context.ContextLoaderListener<
> /listener-class>
>
> </listener>
>
>
>
> <listener>
>
>  <listener-class>com.listeners.SpringInit</listener-class>
>
> </listener>
>
>
>
> As you can see it is looking for /WEB-INF/spring.xml create this file.
> Attached is the one I created for jpetstore5
>
>
> Cleanup
>
> Throw away DaoConfig.java and dao.xml out of 
> com.ibatis.jpetstore.persistence, and the reference to it in the 
> Service classes
>
>
>


RE: converting iBATIS framework DAOs to the Spring Framework

Posted by Meindert <me...@pastelebusiness.com>.
Another framework? How much work is it to convert an existing project (like
jpetstore)?

-----Original Message-----
From: Poitras Christian [mailto:Christian.Poitras@ircm.qc.ca] 
Sent: 12 April 2007 03:57 PM
To: user-java@ibatis.apache.org; lmeadors@apache.org
Subject: RE: converting iBATIS framework DAOs to the Spring Framework

I also suggest to use Stripes to cut the size of Spring config file.

By adding @SpringBean annotation in front of properties (event private),
Stripes will fill these properties by simulating the auto-wire process
of Spring (even if no setter/getter/constructor exists).

For instance, if I have a class like this
/**
 * Manage DataFile.
 * 
 * @author poitrac
 */
public class DataFileManagement {
    public DataFileManagement() {
        super();
    }
    /**
     * For DataFile management.
     */
    @SpringBean private DataFileDao dataFileDao;
    /**
     * Hides DataFiles.
     * @param dataFileList DataFiles to hide from search requests.
     */
    @Transactional
    public synchronized void hide(List<DataFile> dataFileList) {
        dataFileDao.hide(dataFileList);
    }
}

Then I have no special configuration to do in spring.xml beside
declaring the bean.
<bean id="dataFileManagement"
class="ca.qc.ircm.management.DataFileManagement"/>

It's possible to use the feature without using the web framework of
Stripes. To do so, you juste need to add a PostProcessor.
/**
 * Injection bean dependencies based on Annotation.
 * 
 * @author poitrac
 */
public class AnnotationInjector implements BeanPostProcessor,
ApplicationContextAware, Ordered {
    public AnnotationInjector() {
        super();
    }
    /**
     * ApplicationContext for bean injection.
     */
    private ApplicationContext applicationContext;
    /* (non-Javadoc)
     * @see
org.springframework.beans.factory.config.BeanPostProcessor#postProcessBe
foreInitialization(java.lang.Object, java.lang.String)
     */
    public Object postProcessBeforeInitialization(Object bean, String
beanName) throws BeansException {
        return bean;
    }
    /* (non-Javadoc)
     * @see
org.springframework.beans.factory.config.BeanPostProcessor#postProcessAf
terInitialization(java.lang.Object, java.lang.String)
     */
    public Object postProcessAfterInitialization(Object bean, String
beanName) throws BeansException {
        SpringHelper.injectBeans(bean, applicationContext);
        return bean;
    }
    /* (non-Javadoc)
     * @see org.springframework.core.Ordered#getOrder()
     */
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE;
    }
    /*
     *  (non-Javadoc)
     * @see
org.springframework.context.ApplicationContextAware#setApplicationContex
t(org.springframework.context.ApplicationContext)
     */
    public void setApplicationContext(ApplicationContext
applicationContext) {
        this.applicationContext = applicationContext;
    }
}

Here I use HIGHEST_PRECEDENCE since Stripes requires to set properties
(without setters) before it is enhanced by cglib.

I prefer to just declare my beans in Spring and update my code and not
the xml file (I always forget about updating xml anyway...).


Christian


-----Original Message-----
From: larry.meadors@gmail.com [mailto:larry.meadors@gmail.com] On Behalf
Of Larry Meadors
Sent: Thursday, 12 April 2007 07:35
To: user-java@ibatis.apache.org
Subject: Re: converting iBATIS framework DAOs to the Spring Framework

I'll add this to the wiki, and clean up anything I see in it that looks
odd.

Larry


On 4/12/07, Meindert <me...@pastelebusiness.com> wrote:
>
>
>
> Hi all.
>
> I've documented the process of converting jpetshop5, and would like to

> know if it is right?
>
> Also I don't know what to do with the
> com.ibatis.dao.client.DaoException thrown in SequenceSqlMapDao?
>
> I'm going to add the transaction on OrderService now.
>
>
> Steps to convert jpetshop5 to ibatis-spring
>
>
>
> The DAO iBATIS has been depreciated and it is recommended to replace 
> this with the spring framework. This document describes the steps 
> taken to convert from ibatis dao to spring dao. I work on the project 
> in netbeans so some terminology used might be incorrect for other 
> IDE's
>
>
>
> Prerequisite is to have a project similar to jpetstore5, and have it 
> connected to a 'normal' database
>
> (There is support for hssqldb in the DaoConfig class what we are going

> to throw away.)
>
>
> Lib jar files;
>
> Remove the old ibatis jar files from the class path and create the 
> reference to the new ibatis and spring jars
>
> Remove ibatis-common-2.jar
> Remove ibatis-dao-2.jar
> Remove ibatis-sqlmap-2.jar
> Add ibatis-2.3.0.677.jar
> Add spring.jar
> Add commons-dbcp-1.2.1.jar
> Add commons-pool-1.2.jar
> Add commons-collections.jar
>
>
> Com.ibatis.jpetstore.persistence.sqlmapdao
>
> Change the BaseSqlMapDao to;
>
> package com.ibatis.jpetstore.persistence.sqlmapdao;
>
> import org.springframework.orm.ibatis.SqlMapClientTemplate;
>
> public class BaseSqlMapDao extends SqlMapClientTemplate {
>
>     protected static final int PAGE_SIZE = 4;
>
> }
>
> All the other SqlMapDao files;
>
> Remove the public AccountSqlMapDao(DaoManager daoManager)  methode
>
> Remove import com.ibatis.dao.client.DaoManager; 
> Com.ibatis.jpetstore.service
>
> The DAO will be injected by spring, thus the constructor can be 
> deleted
>
> Example for AccountService, remove the public AccountService()methode
>
> (also remove import com.ibatis.dao.client.DaoManager;)
>
> There is a transaction in OrderService.java, this transaction must be 
> handled by Spring, remove it here for now. Also remove it out of the 
> constructor;
>
> public OrderService(ItemDao itemDao, OrderDao orderDao, SequenceDao
> sequenceDao) {
>
>     this.itemDao = itemDao;
>
>     this.orderDao = orderDao;
>
>     this.sequenceDao = sequenceDao;
>
>   }
>
>
> The Link between spring and ibatis
>
> Add the SpringInit.java class to a new package com.listeners
>
> This class will be instantiated on load of the website and will pass 
> the reference to the spring managed objects 
> Com.ibatis.jpetstore.presentation
>
>
>
> The presentation layer must connect with the spring managed service 
> layer
>
> So replace;
>
> public AccountBean() {
>
>     this(new AccountService(), new CatalogService());
>
>   }
>
> With
>
> public AccountBean() {
>
>      this((AccountService)
> SpringInit.getApplicationContext().getBean("accountService"),
>
>           (CatalogService)
> SpringInit.getApplicationContext().getBean("catalogService"));
>
>   }
>
> And add import com.listeners.SpringInit;
>
> Repeat for the other classes
>
>
> Configuration
>
> First setup the listeners to have SpringInit initiated on loading of 
> the site
>
> Add to web.xml (after <description> tag)
>
> <context-param>
>
>         <param-name>contextConfigLocation</param-name>
>
>         <param-value>/WEB-INF/spring.xml</param-value>
>
> </context-param>
>
>
>
> <listener>
>
> <listener-class>org.springframework.web.context.ContextLoaderListener<
> /listener-class>
>
> </listener>
>
>
>
> <listener>
>
>  <listener-class>com.listeners.SpringInit</listener-class>
>
> </listener>
>
>
>
> As you can see it is looking for /WEB-INF/spring.xml create this file.
> Attached is the one I created for jpetstore5
>
>
> Cleanup
>
> Throw away DaoConfig.java and dao.xml out of 
> com.ibatis.jpetstore.persistence, and the reference to it in the 
> Service classes
>
>
>


RE: converting iBATIS framework DAOs to the Spring Framework

Posted by Poitras Christian <Ch...@ircm.qc.ca>.
I also suggest to use Stripes to cut the size of Spring config file.

By adding @SpringBean annotation in front of properties (event private),
Stripes will fill these properties by simulating the auto-wire process
of Spring (even if no setter/getter/constructor exists).

For instance, if I have a class like this
/**
 * Manage DataFile.
 * 
 * @author poitrac
 */
public class DataFileManagement {
    public DataFileManagement() {
        super();
    }
    /**
     * For DataFile management.
     */
    @SpringBean private DataFileDao dataFileDao;
    /**
     * Hides DataFiles.
     * @param dataFileList DataFiles to hide from search requests.
     */
    @Transactional
    public synchronized void hide(List<DataFile> dataFileList) {
        dataFileDao.hide(dataFileList);
    }
}

Then I have no special configuration to do in spring.xml beside
declaring the bean.
<bean id="dataFileManagement"
class="ca.qc.ircm.management.DataFileManagement"/>

It's possible to use the feature without using the web framework of
Stripes. To do so, you juste need to add a PostProcessor.
/**
 * Injection bean dependencies based on Annotation.
 * 
 * @author poitrac
 */
public class AnnotationInjector implements BeanPostProcessor,
ApplicationContextAware, Ordered {
    public AnnotationInjector() {
        super();
    }
    /**
     * ApplicationContext for bean injection.
     */
    private ApplicationContext applicationContext;
    /* (non-Javadoc)
     * @see
org.springframework.beans.factory.config.BeanPostProcessor#postProcessBe
foreInitialization(java.lang.Object, java.lang.String)
     */
    public Object postProcessBeforeInitialization(Object bean, String
beanName) throws BeansException {
        return bean;
    }
    /* (non-Javadoc)
     * @see
org.springframework.beans.factory.config.BeanPostProcessor#postProcessAf
terInitialization(java.lang.Object, java.lang.String)
     */
    public Object postProcessAfterInitialization(Object bean, String
beanName) throws BeansException {
        SpringHelper.injectBeans(bean, applicationContext);
        return bean;
    }
    /* (non-Javadoc)
     * @see org.springframework.core.Ordered#getOrder()
     */
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE;
    }
    /*
     *  (non-Javadoc)
     * @see
org.springframework.context.ApplicationContextAware#setApplicationContex
t(org.springframework.context.ApplicationContext)
     */
    public void setApplicationContext(ApplicationContext
applicationContext) {
        this.applicationContext = applicationContext;
    }
}

Here I use HIGHEST_PRECEDENCE since Stripes requires to set properties
(without setters) before it is enhanced by cglib.

I prefer to just declare my beans in Spring and update my code and not
the xml file (I always forget about updating xml anyway...).


Christian


-----Original Message-----
From: larry.meadors@gmail.com [mailto:larry.meadors@gmail.com] On Behalf
Of Larry Meadors
Sent: Thursday, 12 April 2007 07:35
To: user-java@ibatis.apache.org
Subject: Re: converting iBATIS framework DAOs to the Spring Framework

I'll add this to the wiki, and clean up anything I see in it that looks
odd.

Larry


On 4/12/07, Meindert <me...@pastelebusiness.com> wrote:
>
>
>
> Hi all.
>
> I've documented the process of converting jpetshop5, and would like to

> know if it is right?
>
> Also I don't know what to do with the
> com.ibatis.dao.client.DaoException thrown in SequenceSqlMapDao?
>
> I'm going to add the transaction on OrderService now.
>
>
> Steps to convert jpetshop5 to ibatis-spring
>
>
>
> The DAO iBATIS has been depreciated and it is recommended to replace 
> this with the spring framework. This document describes the steps 
> taken to convert from ibatis dao to spring dao. I work on the project 
> in netbeans so some terminology used might be incorrect for other 
> IDE's
>
>
>
> Prerequisite is to have a project similar to jpetstore5, and have it 
> connected to a 'normal' database
>
> (There is support for hssqldb in the DaoConfig class what we are going

> to throw away.)
>
>
> Lib jar files;
>
> Remove the old ibatis jar files from the class path and create the 
> reference to the new ibatis and spring jars
>
> Remove ibatis-common-2.jar
> Remove ibatis-dao-2.jar
> Remove ibatis-sqlmap-2.jar
> Add ibatis-2.3.0.677.jar
> Add spring.jar
> Add commons-dbcp-1.2.1.jar
> Add commons-pool-1.2.jar
> Add commons-collections.jar
>
>
> Com.ibatis.jpetstore.persistence.sqlmapdao
>
> Change the BaseSqlMapDao to;
>
> package com.ibatis.jpetstore.persistence.sqlmapdao;
>
> import org.springframework.orm.ibatis.SqlMapClientTemplate;
>
> public class BaseSqlMapDao extends SqlMapClientTemplate {
>
>     protected static final int PAGE_SIZE = 4;
>
> }
>
> All the other SqlMapDao files;
>
> Remove the public AccountSqlMapDao(DaoManager daoManager)  methode
>
> Remove import com.ibatis.dao.client.DaoManager; 
> Com.ibatis.jpetstore.service
>
> The DAO will be injected by spring, thus the constructor can be 
> deleted
>
> Example for AccountService, remove the public AccountService()methode
>
> (also remove import com.ibatis.dao.client.DaoManager;)
>
> There is a transaction in OrderService.java, this transaction must be 
> handled by Spring, remove it here for now. Also remove it out of the 
> constructor;
>
> public OrderService(ItemDao itemDao, OrderDao orderDao, SequenceDao
> sequenceDao) {
>
>     this.itemDao = itemDao;
>
>     this.orderDao = orderDao;
>
>     this.sequenceDao = sequenceDao;
>
>   }
>
>
> The Link between spring and ibatis
>
> Add the SpringInit.java class to a new package com.listeners
>
> This class will be instantiated on load of the website and will pass 
> the reference to the spring managed objects 
> Com.ibatis.jpetstore.presentation
>
>
>
> The presentation layer must connect with the spring managed service 
> layer
>
> So replace;
>
> public AccountBean() {
>
>     this(new AccountService(), new CatalogService());
>
>   }
>
> With
>
> public AccountBean() {
>
>      this((AccountService)
> SpringInit.getApplicationContext().getBean("accountService"),
>
>           (CatalogService)
> SpringInit.getApplicationContext().getBean("catalogService"));
>
>   }
>
> And add import com.listeners.SpringInit;
>
> Repeat for the other classes
>
>
> Configuration
>
> First setup the listeners to have SpringInit initiated on loading of 
> the site
>
> Add to web.xml (after <description> tag)
>
> <context-param>
>
>         <param-name>contextConfigLocation</param-name>
>
>         <param-value>/WEB-INF/spring.xml</param-value>
>
> </context-param>
>
>
>
> <listener>
>
> <listener-class>org.springframework.web.context.ContextLoaderListener<
> /listener-class>
>
> </listener>
>
>
>
> <listener>
>
>  <listener-class>com.listeners.SpringInit</listener-class>
>
> </listener>
>
>
>
> As you can see it is looking for /WEB-INF/spring.xml create this file.
> Attached is the one I created for jpetstore5
>
>
> Cleanup
>
> Throw away DaoConfig.java and dao.xml out of 
> com.ibatis.jpetstore.persistence, and the reference to it in the 
> Service classes
>
>
>

Re: converting iBATIS framework DAOs to the Spring Framework

Posted by Larry Meadors <lm...@apache.org>.
I'll add this to the wiki, and clean up anything I see in it that looks odd.

Larry


On 4/12/07, Meindert <me...@pastelebusiness.com> wrote:
>
>
>
> Hi all.
>
> I've documented the process of converting jpetshop5, and would like to know
> if it is right?
>
> Also I don't know what to do with the
> com.ibatis.dao.client.DaoException thrown in
> SequenceSqlMapDao?
>
> I'm going to add the transaction on OrderService now.
>
>
> Steps to convert jpetshop5 to ibatis-spring
>
>
>
> The DAO iBATIS has been depreciated and it is recommended to replace this
> with the spring framework. This document describes the steps taken to
> convert from ibatis dao to spring dao. I work on the project in netbeans so
> some terminology used might be incorrect for other IDE's
>
>
>
> Prerequisite is to have a project similar to jpetstore5, and have it
> connected to a 'normal' database
>
> (There is support for hssqldb in the DaoConfig class what we are going to
> throw away.)
>
>
> Lib jar files;
>
> Remove the old ibatis jar files from the class path and create the reference
> to the new ibatis and spring jars
>
> Remove ibatis-common-2.jar
> Remove ibatis-dao-2.jar
> Remove ibatis-sqlmap-2.jar
> Add ibatis-2.3.0.677.jar
> Add spring.jar
> Add commons-dbcp-1.2.1.jar
> Add commons-pool-1.2.jar
> Add commons-collections.jar
>
>
> Com.ibatis.jpetstore.persistence.sqlmapdao
>
> Change the BaseSqlMapDao to;
>
> package com.ibatis.jpetstore.persistence.sqlmapdao;
>
> import org.springframework.orm.ibatis.SqlMapClientTemplate;
>
> public class BaseSqlMapDao extends SqlMapClientTemplate {
>
>     protected static final int PAGE_SIZE = 4;
>
> }
>
> All the other SqlMapDao files;
>
> Remove the public AccountSqlMapDao(DaoManager daoManager)  methode
>
> Remove import com.ibatis.dao.client.DaoManager;
> Com.ibatis.jpetstore.service
>
> The DAO will be injected by spring, thus the constructor can be deleted
>
> Example for AccountService, remove the public AccountService()methode
>
> (also remove import com.ibatis.dao.client.DaoManager;)
>
> There is a transaction in OrderService.java, this transaction must be
> handled by Spring, remove it here for now. Also remove it out of the
> constructor;
>
> public OrderService(ItemDao itemDao, OrderDao orderDao, SequenceDao
> sequenceDao) {
>
>     this.itemDao = itemDao;
>
>     this.orderDao = orderDao;
>
>     this.sequenceDao = sequenceDao;
>
>   }
>
>
> The Link between spring and ibatis
>
> Add the SpringInit.java class to a new package com.listeners
>
> This class will be instantiated on load of the website and will pass the
> reference to the spring managed objects
> Com.ibatis.jpetstore.presentation
>
>
>
> The presentation layer must connect with the spring managed service layer
>
> So replace;
>
> public AccountBean() {
>
>     this(new AccountService(), new CatalogService());
>
>   }
>
> With
>
> public AccountBean() {
>
>      this((AccountService)
> SpringInit.getApplicationContext().getBean("accountService"),
>
>           (CatalogService)
> SpringInit.getApplicationContext().getBean("catalogService"));
>
>   }
>
> And add import com.listeners.SpringInit;
>
> Repeat for the other classes
>
>
> Configuration
>
> First setup the listeners to have SpringInit initiated on loading of the
> site
>
> Add to web.xml (after <description> tag)
>
> <context-param>
>
>         <param-name>contextConfigLocation</param-name>
>
>         <param-value>/WEB-INF/spring.xml</param-value>
>
> </context-param>
>
>
>
> <listener>
>
> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
>
> </listener>
>
>
>
> <listener>
>
>  <listener-class>com.listeners.SpringInit</listener-class>
>
> </listener>
>
>
>
> As you can see it is looking for /WEB-INF/spring.xml create this file.
> Attached is the one I created for jpetstore5
>
>
> Cleanup
>
> Throw away DaoConfig.java and dao.xml out of
> com.ibatis.jpetstore.persistence, and the reference to it
> in the Service classes
>
>
>