Results 1 to 4 of 4

Thread: Preventing non-Singleton use outside of Spring

  1. #1

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    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.
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3

    Default

    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.

  4. #4
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    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...
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

Similar Threads

  1. Spring MVC Web Framework versus Struts
    By biguniverse in forum Web Flow
    Replies: 27
    Last Post: Aug 29th, 2012, 03:57 AM
  2. Replies: 4
    Last Post: Oct 5th, 2005, 11:04 AM
  3. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  4. DefaultAdvisorAutoProxyCreator skipping beans
    By youngm in forum Container
    Replies: 6
    Last Post: Apr 12th, 2005, 04:29 PM
  5. Replies: 14
    Last Post: Feb 21st, 2005, 05:41 PM

Posting Permissions

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