Get all updates of Java4s
Follow Us: Email Newsletter

Example On Spring Dependency In The Form Of Objects

Spring » On Aug 7, 2011 { 34 Comments } By Sivateja

Let us see one example for previous concept, dependency in the form of objects with <ref /> element

files required..

i am taking complete pojo / poji model this time, And mates this is  ” very important and Exact concept of spring framework ” , make sure you understood this application from start to end point, if you are not able to understand you may not able to continue with the remaining concepts :-)

i will try to explain clearly in my own way….

  • spconfig1.xml
  • spconfig2.xml

Journey is the interface, and Traveler is the implemented class


is the interface, Car and Bus are the implemented classes

See Traveler Class is depends on Vehicle object






  • is our client application, i mean it contains our business logic
  • Journey, Vehicle are two interfaces in our application
  • No confusion, i have taken all the classes with some implementation, i will tell you the reason why… ?
  • Now come to the configuration files, i have been taken 2 configuration xml (for explaining local, parent, bean attribute concept :) )

Now see execution flow…………….

Actually we have 2 configuration files right, so in our client logic we need to define factory object two times [, line numbers, 12,13 – 15,16 ].  In factory1 object, i have been taken spconfig1.xml [, Line number 12 ], where Car,Bus classes been configured. factory2 knows factory1, as we included factory1 object in factory2 [ see line number 16, ]

Once we call id3 in, then spring container will move to spconfig2.xml and checks, there property v depends on other class right, and we given parent=id2, then control move to spconfig1.xml and finds id2 –> creates Bus class object, and will send back that Bus object to our client program

According to our program Bus object will be injected into the Traveler finally and there after we will get it into client application, and we can change id2 to id1 in the spconfig2.xml for getting Car object, with our recompilation ;)

Huuuu, mates hope you got what am saying, :-)

This is the complete POJO / POJI Model of spring, i will tell you why we need to take Interface for every class like above, you might have known with this example, but no problem i will rise this topic in the next sessions.



You Might Also Like

  ♦ About Author ♦

Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.


34 Responses to “Example On Spring Dependency In The Form Of Objects”
  1. vijay says:

    Your explanation is too good and as i am fresher it is so easy to learn from your site..

    But if you will provide previous and next option while reading every page for continuous reading then it will be great..

    Thanks a lot..

  2. Java4s says:


    Actually we thought this, but all of us doesn’t need all these topics right ? so we have given as index, some thing like..

  3. Pavan says:

    Sir when i run this i am getting this error,
    Exception in thread “main” org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [java4s.Travel] for bean with name ‘id3′ defined in class path resource [spconfig2.xml]; nested exception is java.lang.ClassNotFoundException: java4s.Travel

  4. Java4s says:


    Yeah sorry its my mistake, correct class name is Travel not Traveler(Which i have given, and corrected now) please check now it will works.

    Like this if you face any issues please download the application and test/compare in your local.

    Please let me know if you face any further issues ?

  5. Pavan says:

    Thank you sir…really good examples explained in each topics for freshers to remember each concepts..
    Sir,when wil be the JSP,servlet part complete…

  6. Anitha says:

    This site is very very useful and your explanation is so simple and intersting that helps to study further.
    Thanks a lot for your effort..
    Anitha Raj

  7. Java4s says:


    You welcome.

    Hmm i cannot tell you the exact time frame regarding to completion of JSP,Servlet, but might be in less than 2 months.

    We will send you an E-Mail update once we started to post, be in touch with our facebook/twitter / E-mail NewsLetters.

  8. Java4s says:


    Glad to hear that its helped you :-), please feel free to share our blog with your friends/colleagues.

  9. Pankaj Kalra says:


    This site is very helpful who is new in java framework.

    Very thanks for the same.

    I have to study about spring web-mvc module please provide the same ASAP or tell me another site for spring web-mvc.

    Pankaj Kalra

  10. Vidyasagar says:

    Nice piece of tutorial,
    thank you.

  11. vikram says:


    Your website is very easy to learn like anything it is just like reading newspaper as simple as that.Can you post the IBatis topics.


  12. Ashok says:

    your explanation is good but lot of spelling mistakes are there just verify once in each and every page.

  13. Anil Gupta says:

    your explanation is very good even example is very good…thanks

  14. SriDharan says:

    Very Good Explanantion. Understand core concepts in beginning stage…Good work dude.

  15. Geetha says:

    Really Good Explaination!! Its very helpful for beginners to learn and explore.. Thank You java4s.. Keep it up..

  16. Arun says:

    Wao nice tutorial.I got my dreamed website.

  17. mohan says:

    you r really great its to clean and perfect and a real fact is that your explained such a complicated thing into pretty simple one ..!! thanks a lot..!!!!

  18. Rupesh+Vislawath says:

    sir im getting error at j.startJourney(). it says identifier expected after this token

  19. paveen says:

    Above given example shows some error.
    Can you check it out plz.

  20. balu says:

    Really great very nice explanation

  21. asss says:

    khup mast explanations…

  22. sumit says:

    really ausome..

  23. Vidya says:

    Thank you Java4s for nice example, can you please help me how to achieve this using applicationcontext instead of beanfactory as i am using mainlt the applicationcontext in most of the examples:
    ApplicationContext ctx = new ClassPathXmlApplicationContext(“object.xml”);
    ApplicationContext ctx = new ClassPathXmlApplicationContext(“object1.xml”);

    object and object1.xml are my config files, if you can explain the differnce b/w beanfacorty and applicationcontext . thank you.

  24. Biplav says:

    Thanku Sir awesome explaination…..

    It will be very nice of you if you include some complex examples with MVC so that it will be more effective to learn realtime scenarios…………
    Thanks a lot

  25. suhas h says:

    Hi siva,
    Its a fabulous job.

  26. kunal chaudhari says:

    hello sir ,

    first of all vary thanks for this tutorial.

    i have one little confusion that what we can do if we want to Map TWO BEAN in single property Tag of another BEAN as i define below in my WAY don’t mind and please give me solution

  27. June says:

    Absolutely awesome and very helpful. you make the concepts so easy to follow!!!

  28. Priyanka Sharma says:

    Hi! When I am using ref parent then I get following exception:

    Feb 11, 2016 2:59:41 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [springconfig1.xml]
    Feb 11, 2016 2:59:41 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [springconfig2.xml]
    Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘id3′ defined in class path resource [springconfig2.xml]: Cannot resolve reference to bean ‘id2′ while setting bean property ‘v'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘id3′ defined in class path resource [springconfig2.xml]: Can’t resolve reference to bean ‘id2′ in parent factory: no parent factory available
    at Method)
    at java4s.ClientlogicObjectInjection.main(
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘id3′ defined in class path resource [springconfig2.xml]: Can’t resolve reference to bean ‘id2′ in parent factory: no parent factory available
    … 13 more

    But if I do all the configuration in single file and use ref as local then it works final. Please help in resolving the issue.

  29. kshirod says:

    when i am executing the client logic program , i am getting the “java4s.Travel cannot be cast to java4s.Journey” error. please help me out.Quick response will be appreciated.

  30. UnderScore says:

    Can you share your code. I just want to know why you are casting Travel class to Journey.
    you can contact me by sending mail to

    @Priyanka Sharma
    Make sure that you have setter method for property “v” in Travel class
    Both configuration files should be in specified directory.

    you can contact me by sending mail to

  31. vasu says:

    hi sir
    this is very usefull to me
    and i was getting very useful information of springs, but i need some more information with using jsp servlets aplication examples from your site,
    please provide examples what i need sir

  32. Raj says:


    I have a confusion!

    On the above code as you mention to call the Jurney Object (Interface) was right

    Object o = factory2.getBean(“id3″);
    Journey j = (Journey)o;

    the below code is right. Because the ID=”id3″ refere the Travel class only..,

    Object o = factory2.getBean(“id3″);
    Travel j = (Travel)o;

    please clarify me..,

  33. abhishek Kumar says:

    @ Vidya
    You can do it with ApplicationContext container also.
    Method 1st:
    ApplicationContext actx = new ClassPathXmlApplicationContext(“spconfig1.xml”);
    String[] config2 = {“spconfig2.xml”};
    ApplicationContext actx1 = new ClassPathXmlApplicationContext(config2 ,actx);
    Now you can get the bean on actx1.getBean(“id3″);

    Method 2nd:
    String[] XmlConfigFile = {“spconfig2.xml”,”spconfig1.xml” };
    ApplicationContext actx = new ClassPathXmlApplicationContext(XmlConfigFile);
    Now you can get the bean on actx.getBean(“id3″);

    But before going with method 2nd, we need to make a little bit change in “spconfig2.xml” file. Instead of writing we need to write .
    It will work.
    Abhishek Kumar

  34. Gopikumar S says:

    i am new to java , i need core java concepts from you


By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2017 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.