6 Replies Latest reply on Nov 9, 2010 11:00 AM by aldian00

    Migration from Spring to Seam

    aldian00

      Hello


      I am working on the prototype of a project. Originally, I worked with JSF and Richfaces / Spring / Hibernate, but it eventually appeared that Seam should be better than Spring for what I intend. My goal is to totally replace Spring by Seam, but I have issues translating my application-context.xml file into Seam configuration files and annotations.


      Actually, I am not very fan of annotations, I still feel that having all discribed in xml files is better, because in a few xml files I can see the whole configuration, but I will do my best using annotations.


      For example, I would like to know how to describe the folowing bean to seam:



      <bean id="sessionFactory"
                class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
                <property name="mappingResources">
                     <list>
                          <value>entity/Table_1.hbm.xml</value>
                          <value>entity/Table_2.hbm.xml</value>
                          <value>entity/Table_3.hbm.xml</value>
                          <value>entity/Table_4.hbm.xml</value>
                          <value>entity/Table_5.hbm.xml</value>
                          <value>entity/Table_6.hbm.xml</value>
                          <value>entity/Table_7.hbm.xml</value>
                          <value>entity/Table_8.hbm.xml</value>
                          <value>entity/Table_9.hbm.xml</value>
                          <value>entity/Table_10.hbm.xml</value>
                          <value>entity/Table_11.hbm.xml</value>
                          <value>entity/Table_12.hbm.xml</value>
                          <value>entity/Table_13.hbm.xml</value>
                          <value>entity/Table_14.hbm.xml</value>
                          <value>entity/Table_15.hbm.xml</value>
                          <value>entity/Table_16.hbm.xml</value>
                          <value>entity/Table_17.hbm.xml</value>
                          <value>entity/Table_18.hbm.xml</value>
                          <value>entity/Table_19.hbm.xml</value>
                          <value>entity/Table_20.hbm.xml</value>
                          <value>entity/Table_21.hbm.xml</value>
                     </list>
                </property>
      
                <property name="hibernateProperties">
                     <props>
                          <prop key="hibernate.dialect">
                               org.hibernate.dialect.MySQLDialect
                          </prop>
                          <prop key="hibernate.show_sql">true</prop>
                          <prop key="hibernate.cglib.use_reflection_optimizer">
                               true
                          </prop>
                     </props>
                </property>
                
                <property name="dataSource">
                     <ref bean="dataSource" />
                </property>
           </bean>



      Is there any page which explains how to do that? I presume I am not the first one to ask.. Thank you very much :)

        • 1. Re: Migration from Spring to Seam
          lvdberg

          Hi,


          have you seen the (FREE) downloadable Spring chapter of Seam In Action at Manning.com. It explains exactly what you want to do.


          Leo

          • 2. Re: Migration from Spring to Seam
            aldian00

            Leo van den Berg wrote on Nov 04, 2010 10:00:

            It explains exactly what you want to do.


            I am sorry, but it doesn't. It explains how to build an hybrid spring-seam application, when I want to totally remove Spring and completely replace it by Seam.


            BTW, I have been reading a lot since I posted there and almost found how to do : put all the hibernate stuff in the file hibernate.cfg.xml, and follow these directives: My Link.descriptor 


            I also used this explanation My Link.descriptor to try to translate my beans into component (replacing "bean id" by "component name" at all occurences, for example like this:



            <component name="databaseAccess" class="dao.DatabaseAccess">
                      <property name="sessionFactory">#{hibernateSessionFactory}</property>
                 </component>



            But the injection doesn't seam to work, I have lots of "java.lang.RuntimeException: Could not create Component" caused by "java.lang.IllegalArgumentException: type argument not a class"

            • 3. Re: Migration from Spring to Seam
              lvdberg

              Hi,


              I few days ago I answered a comparable answer. There is a vital thing you must remind about spring in Seam. When there is a dependency on the spring container (via init/ depends on etc) you must subclass the bean and add create and unwrap annotations. I don't know how complex your database acces is, but I would certainly replace it with the Seam managed manager, not only for its added value, but to prevent further problems.


              Leo

              • 4. Re: Migration from Spring to Seam
                aldian00

                I am sorry, but I don't think it can help me. As I said, I don't use Spring anymore. So there is no dependency. As for my class DatabaseAccess, it is nothing but a class which calls hibernate methods for the given sessionfactory, for example, here is one of its methods:




                     /* (non-Javadoc)
                      * @see dao.IDatabaseAcces#persist(entity.IEntity)
                      */
                     public void persist(IEntity transientInstance) throws RuntimeException{
                          sessionFactory.getCurrentSession().persist(transientInstance);
                     }



                It has no interest but allowing an easy mock.


                Now let's get down to business. What I am asking is very simple:
                In spring, we can for example have this in the file applicationContext.xml:


                <bean id="toto" class="dao.GenericEntityHome">
                     <property name="classtype">
                          <value>entity.Toto</value>
                     </property>
                </bean>



                It specifies that when a reference to the bean named toto is needed, an instance of the class dao.GenericEntityHome will be created, and the method setClasstype(entity.Toto) will be called for this instance.


                I took a look to the documentation of Seam and wrote the folowing in the file components.xml:


                <component name="toto" class="dao.GenericEntityHome">
                     <property name="classtype">
                          <value>entity.Toto</value>
                     </property>
                </component>



                I have tried a lot of variations, to the point I am almost running out of options and patience, but I always have the exception:


                java.lang.RuntimeException: Could not create Component: toto
                Caused by: java.lang.IllegalArgumentException: type argument not a class



                My one and only question : where I am wrong? As for the reason why I need to do this, I will explain it in another thread.



                • 5. Re: Migration from Spring to Seam
                  lvdberg

                  Hi,


                  I suspect that the argument you're providing is not properly transformed to the type you need. Looking at the error the property is of type Class and I think it is trying it as a String. You can put an additional type attribute on the property element to provide the exact type you want.


                  Leo

                  • 6. Re: Migration from Spring to Seam
                    aldian00

                    Thank you very much, all errors have vanished. But I don't know if the problem is really solved because I am confronted with a lot of Hibernate exceptions, so I still can't test if the whole thing really works