• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help needed to set up Spring IOC in a web application

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Kjeld Sigtermans
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at http://www.javabeat.net/articles/39-integrating-spring-and-jsf-1.html.
 
Kjeld Sigtermans
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Kengkaj for all the information. I will use it when we set up our new project.
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah I see ... no problem. Good luck! =)

Trilochan.
reply
    Bookmark Topic Watch Topic
  • New Topic