
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 {
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; }
}
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.