Sunday, October 28, 2012

How can you configure sets, lists, maps and properties in Bean configuration file?



Properties:

<props>

<prop key="dbname">univiersitydb </prop>
<prop key="port">12345</prop>
<prop key="username">suresh</prop>
 <prop key="password">suresh123</prop>
 </props>

Ex:

<bean id="hank" class="com.springinaction.springidol.OneManBand">
<property name="instruments">
<props>
<prop key="GUITAR">STRUM STRUM STRUM</prop>
<prop key="CYMBAL">CRASH CRASH CRASH</prop>
<prop key="HARMONICA">HUM HUM HUM</prop>
</props>
</property>
</bean>

LIST

<bean id="hank" class="com.springinaction.springidol.OneManBand">
<property name="instruments">
<list>
<ref bean="guitar" />
<ref bean="cymbal" />
<ref bean="harmonica" />
</list>
</property>
</bean>

The <list> element contains one or more values. Here <ref> elements are used
to define the values as references to other beans in the Spring context. However, it’s also
possible to use other value-setting Spring elements as the members of a <list>, including
<value>, <bean>, and <null/>. In fact, a <list> may contain another <list> as a
member for multidimensional lists.

SET

<bean id="hank" class="com.springinaction.springidol.OneManBand">
<property name="instruments">
<set>
<ref bean="guitar" />
<ref bean="cymbal" />
<ref bean="harmonica" />
<ref bean="harmonica" />
</set>
</property>
</bean>

MAP


<bean id="hank" class="com.springinaction.springidol.OneManBand">
<property name="instruments">
<map>
<entry key="GUITAR" value-ref="guitar" />
<entry key="CYMBAL" value-ref="cymbal" />
<entry key="HARMONICA" value-ref="harmonica" />
</map>
</property>
</bean>




what is the default scope of bean in spring?

the two spring bean scopes are

1. singleton

2. prototype

The default scope is Singleton

Prototype scope creates a  new instance every time a call is made to fetch the bean. to achieve this we need to set singleton property to false

<bean name="com.suresh.MyBean" singleton="false" />


what is the difference between Spring Signleton and Java Singleton?

Spring singleton is a singleton for Context or container

Java singleton is per process and per class loader


what are method hooks / callback methods in bean creation?

Spring Framework provides a couple of hooks in the form of callback methods.these methods provide opportunity for the bean to initialize properties or clean up resources

there are two method hooks :

init-method

destroy-method


<bean name="oracleDAO" class="com.suresh.OracleDAO" init-method="initializePool"/>

The similar functionality can  be  achieved using special interfaces

> InitializeingBean     -> method: afterPropertiesSet

> DisposableBean    -> method: destory


Friday, October 12, 2012

What is difference between IOC and DI?

A better solution to resource retrieval is to apply IoC. The idea of this principle is to invert the
direction of resource retrieval. In a traditional lookup, components seek resources by making
requests to a container, and the container duly returns the resources in question.With IoC, the
container itself actively delivers resources to itsmanaged components. A component has only
to choose a way of accepting the resources. This could be described as a passive formof
lookup.

IoC is a general principle, while DI is a concrete design pattern that embodies this principle.
In the DI pattern, a container is responsible for injecting appropriate resources into each
component in some predetermined way, such as via a setter method.


The IoC principle resembles Hollywood’s infamous catchphrase, “Don’t call us, we’ll call
you,”
so it is sometimes called the “Hollywood principle.”Moreover, as DI is themost typical
implementation of IoC, the terms IoC and DI are often used interchangeably.