Spring IOC features

1. Setting fields in a Bean
Spring Framework can initialize class fields. Lets take this Class example:
Then we will set the class fields by “Setter Injection”

public class ExampleBean{

 private ObjectField object;
 private int i;

 public void setObject(ObjectField object) {
  this.object = object;

 public void setIntegerProperty(int i) {
  this.i = i;

Then we will use Spring to set values in the fields ‘object’ and ‘i’

Here is the Spring configuration:

<bean id="exampleBean" class="com.examples.ExampleBean">
<property name="object"><ref bean="ObjectField"/></property>
<property name="integerProperty"><value>1</value></property>

<bean id="ObjectField" class="com.examples.ObjectField"/>

In this Spring configuration “object” will take the class defined in
“com.example.ObjectField”, as set in the configuration. Spring will
set the value of this field on start-up, thus it can already be used.

As for primitive types, such as the “i” field, then we can just a
value directly.

Aside from setter injection, we can also do constructor injection. Use either which suits a better purpose. Constructor injection is better for one-time, start-up initialization, while setter injection is better for in-process value injection.

Here is an example of a constructor Spring configuration. Just make a class that has a constructor that corresponds to it.

<bean id="exampleBean" class="examples.ExampleBean"
  <constructor-arg ref="anotherExampleBean"/>
  <constructor-arg ref="yetAnotherBean"/>
  <constructor-arg value="1"/> 

2. Setting inner beans

We can set properties on a an object property of a Bean. Yes properties inside a property. Here is the Spring example:

<bean id="outer" class="...">
  <!-- instead of using a reference to a target bean, simply define the target bean inline -->
  <property name="target">
    <bean class="com.example.Person"> <!-- this is the inner bean -->
      <property name="name" value="Fiona Apple"/>
      <property name="age" value="25"/>

Note that inner beans are “Prototypes”. It is described under Scopes ( Singleton and Prototype ). In this case, there will be two classes to be created. One as the outer Class, and the other as the inner Class.

3. Lazy instantiated Beans

Set a Singleton Bean not to be set in start-up. Just add the lazy-init=”true” property. This bean will only be instantiated when it is first requested. Comes in handy for Classes that are only needed in some situational process where we can save Memory and CPU. See the example below:

<bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true"/>

<bean name="not.lazy" class="com.foo.AnotherBean"/>

4. Depends-on

Use the “Depends-on” to initialize a dependent bean, before this bean can be initialized. Usually we can use the “ref” keyword, but in this case, this is for the indirect dependency such as connectivity and other 3rd party initialization as base. See the examples below; also include multiple depends, which is separated by a comma:

<bean id="beanOne" class="ExampleBean" depends-on="manager"/>
<bean id="beanOne" class="ExampleBean" depends-on="manager,accountDao">

manager and accountDao are Classes.

5. Scopes: singleton and prototype

To put it simply,

Singleton – Quite different from the Design Pattern, this means that when a Bean is instantiated, It is the DEFAULT that Spring only uses one instance of it even though it is repeatedly referenced in the configuration. Referenced is using the “ref=” keyword.

Prototype- If a bean is instantiated with a “scope=prototype” keyword,  each “ref=” will have a new instance of the Class. An Opposite to the Singleton scope.


See more references here:

basics of  beans


more details on Spring IOC features



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s