Help needed to set up Spring IOC in a web application
Kjeld Sigtermans
Ranch Hand
Joined: Aug 10, 2006
Posts: 111
posted
0
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 JUnittest 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: 111
posted
0
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
posted
0
1. Rename to applicationContext.xml.
2. Add the following to web.xml.
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: 111
posted
0
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
posted
0
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: 111
posted
0
The Framework is JSF. The Client class I mentioned is a Message Bean.
I believe it is possible to combine Spring and JSF?
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
posted
0
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:
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: 111
posted
0
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.