Java Login

About javalogin.com

Hello guys,
javalogin.com is for Java and J2EE developers, all examples are simple and easy to understand 

It is developed and maintained by Vaibhav Sharma. The views expressed on this website are his own and do not necessarily reflect the views of his former, current or future employers. I am professional Web development. I work for an IT company as Senior Consultant. Primary I write about spring, hibernate and web-services. I am trying to present here new technologies.


     << Previous
Next >>     


Spring Inner Bean

Java inner classes are defined within the scope of other classes, similarly, inner beans are beans that are defined within the scope of another bean.Spring IOC allows Inner Beans declaration.

We can declare a bean inside a beans. But, it has few restriction. Inner Beans are like annonymous beans where it is created and used on the fly. this beans cannot be used outside the enclosing beans. So, it is wise to avoid declaring the 'ID' or 'SCOPE' attributes for them. Because, these values will be ignored by the container. Only the enclosing beans can access them.

Spring provides a way of dependency injection that is sometime also known as dependency inversion. Spring works as a factory of beans that produces the beans instances. To access these created bean instances, we are required to have a bean identity. Spring provides an alias to access these beans. Whenever a bean is being used for a single place, it can be defined as a inner bean instead of defining a separate bean.

See a detail example to demonstrate the use of Spring inner bean.

Address.java

package dependencyInjection;

public class Address {
private String city;
private String state;

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String toString() {
return " City:: " + city + " State:: " + state;
}

}

Emp.java

package dependencyInjection;

public class Emp {
private String id;
private String name;
private Address address;

public Address getAddress() {
return address;
}

public void setAddress(Address address) {
this.address = address;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String toString() {
return "Id:: " + id + " Name:: " + name + " " + address;
}

}

context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" >

<bean id="emp" class="dependencyInjection.Emp">
<property name="id" value="1"></property>
<property name="name" value="ABC"></property>
<property name="address">
<bean class="dependencyInjection.Address">
<property name="city" value="Noida"></property>
<property name="state" value="Uttar Pradesh"></property>
</bean>
</property>

</bean>

<alias name="emp" alias="emp_alias" />


In above context.xml we have mentioned one emp class entry and then create inner bean Address.java in property tag address. In above file Spring container find the emp class entry then assign address class as per inner bean in Emp class.

ReferenceMain.java

package dependencyInjection;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ReferenceMain {

public static void main(String[] args) {
BeanFactory bean = new ClassPathXmlApplicationContext(
"dependencyInjection/context.xml");
Emp emp = (Emp)bean.getBean("emp_alias");
System.out.println(emp);
}

}

In above example We have mention alias of bean (emp_alias) which is mentioned in context.xml. When you run this program Spring container find the bean entry from the context.xml as per emp_alias and create object for emp class.


     << Previous
Next >>