Announcement Announcement Module
Collapse
No announcement yet.
Preventing non-Singleton use outside of Spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Preventing non-Singleton use outside of Spring

    Hi,

    I have a DAO which I proxy and set up with an Interceptor using Spring.

    However, there are no guarantees that Spring will be used everywhere the class is used.

    I would normally force the DAO to be a singleton by using a private constructor with a getInstance(). How can I set this up so that Spring can create it as a Singleton, and everyone else have to use the getInstance()?

    Cheers,

    David

  • #2
    You can configure a singleton class as a factory bean within Spring using the following:
    Code:
      <bean name="myDAO"
                factory-method="getInstance"
                class="com.company.MyDAO" />
    now back to your initial problem:
    I do not think refactoring your DAO into a singleton will solve your problem unless you apply your Interceptor by code in the getInstance method. Otherwise, calling MyDAO.getInstance() will return an instance that is not proxied by your Interceptor.

    Comment


    • #3
      Thanks. I think that will be fine just making it a singleton and setting it as a factory bean.

      If it's used outside of spring then the interceptor (handles transactions) won't be used.

      Comment


      • #4
        Spring 1.1 should be able to invoke your private constructor, so you can stick with the previous getInstance() approach. Though of course Spring will be creating a new instance, subverting the Singleton usage pattern getInstance() implies. This may bother you more than it bothers me...

        Comment

        Working...
        X