Monday, August 17, 2009

Spring-2 some learnings while reading a book

I was reading this book "Sping in Action" by Manning publishers to catch up with what's new in Spring 2.0. Agree, kinda late in doing it when Spring 3 is already in the market. But it's better to be late than never.
Personally, I feel that they've overloaded the bean creation with lots of convenience methods like these:
  • Autowiring: Use "byName", "byType", "constructor" and "autodetect" to autowire a bean by name and type, resp. Something like this:
<bean id="kenny"
class="com.springinaction.springidol.Instrumentalist"
autowire="byName">
<property name="song" value="Jingle Bells" />
</bean>
  • You can tell all the beans defined in the context file to autowire themselves with something like this:
<beans default-autowire="byName">
  • Autowiring, though it is convenient to use might lead to wrong wirings. So, I personally might resort to the old-fashioned manual wiring.
  • Bean Scoping: By default beans are created as Singletons. Therefore, spring provides the following "scope" options when you create a bean: singleton, prototype, request & session(only valid in spring mvc) and global-session(only valid if used in portlet context).
  • factory-method: If you already have a java implementation that returns a singleton, anyways. Then you can use it as a bean by declaring the method that spring might use to create an instance of the bean by using the factory-bean bean attribute.
  • Now, this is pampering the users to no end. You, now have a way to tell spring to run init and cleanup methods when a bean is created. This, you can do using the init-method and destroy-method bean attributes where you can mention the methods of that bean that must be executed for the resp. stages. If you have used the same named init and cleanup methods across all your beans then you can specify it by using the default-init-method and default-destroy-method beans attributes.
  • Parent-child concept in Spring! This is inheritance, the spring way. If you have a bean that will be created a multiple times in the context file, then you can use it like this:
<bean id="baseSaxophonist"
class="com.springinaction.springidol.Instrumentalist"
abstract="true">
<property name="instrument" ref="saxophone" />
<property name="song" value="Jingle Bells" />
</bean>

<bean id="kenny" parent="baseSaxophonist" />
<bean id="david" parent="baseSaxophonist" />
  • You can also abstract out the common properties into a parent-child relationship like this:
<bean id="basePerformer" abstract="true">
<property name="song" value="Somewhere Over the Rainbow" />
</bean>

<bean id="taylor" class="com.springinaction.springidol.Vocalist" parent="basePerformer" />

<bean id="stevie" class="com.springinaction.springidol.Instrumentalist" parent="basePerformer">
<property name="instrument" ref="guitar" />
</bean>
  • Method injection: Another sorcery provided by Spring-2 is method injection whereby, you can, during runtime, replace one method with another method. This is done in two ways:
  1. Method replacement: Here you would implement an interface called org.springframework.beans.factory.support.MethodReplacer and implement the method public class TigerReplacer implements MethodReplacer {
    public Object reimplement(Object target, Method method,
    Object[] args) throws Throwable;} Then you do something like this in the context file: <bean id="magicBox" class="com.springinaction.springidol.MagicBoxImpl">
    <replaced-method name="getContents" replacer="tigerReplacer" />
    </bean>

    <bean id="tigerReplacer" class="com.springinaction.springidol.TigerReplacer" />
  2. Getter injection: In this method you leave the getter method to be injected as abstract and then the lookup-method in the context file in the following way: <bean id="stevie"
    class="com.springinaction.springidol.Instrumentalist">
    <lookup-method name="getInstrument" bean="guitar" />
    <property name="song" value="Greensleeves" />
    </bean>

No comments: