Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • depends-on attributes in spring

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 1.18k
    Comment on it

    dependsOn attribute for loading the depended beans referenced by another bean. dependsOn is a bean tag that can indicate bean depends on another bean .

    In this Example we can see when ApplicationContext Container is initialize first it will initialize Course Bean because we can put depends-on attribute ans give the dependency to the Student bean.

    Course.java

    package com.manish;
    
        public class Course {
        private String name;
    
        Course()
        {
            System.out.println("Course Bean Creation : Initializing Course Details...");
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
    }
    

    Student.java

    package com.manish;
    
    public class Student 
    {
        private int id;
        private String name;
    
    
        Student()
        {
            System.out.println("Student Bean");
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    
        }
    

    App.java

    package com.app;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.manish.Course;
    import com.manish.Student;
    
    public class App {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"app-context.xml"});
    
    
    
    
        }
    
    }
    

    app-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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    
    <bean id="student" class="com.manish.Student" depends-on="course">
        <property name="id" value="1"></property>
        <property name="name" value="manish"></property>
        <property name="course" ref="course"></property>
    </bean> 
    
    <bean id="course" class="com.manish.Course">
        <property name="name" value="mca"></property>
    </bean>
    
    
    
    </beans>
    

    Output:

    Course Bean Creation : Initializing Course Details...
    Student Bean
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: