Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: How to specify/make a bean as immutable in beanfactory

Hybrid View

  1. #1
    Join Date
    Aug 2004
    Location
    Omaha, Nebraska
    Posts
    6

    Default How to specify/make a bean as immutable in beanfactory

    Hi,

    I am to newbie to spring, hence pardon for any dumb questions.

    I was going thru beanfactory xml and spring has a support for specifying whether a bean is singleton or prototype. I would like to know whether there is way I can say that particular bean is immutable (ex: <bean name="yetAnotherExample" class="examples.ExampleBeanTwo" immutable="true"/>and when I declare it as immutable, spring automatically uses '==' for equality (obj1 == obj2) rather than equals().
    Thanks,
    Jeelani

  2. #2
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    There's no immutable flag, but I'm confused as to when you think Spring should be comparing two instances of a bean one way vs. another (.equals vs. ==).

    Please add some more details, if you want to followi this up.

    Regards,
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  3. #3
    Join Date
    Aug 2004
    Location
    Germany, Magdeburg
    Posts
    279

    Default

    Are you puzzled about the term 'prototype'? By prototyping it is just said that everytime you call 'getBean' or refer to the bean a new instance is created according to the prototype specified by the <bean> tags.

    It is not the same like query by example or anything like that.

    Also you might refer to the Flyweight (/Lightweight) Pattern. By calling it immutable you might want to create only one instance for given property combination or something like that. This can be done by providing your own factory implementation. This is also fairly simple to do.

  4. #4
    Join Date
    Aug 2004
    Location
    Omaha, Nebraska
    Posts
    6

    Default about immutable

    Hi,

    Thanks for the reply. Since when we say that singleton="true" even though the framework takes care of creating only instance, we can't prevent the user apps from modifying the values. I was looking more at
    'Is there a way to tell spring that make mybean in this context immutable and singleton. If we do make the bean immutable programmatically, then I can't use the same bean in other context, where mutability is required.

    I was just curious to know that whether we can inject immutable property in particular contexts by allowing the framework to create some wrapper class over the bean to make it immutable.

    Thanks,
    Jeelani

  5. #5
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    Jeelani,

    You can create an Advisor that throws an exception whenever a method is invoked and apply it to your singleton bean setters methods. This will prevents "accidental" properties changees. I can see, however two issues with this scenario:
    1. you can not apply your Advice to public properties.
    2. you can not prevent changes to "Object" properties:
    Code:
      myBean.getSubBeans&#40;&#41;.clear&#40;&#41;;
    All in all, this will work only for "basic" beans. :cry:
    Omar Irbouh

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

  6. #6
    Join Date
    Aug 2004
    Location
    London, UK
    Posts
    339

    Default Re: about immutable

    Quote Originally Posted by sjeelani
    I was just curious to know that whether we can inject immutable property in particular contexts by allowing the framework to create some wrapper class over the bean to make it immutable.
    seems an odd requirement but you can still manage it programatically quite well if I understand correctly what you're trying to do and you want to avoid AOP.

    Code:
    public class MyBean &#123;
      private boolean immutable = false;
      private String propOne;
    
      public void setPropOne&#40;String propOne&#41; &#123;
        if &#40;immutable&#41; throw new IllegalAccessException&#40;"no way Jose"&#41;;
        this.propOne = propOne;
      &#125;
    
      public void makeImmutable&#40;&#41; &#123; immutable = true; &#125;
    &#125;
    Now in your bean definition, make the makeImmutable method an init method..

    Code:
    <bean id="myBean" class="MyBean" init-method="makeImmutable">
      <property name="propOne"><value>foo</value></property>
    </bean>
    Your bean will be made immutable after the bean factory has set the properties on it, but your bean class can still be used elsewhere for mutable instances while ever you don't call the setImmutable() method on it.
    Darren Davison.
    Public Key: 0xE855B3EA

  7. #7
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    public class MyBean {
    private boolean immutable = false;
    private String propOne;

    public void setPropOne(String propOne) {
    if (immutable) throw new IllegalAccessException("no way Jose");
    this.propOne = propOne;
    }

    public void makeImmutable() { immutable = true; }
    }
    this is a nice solution but it does not yet prevents users from calling
    Code:
      MyBean.getSomeObject&#40;&#41;.setSomeProperty&#40;someValue&#41;;
    and of course we need here a way to prevents accidental calls to
    Code:
      MyBean.makeImmutable&#40;&#41;;
    Omar Irbouh

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

  8. #8
    Join Date
    Aug 2004
    Location
    London, UK
    Posts
    339

    Default

    Quote Originally Posted by irbouho
    this is a nice solution but it does not yet prevents users from calling
    Code:
      MyBean.getSomeObject&#40;&#41;.setSomeProperty&#40;someValue&#41;;
    correct, it was a simple answer to an incomplete set of requirements. The main point being AIUI how to prevent properties being set after the bean factory creates the bean, but without preventing other (non Spring) apps from reusing the class and setting them. In your example above, the accessor can return a clone of someObject.
    Darren Davison.
    Public Key: 0xE855B3EA

  9. #9
    Join Date
    Aug 2004
    Location
    Slovakia
    Posts
    6

    Default Immutable wrapper

    Hi,

    maybe you were inspired by J2SE Collections.unmodifiebleSet etc. methods. Please notice that for each type of Collection there is a special method/wrapper respectively. A general wrapper can be made only using AOP. And not just the simple DynaProxy solution, but the full blown code manipulation (to disable an access to the public fields).

    You have one more possibility. Define your implementing class as real immutable (no direct setters, getters returning clones or immutable collections, ...)
    The bean definition can then use just constructor args.

  10. #10

    Default POJO problems

    The flexibility provided by Spring Framework is good. However, as I start introducing it into our project, my tech lead did ask me the following questions,

    If it's a "spring singleton" defined in Spring Context, how do you prevent the other developers from doing a "new" manually? :?:

    If it's not a "spring singleton”, again, how can you prevent people from just trying the "new" but not “getBean” from the framework? :?:

    I think a constructor injection is a necessary. We are also considering making a "non-spring" singleton to access the Spring context in order to prevent this from happening.

    Any other suggestion is all welcome.

Similar Threads

  1. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  2. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  3. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  4. could not satisfy dependencies
    By springuser in forum Container
    Replies: 4
    Last Post: Apr 26th, 2005, 01:15 PM
  5. Replies: 1
    Last Post: Apr 25th, 2005, 07:37 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
  •