Monday 27 March 2017

Spring: depends-on example

In any non-trivial application, beans has to communicate each other to perform certain action. For example, serivce layer bean dependes on dao layer bean, which in turn depends on session bean.

If a bean depends on other bean, we must ensure that, all the dependency beans are initialized properly before initializing this bean.
 <bean id="serviceBean" class="com.sample.pojo.ServiceBean" depends-on="daoBean" />

 <bean id="daoBean" class="com.sample.pojo.DaoBean" depends-on="sessionBean" />

 <bean id="sessionBean" class="com.sample.pojo.SessionBean" />


Notify above statements, serviceBean depends on daoBean, and daoBean depends on sessionBean. So sessionBean is initialized before initializing daoBean, daoBean initialized before initializing sessionbean.

Following is the complete working application.


SessionBean.java
package com.sample.pojo;

public class SessionBean {

 public SessionBean(){
  System.out.println("Session Bean initialized successfully");
 }
}

DaoBean.java
package com.sample.pojo;

public class DaoBean {

 public DaoBean(){
  System.out.println("DaoBean initialized successfully");
 }
}

ServiceBean.java
package com.sample.pojo;

public class ServiceBean {
 
 public ServiceBean(){
  System.out.println("ServiceBean initialized successfully");
 }
}

myConfiguration.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"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

 <bean id="serviceBean" class="com.sample.pojo.ServiceBean"
  depends-on="daoBean" />

 <bean id="daoBean" class="com.sample.pojo.DaoBean" depends-on="sessionBean" />

 <bean id="sessionBean" class="com.sample.pojo.SessionBean" />

</beans>

HelloWorld.java
package com.sample.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sample.pojo.ServiceBean;

public class HelloWorld {
 public static void main(String args[]) {
  ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "myConfiguration.xml" });

  ServiceBean bean = context.getBean("serviceBean", ServiceBean.class);

  ((ClassPathXmlApplicationContext) context).close();
 }
}

Run ‘HelloWorld.java’, you can able to see following output.
Session Bean initialized successfully
DaoBean initialized successfully
ServiceBean initialized successfully

Specifying multiple dependencies using ‘depends-on’ attribute

To express a dependency on multiple beans, supply a list of bean names as the value of the depends-on attribute, with commas, whitespace and semicolons, used as valid delimiters.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

 <bean id="serviceBean" class="com.sample.pojo.ServiceBean"
  depends-on="daoBean, sessionBean" />

 <bean id="daoBean" class="com.sample.pojo.DaoBean" />

 <bean id="sessionBean" class="com.sample.pojo.SessionBean" />

</beans>


Notify above snippet, two dependencys are specified for serviceBean.




Previous                                                 Next                                                 Home

No comments:

Post a Comment