How to have a class use applicationContext beans?

I think Spring will normally do dependency injection only for those classes that are created by Spring, not for the ones that you create yourself with new operator.

I think Spring will normally do dependency injection only for those classes that are created by Spring, not for the ones that you create yourself with new operator. BeanTest is created by Spring so it will get its dependencies injected, but BeanTest2 is not created by Spring, thus Spring knows nothing about BeanTest2 instances. You can add BeanTest2 as a field in BeanTest public class BeanTest { private BeanTest2 beanTest2; public void setBeanTest2(BeanTest2 b) { this.

BeanTest2 = b; } public BeanTest2 getBeanTest2() { return this. BeanTest2; }; } Then you can inject beanTest2 to beanTest instance. This way beanTest2 should be injected into BeanTest instance.

– Adam Plumb Jun 4 '09 at 20:34 @Adam, you could do that, or you could inject BeanTest2 directly to BeanTest (see edited answer) or you could obtain reference to ApplicationContext instance and fetch beans directly from that. – Juha Syrjälä Jun 4 '09 at 20:43 @Juha, wouldn't just pass the string "beanTest2" to BeanTest? – Adam Plumb Jun 4 '09 at 20:46 Yes, you are right, you need to use ref instead of value.

Fixed answer. – Juha Syrjälä Jun 4 '09 at 20:50 Thanks! That works!

I just want to clarify for others that you should not instantiate beatTest2 in BeanTest (i.e. BeanTest2 = new BeanTest2();). This will cause the spring-created beanTest2 object to be overwritten.

– Adam Plumb Jun 4 '097 at 16:37.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions