Hello, I got an issue about @Autwired annotation on a Bean that is accessed through a parent application context.
There are two application contexts: Parent and Child. Parent context contains bean B and child context contains bean A. A uses @Autowired on a class variable B. It looks like this:
As I get bean A from child context the @Autowiring on myB didnt work. MyB reference is null. The following testcase shows how it is working:Code:class A { @Autowired private B myB; public B getMyB() { return myB; } } class B {}
It seems that it is legit to load beans that are registered in a parent context through child contexts by calling getBean Method but not inject them through @Autowiring. Is that a bug, works as designed or am I doing something wrong?Code:public void testParentAutowiring() { // arrange // Parent GenericXmlApplicationContext ctxParent = new GenericXmlApplicationContext(); BeanDefinition bd = BeanDefinitionBuilder.genericBeanDefinition(B.class).getBeanDefinition(); ctxParent.registerBeanDefinition(B.class.getName(), bd); // Child GenericXmlApplicationContext ctxChild = new GenericXmlApplicationContext(); ctxChild.setParent(ctxParent); BeanDefinition bd2 = BeanDefinitionBuilder.genericBeanDefinition(A.class).getBeanDefinition(); ctxChild.registerBeanDefinition(A.class.getName(), bd2); // act A a = ctxChild.getBean(A.class); B b = ctxChild.getBean(B.class); // assert assertNotNull(a); assertNotNull(b); assertEquals(null, a.getMyB()); }
Thanks in advance.


Reply With Quote