This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Spring and the fly likes Bean initialization in spring Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Bean initialization in spring" Watch "Bean initialization in spring" New topic
Author

Bean initialization in spring

Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 215

Hi all,
I am trying to understand the bean creation mechanism in spring.I have a doubt regarding the same.I want to know that when do we get the bean object (assuming the bean configuration is done in the xml file xxx-servlet.xml)?Does it happens as soon as we load the xml file from the path?or does it happens when we call a method from BeanFactory interface(or ApplicationContext interface)?

Thanks...
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

Assuming that the beans are eagerly loaded (the default) it will be created when the application context is initialized. The container will call interfaces on BeanFactory (and others) to create and initialize those beans as part of the initialization process.


[How To Ask Questions][Read before you PM me]
Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 215

Thanks for the reply.
But i still want to get one thing clear.What is really an application context?Who creates it?Do i have my beans created as soon as my xml files are loaded/read by container?

I would be grateful for the answers.
Thanks.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2273
    
  28

You probably want to pick up a book on Spring. It will go in much more detail. I reccommend Spring In Action. It explains how the application context is loaded and how beans are initialized and wired

To answer your question, if you have a standalone app that you want to use spring in, you have to load the application context from the main method yourself. So, in your main method you do something like this


This call starts the application context, loads the xml, parses the xml, creates the beans, initializes the beans before returning. Basically, the container and beans are loaded for you from the constructor

If you are writing a webapp using spring, generally, you add something like this in your web.xml



Basically, you declare a servlet of type org.springframework.web.servlet.DispatcherServlet to start on startup. This servlet will load the Application Context on startup. It will do exactly what your main method would do.
 
Don't get me started about those stupid light bulbs.
 
subject: Bean initialization in spring
 
Similar Threads
Why can stateless ejbCreate() access EJBObject???
What is dependency injection?
EJBHome & Bean
How we make entitybean as database independent
JavaMail Problem with WAS 3.5