Correct me if I'm wrong but are you looking for a way to do something like
where new TestObject may be replaced by Spring, automagically returning a bean from the application context?Code:public class MyClass { public void someMethod () { TestObject myTestObject = new TestObject (); } }
As irbouho said, that's not really possible without maybe using AOP to intercept the construction of TestObject. Your best bet is to have a some kind of factory which is injected into MyClass.
The factory can then decide how it wants to create/acquire a new TestObject instance. OK, it's one more class but it's still ignorant of Spring.Code:public class MyClass { TestObjectFactory toFactory; public void setFactory ( TestObjectFactory tof ) { toFactory = tof; } public void someMethod () { TestObject myTestObject = toFactory.newTestObject (); } }
My experience is that maybe 85% of my classes have no knowledge of Spring but to harness the full power of Spring you need to use it's API, ideally only in your-own-framework classes.
Maybe you can give us an example of your ideal code and we'll try to help
Cuong.



Reply With Quote