JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

DependsOn Attribute in Spring Configuration

May 3, 2014 by Krishna Srinivasan Leave a Comment

This example shows how to use the dependsOn attribute for loading the depended beans referenced by another bean. dependsOn is a attribute as part of the bean tag and it takes the comma separated bean names which are loaded before the actual bean is instantiated. Lets look at the syntax for using the dependsOn attribute.

[code lang=”xml”]
<bean id="initialize" class="javabeat.net.spring.core.Initialize" depends-on="country"/>
[/code]

In the above declaration, Initialize bean has the dependency of bean “country” which needs to be loaded. In practical, the initialize bean would want to use some of the resources or data in the Country bean which can be accessed only if that bean has been initialized.

This example shows a simple standalone spring application using dependsOn attribute for loading the beans.

1. Spring Beans

Initialize.java

[code lang=”java”]
package javabeat.net.spring.core;

public class Initialize {
public Initialize() {
System.out.println("Initialize Bean Creation : Initializing Values");
}
}
[/code]

Country.java

[code lang=”java”]
package javabeat.net.spring.core;

public class Country {
public Country() {
System.out.println("Country Bean Creation : Initializing Country Details…");
}
private String countryName;
private String countryCode;
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
}
[/code]

2. Spring Configurations

spring-application-context.xml

[code lang=”java”]
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:websocket="http://www.springframework.org/schema/websocket"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/websocket
http://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd">

<bean id="initialize" class="javabeat.net.spring.core.Initialize" depends-on="country"/>
<bean id="country" class="javabeat.net.spring.core.Country" />

</beans>
[/code]

3. DependsOn Attribute Application Example

SpringStandAloneExample.java

[code lang=”java”]
package javabeat.net.spring.core;

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

public class SpringStandAloneExample {

public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"javabeat\\net\\spring\\core\\spring-application-context.xml");

}
}
[/code]

4. Output

Output…

[code]
Country Bean Creation : Initializing Country Details…
Initialize Bean Creation : Initializing Values
[/code]

[wpdm_file id=83]

Filed Under: Spring Framework Tagged With: Spring MVC

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved