java - accessing the spring context -


something confuses me spring. understand 1 of main functions promote modularity , loose coupling providing way modular components (classes) wired without having knowledge of other modules' implementations, contracts expose.

however, in practice, find myself in following situation.

one class has spring application context. let's in main();

so load spring in main, , rest of objects nicely injected beans , can interact without being tightly coupled.

but if 1 of objects has method every time it's called, needs create fresh bean? object needs reference spring application context. it's no longer pojo, it's coupled spring?

i guess in practice, create global object contains spring application context easy access classes need this, still seems bit messy , counter spring philosophy of loose coupling?

is 1 of things? it's not perfect it's still lot better having classes tightly coupled?

edit: , if use global object hold spring application context, how unit test class accesses it? write global holder can parameterize test context. alternatively if don't use global object, rather inject spring context classes need it, means need keep chain of injecting spring going down through code, or you'll reach point want non-springified object create new bean, have no reference spring?

the situation describe (creating objects in each method invocation) connected creating business model objects , fine create such objects using "new".

spring ioc (dependency injection) important non-model objects, in practice won't need spring context create objects in specific method, because created class not dependency.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -