Announcement Announcement Module
Collapse
No announcement yet.
Can we write static classes in spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can we write static classes in spring

    Hi

    I am using Spring 2.5 with Webflow 2.0.3 and Hibernate 3.x

    For the Hibernate session factory, I have written a HibernateUtils static class that returns SessionFactory.

    Can we write static classes in spring?

    am I in the right direction?

    Appreciate inputs.

    Thanks

  • #2
    It is not clear what do you mean under 'write static classes in spring'

    Comment


    • #3
      Originally posted by skadiy000 View Post
      Hi

      I am using Spring 2.5 with Webflow 2.0.3 and Hibernate 3.x

      For the Hibernate session factory, I have written a HibernateUtils static class that returns SessionFactory.

      Can we write static classes in spring?

      am I in the right direction?

      Appreciate inputs.

      Thanks
      If I remember correctly, the Hibernate tutorial demonstrates the use of a "HibernateUtil" class that has a static method to fetch the Session Factory.

      This is how you could declare your Session Factory (using this HibernateUtil that is discussed in the Hibernate tutorial) in your spring beans document:

      Code:
      <beans>
      	...
      	<bean id="sessionFactory" class="util.HibernateUtil" factory-method="getSessionFactory" />
      	...
      </beans>
      Using a static factory method is discussed here: http://static.springframework.org/sp...factory-method

      Comment


      • #4
        Thanks Frank for the inputs.

        With Bean injection, can the HibernateUtil class still be a static class?

        Comment


        • #5
          Originally posted by skadiy000 View Post
          Thanks Frank for the inputs.

          With Bean injection, can the HibernateUtil class still be a static class?
          I'm not sure what you mean by "static class"... but you can reference any object that is created via the result of a 'static factory method' in your Spring beans document.

          The XML above will tell Spring to invoke the static 'getSessionFactory' method on the 'HibernateUtil' class. Spring will not create an instance of the 'HibernateUtil' class in this scenario.

          Comment


          • #6
            Thanks Frank for the clarification.

            I am successful in creating the Bean for the HibernateUtil static class that is creating the static Hibernate SessionFactory object during context initialization and doing the injection in to my service class.

            I was under the wrong impression that we should not write user defined static classes or static methods while using the Spring Framework.

            Nice clarification. Thanks a lot

            Comment

            Working...
            X