In all our applications integration tests, we are using manual autowiring with the following:
context = new ClassPathXmlApplicationContext(getConfigLocations());
context.getAutowireCapableBeanFactory().autowireBeanProperties(this, getAutowireMode(), true);
This way, the required Spring beans are automatically injected in the test class (this).
This works fine, but in one of my test class, i’ve made my class being a InitializingBean.
When i autowire this way in my bean, the afterPropertiesSet() method is never triggered, while in debug i see all the properties being set correctly.
Can someone explain me why?
autowireBeanProperties()only autowires properties, it doesn’t perform other steps of initialization.You can use other methods of
AutowireCapableBeanFactoryto perform the full initialization, such asinitializeBean(). However, it looks like you cannot passautowireModein this case. So, it’s not clear how to solve this problem if you really need customautowireModes (i.e. if you don’t use@Autowiredin your bean).