Results 1 to 6 of 6

Thread: Can we write static classes in spring

  1. #1

    Default 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. #2
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

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

  3. #3
    Join Date
    Jul 2008
    Location
    Columbus, OH
    Posts
    43

    Default

    Quote 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
    Thanks,
    Frank Lamantia

  4. #4

    Default

    Thanks Frank for the inputs.

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

  5. #5
    Join Date
    Jul 2008
    Location
    Columbus, OH
    Posts
    43

    Default

    Quote 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.
    Thanks,
    Frank Lamantia

  6. #6

    Default

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •