This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Spring and the fly likes Help needed to set up Spring IOC in a web application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Help needed to set up Spring IOC in a web application" Watch "Help needed to set up Spring IOC in a web application" New topic
Author

Help needed to set up Spring IOC in a web application

Kjeld Sigtermans
Ranch Hand

Joined: Aug 10, 2006
Posts: 122
Hi there.

I am kind of new to setting up Spring and I'd like to use Spring IOC to retrieve an implementation of a class.
The interface is Facade and the implementing class is FacadeImpl. Normally you would do Facade facade = new FacadeImpl() obviously.

My spring config file is called spring-config.xml, it's in my project's WEB-INF folder and it looks like this:

I have already figured out how to get an implementation using Spring IOC in a stand-alone JUnit test class, which is also in the web-project.
I used this code, which works fine:

My big question is, how to retrieve an instance of this facade in the code that is running in a container. I know I have to use something like:

But when I run it, it says:

Thanks!
Kjeld

Kjeld Sigtermans - SCJP 1.4 - SCWCD 1.4
Kjeld Sigtermans
Ranch Hand

Joined: Aug 10, 2006
Posts: 122
PS: however, when I put the config file in the project's resources folder (src/main/resources) it works fine.
But I want it in the WEB-INF folder...
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
1. Rename to applicationContext.xml.
2. Add the following to web.xml.


You can read the following link for more information.
http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#context-introduction


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
Kjeld Sigtermans
Ranch Hand

Joined: Aug 10, 2006
Posts: 122
Ok thank you.

So if I implement a listener in the web.xml, I do not need to use the programmatic model.

Still my facade is not being instantiated.

Let's say I have a client class called Client who has a member variable of type Facade. How can I make Spring inject an instance of FacadeImpl into Client without using ApplicationContext programmatically?

(Note that Client objects are being instantiated by another framework.)

Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Kjeld Sigtermans wrote:
Let's say I have a client class called Client who has a member variable of type Facade. How can I make Spring inject an instance of FacadeImpl into Client without using ApplicationContext programmatically?

(Note that Client objects are being instantiated by another framework.)


You have to integrate that framework and Spring. The bottom line is we must let Spring instantiates the object, otherwise DI will not happen. What is the framework you're using?
Kjeld Sigtermans
Ranch Hand

Joined: Aug 10, 2006
Posts: 122
The Framework is JSF. The Client class I mentioned is a Message Bean.
I believe it is possible to combine Spring and JSF?

Thanks for your quick response!
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Take a look at http://www.javabeat.net/articles/39-integrating-spring-and-jsf-1.html.
Kjeld Sigtermans
Ranch Hand

Joined: Aug 10, 2006
Posts: 122
Thanks Kengkaj for all the information. I will use it when we set up our new project.
Trilochan Bharadwaj
Ranch Hand

Joined: Feb 02, 2009
Posts: 100
Well, actually, its not just JSF with Spring you'd need to refer to, you'd need to know (I am assuming from the way you speak, this is JMS and EJB oriented environment we're talking about) :
for EJBs, refer to:

1. ) EJB integration
2.) JMS integration

container will need app context, so you'd also need to change your variable resolver reference in facesconfig to point to spring. Rest is pretty much the same.

Trilochan.
Kjeld Sigtermans
Ranch Hand

Joined: Aug 10, 2006
Posts: 122
Hi Trilochan,

No I am sorry to have been unclear. The 'MessageBean' is part of our JSF implementation. It is not a Message Driven Bean, we have not used EJBs in the project.

Greetings!
Kjeld
Trilochan Bharadwaj
Ranch Hand

Joined: Feb 02, 2009
Posts: 100
Ah I see ... no problem. Good luck! =)

Trilochan.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help needed to set up Spring IOC in a web application
 
Similar Threads
collection getting appended on refresh of page
Spring Application Context exception: Cannot find the declaration of element 'beans'
"Document root element "beans", must match DOCTYPE root" problem
Spring Portlet - Bean scope error
Problem in integrating apache tiles with Spring 3.0