File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Application Frameworks and the fly likes Reading from CommonContext.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Application Frameworks » Other Application Frameworks
Reply Bookmark "Reading from CommonContext.xml" Watch "Reading from CommonContext.xml" New topic
Author

Reading from CommonContext.xml

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 867
I realize that the classes defined in CommonContext.xml get instantiated automatically by Spring, but I'm not sure how to get a hold of that instance.

What's the "Spring way" of getting a hold of a class that Spring automatically instantiates for me (that is with a BeanID or other ID) defined in CommonContext.xml?

Thanks in advance!!!

-- Mike
Junilu Lacar
Ranch Hand

Joined: Feb 26, 2001
Posts: 3008
In most cases, you wouldn't pull a class from the Spring context. You'd just let Spring "wire" your application components together via dependency injection (DI).

Here's what a traditional Java application would look like:



Of course, you'd probably need at least one "pull" from the context to start the application off (although it is possible to not have to do a pull at all). In a Spring-wired application, you might have something like:



In the above example, CommonContext.xml would define a bean of class Foo with an id or name of "mainApplication" and inject another bean of class Bar into its bar property. There may be more lines of code in the second sample but it is easier to reconfigure: simply change the CommonContext.xml definitions and restart the application.

Of course, in the real world you should always consider the likelihood of needing to change implementations so that it would actually be worth making the design like the second example. Another consideration would be testability. If Springifying a class makes it much easier to test, then I would also tend to do it.
[ May 06, 2006: Message edited by: Junilu Lacar ]

Junilu
[How to Ask Questions] [How to Answer Questions] [MiH]
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 867
Thanks!!!

Great example.

Mike
 
 
subject: Reading from CommonContext.xml
 
Threads others viewed
Hard time getting Spring working with WS'.
Introducing Dependency Injection in an Application( RESTful WebSrvcs/FLEX)
popup form in spring
Session Object
Different sessions on different firefox tabs ?
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture