File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes How to access a bean in a web application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "How to access a bean in a web application" Watch "How to access a bean in a web application" New topic
Author

How to access a bean in a web application

VenkataPrasad Regula
Ranch Hand

Joined: Jun 30, 2011
Posts: 63
Hi,
I am used to calling the application.xml from main class(public static void main). In any web application I used to load the data in the xxx-servlet.xml instead of accessing the bean. Now as i am developing an application, I am not getting where to declare the Application context to access the beans. If I am in the DAO layer I have to declare the "ApplicationContext ctx = new ClassPath....()" in the dao class to access the beans related to dao tasks. If I am in the business layer I have to declare the application context in each of the business classes to access the beans related to business tasks. If this is the case I have got to access the application context in almost all the classes which is Weird. Could someone please guide me how to proceed and guide me if there is any mistake in my approach towards dealing the issue. Thanks in advance.
Hitesh Singh
Greenhorn

Joined: Mar 04, 2010
Posts: 9
If you are following the MVC architecture "controller---service -- dao" then you have to implement Spring IOC concept where you need to implement setter injection ..

e.g:

<bean id = "service" class="Service">
<property name="" ref="dao"/>
</bean>

<bean id = "dao" class="Service">
<property name="sessionFactory"/>
</bean>

cheers
VenkataPrasad Regula
Ranch Hand

Joined: Jun 30, 2011
Posts: 63
I am sorry for confusing you all. What I exactly means is as below:

DAO Layer
If I have ten classes in dao layer and I need to access beans from the bean.xml, I need to declare and instantiate the application context in all the ten class to access the beans.

Business layer
SAme way in Business layer too. If I have 20 classes in business layer I need to declare the application context in all the 20 classes to access the concerned beans from.

My question is, is there a way where I can declare it at a place and access from all the other classes.
Craig Walls
author
Ranch Hand

Joined: Sep 19, 2003
Posts: 301
First, let me correct what Hitesh said: When you do DI in Spring, setter injection with XML configuration files is just *ONE OF MANY* ways. Spring has long supported constructor injection as well. And with recent versions of Spring (starting with 2.5) you can @Autowire inject into virtually anything (setter method, constructor argument, arbitrary method argument, private member variable, etc). And with even newer Spring versions (3.0 and 3.1) you can ditch the XML and use Java-based configuration instead of XML.

Now to the original question: If you're doing a web application layered as you describe, there's no need to load up the application context everytime you need it...in fact, that would most definitely be the *wrong* way to do it.

Spring's DispatcherServlet and ContextLoaderListener can load their application contexts *once* given a set of configuration files (either XML or Java-based configuration). You need never worry about actually loading the application context manually. The context will be loaded up, the beans created, and their dependencies injected.

Really, the only time you create and load your own application context is when writing a small application within a main() method. But knowing how to do that as a beginner helps you understand the basics of Spring. Once you advance past the basics, you'll rely less on those application context classes directly and just count on the framework (via DispatcherServlet and ContextLoaderListener) to load the context for you.


Spring in Action - Unleash POJO power in your applications!
Modular Java - Discover the secret weapon to modularity on the Java platform!
XDoclet in Action - Your complete guide to code generation with XDoclet.
VenkataPrasad Regula
Ranch Hand

Joined: Jun 30, 2011
Posts: 63
Thanks a lot Craig
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to access a bean in a web application