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 Declarative Usage of Application Contexts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Declarative Usage of Application Contexts" Watch "Declarative Usage of Application Contexts" New topic
Author

Declarative Usage of Application Contexts

Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
Programmatic Usage of Application Contexts


ApplicationContext ctx= new ClassPathXmlApplicationContext("ch03/sample2/applicationContext.xml");

Once the bean factory or application context has been loaded, accessing beans in a basic fashion is as simple as using getBean() from the BeanFactory interface:

WeatherService ws = (WeatherService) ctx.getBean("weatherService");



Declarative Usage of Application Contexts as in (From: Professional Java Development with the Spring Framework)

Following IoC principles, as little application code as possible should know about the application context. We've mentioned that in spite of these principles, it is sometimes necessary for a small amount of glue code to know about the context. For a Spring-enabled J2EE web-app, it is possible to reduce or completely eliminate even this small amount of code by using Spring code to declaratively specify that an application context should be loaded at web-app startup.Spring's ContextLoader is the class that provides this capability.

At this time, containers known to work with the listener approach are Apache Tomcat 4.x+, Jetty 4.x+, Resin 2.1.8+, JBoss 3.0+, WebLogic 8.1 SP3+, and Orion 2.0.2+.
Containers known to not work with the listener approach, and which need the servlet approach, are BEA WebLogic up to 8.1 SP2, IBM WebSphere up to 5.x, and Oracle OC4J up to 9.0.x.

ContextLoaderListener is configured via a <listener> element in the standard J2EE web.xml file that is part of every web-app:

...
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-
class>
</listener>
...

Setting up ContextLoaderServlet instead is done via the <servlet> element in the web.xml file:

<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-
class>
<load-on-startup>1</load-on-startup>
</servlet>

Having done this declarative set-up, how can I access each individual bean?..i.e, doing something similar to getBean() method in programmatic approach.

Thank you,
Himalay


SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
You're supposed to use DI, not getBean. Try to use Spring Web MVC, you'll understand.


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
Thanks for looking into it Kengkaj.

I have followed This Link.
I understood that we inject dependencies using Spring Configuration File, but finally we need to use getbean.

i.e,


Please correct me, if you can point me to an example, that would be great.

-Himalay
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
No we don't need getBean, we should not use getBean.
ContextLoaderListener loads applicationContext, we can use DI.
Nandeesh Satishkumar
Greenhorn

Joined: Sep 30, 2009
Posts: 1
My understanding is...
In case of a web application one can use the contextLoaderListener to load the spring configuration files. I would prefer this approach as spring does the job for me of creating the Application context. So in this example below...

<!--------- In Web.xml ----------->


ContextLoaderListener will look for the param contextConfigLocation and load the configuration files used in the value, in this case applicationContext1. So if later I want to change the path of my applicationContext1.xml I can do it here with out any re-compilation of code required. So internally appContext is created by spring for me.

One has to get hands on to this context to extract the beans of the applicationContext1.xml. One way I think this can be acheived is the use of ApplicationContextAware interface. An example of this usage is given here http://jerlinworld.wordpress.com/2009/07/20/making-your-spring-application-context-aware/. Once access to the context is available one can created a Factory class and using the getBean method on the context beans can be extraceted from applicationContext1.xml.

The usage of getBean is the required glue code between spring and your application. DI is just a concept of injecting resources in to bean so that the bean is avaialble ready to use out of the box.


Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Nandeesh Satishkumar wrote:
The usage of getBean is the required glue code between spring and your application.

Not always true. In web applications, we should not use a single getBean. Using getBean will tie code to Spring Framework which should be avoid if possible.
If you use Spring Web MVC, you can specify Spring Web MVC controllers in *-servlet.xml and you can just use DI.
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
Consider this example from Professional Java Development with the Spring Framework



Do you mean I should not use getBean in the above case, can you demonstrate with an example?
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
You should try to use Spring Web MVC as I suggested, you'll immediately understand.

Using getBean in test code is fine. But we should not use a single getBean in web applications.
ContextLoaderListener will instantiate an application context, and application context will pre-instantiate singleton beans and inject dependencies to beans defined in bean definition. There is no need to use getBean.
Ben Narendren
Greenhorn

Joined: Oct 01, 2009
Posts: 19
I agree that we don't have a use for getBean in a regular scenario like if we are using Spring MVC. But there are situations where getBean can come handy. I remember doing a EJB facade over the spring layer and the Spring MVC interacted with this EJB facade. Here the EJB facade has no managed access to the business layer and it had to use getBean (using ApplicationContextAware) to access the spring beans.
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
I don't know what you mean by EJB facade over Spring, could you please to explain more?
Ben Narendren
Greenhorn

Joined: Oct 01, 2009
Posts: 19
Please take a look at this java world article on EJB facades over Spring Business services

http://www.javaworld.com/javaworld/jw-02-2005/jw-0214-springejb.html

This piece of code which you will find in the article is an example of where getBean() is called instead of DI

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Declarative Usage of Application Contexts
 
Similar Threads
load-on-startup problem ?
WHat is the application conext in java?
Spring + Struts [1 web app / 2 ApplicationContext]
Why WebServiceServlet is not able to load servlets in WLS 8.1?
Bean initialization in spring