With this example we will show you how depends-on works in Spring.
In spring application XML, if we need to initialize any bean before another bean, depends-on does this job. While creating bean we need to define depends-on attribute in bean.
When a bean is typically dependent on another bean then we specify the dependent bean using <ref/> element in XML-based configuration metadata. However, sometimes dependencies between beans are less direct; for example, a static initializer that initializes something like loading of database driver in a class needs to be triggered first. The depends-on attribute can explicitly force one or more beans to be initialized before the bean using this element is initialized.

If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse.
Prerequisites

The following things are required in order to run the application

Eclipse Kepler
JDK 1.8
Have maven 3 installed and configured
Spring dependencies in pom.xml

Now we will see the below steps how to create a maven based spring project in Eclipse

Step 1. Create a standalone maven project in Eclipse

Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.

maven-arctype-quickstart

Now enter the required fields (Group Id, Artifact Id) as shown below

Group Id : com.roytuts
Artifact Id : spring-depends-on

Step 2. Modify the pom.xml file as shown below.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.roytuts</groupId>
    <artifactId>spring-depends-on</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-depends-on</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jdk.version>1.8</jdk.version>
        <junit.version>4.11</junit.version>
        <spring.version>4.1.5.RELEASE</spring.version>
    </properties>

    <dependencies>
        <!-- Spring framework -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Step 3. If you see JRE System Library[J2SE-1.4] then change the version by below process

Do right-click on the project and go to Build -> Configure build path, under Libraries tab click on JRE System Library[J2SE-1.4], click on Edit button and select the appropriate jdk 1.8 from the next window. Click on Finish then Ok.

Step 4. Create src/main/resources folder for putting the resource files.

Do right-click on the project and go New -> Source Folder. Give Folder name: as src/main/resources and click on Finish button.

Step 5. Create an XML properties file under src/main/resources.

Do right-click on src/main/resources in the project and go New -> file. Give File name: as applicationContext.xml and click on Finish button.

<?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">

    <!-- A bean depends on single bean -->
    <!-- <bean id="beanA" class="com.roytutc.spring.dependson.BeanA"
        depends-on="beanB" />
    <bean id="beanB" class="com.roytutc.spring.dependson.BeanB" /> -->

    <!-- A bean depends on multiple beans -->
    <bean id="beanA" class="com.roytutc.spring.dependson.BeanA" depends-on="beanB, beanC">
        <property name="beanB" ref="beanB" />
    </bean>

    <bean id="beanB" class="com.roytutc.spring.dependson.BeanB" />
    <bean id="beanC" class="com.roytutc.spring.dependson.BeanC" />

</beans>

Step 6. Create a POJO classes

package com.roytutc.spring.dependson;

public class BeanA {

    private BeanB beanB;

    public BeanA() {
        System.out.println("Initializing BeanA");
    }

    public BeanB getBeanB() {
        return beanB;
    }

    public void setBeanB(BeanB beanB) {
        this.beanB = beanB;
    }

}
package com.roytutc.spring.dependson;

public class BeanB {

    private String name;

    public BeanB() {
        System.out.println("BeanB is initialized.");
    }

    public String getName() {
        return name;
    }

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

}
package com.roytutc.spring.dependson;

public class BeanC {

    public BeanC() {
        System.out.println("BeanC is initialized.");
    }

}

Step 7. Create a Test class which will test our code how it works

package com.roytutc.spring.dependson;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BeanATest {

    private BeanA beanA;
    private BeanB beanB;

    @Before
    public void setUp() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                "dependsOn.xml");
        beanA = applicationContext.getBean("beanA", BeanA.class);
        beanB = applicationContext.getBean("beanB", BeanB.class);
    }

    @Test
    public void testGetName() {
        beanA.getBeanB().setName("BeanB");
        System.out.println(beanB.getName());
    }

}

Step 8. Run the above test class, you will get below output

BeanB is initialized.
BeanC is initialized.
Initializing BeanA
BeanB

That’s all. Thanks for reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit | Email Me

Leave a Reply

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