I am converting my existing Spring 3.1 based web application into Spring Boot 1.2.5. I initially upgraded my old application to Spring 4.1.7.
The old application has many xml bean configuration files imported using init parameter contextConfigLocation
in the web.xml
. In the new Spring boot application these xml are imported using @ImportResource
in the main class.
When I start my application, I could see that the xml based beans are created with autowired fields null
. When I debug the bean creation part, it looks like the AutowiredAnnotationBeanPostProcessor
is not yet added to the beanfactory. But xml contains <context:annotation-config />
, which suppose to add the AutowiredAnnotationBeanPostProcessor
. Also this post processor getting added right after my xml based beans are created.
Many of the bean dependencies are autowired bean in the xml defined bean.
sample xml config:
<context:annotation-config/>
<context:component-scan base-package="x.y.z"/>
<bean id="discoveredResourceClasses" factory-bean="resourceManager" factory-method="getResourceClasses">
<constructor-arg>
<list>
<value>x.y.z.resources</value>
</list>
</constructor-arg>
</bean>
Factotry Bean class which is used to create 'discoveredResourceClasses' bean:
@Service
public class ResourceManager implements Serializable{
@Autowired
protected IExtensionManager extensionManager;
public List<Class<? extends Resource>> getResourceClasses(String ... packageNames)
{
return extensionManager.getExtensions(packageName, includeEverything, Resource.class);
}
}
Autowired bean:
@Service
public class ExtensionManager implements IExtensionManager {
public <T> List<T> getExtensions(String packageName, Map<String, String> filter, Class<T> clazz) {
//busoinesslogic .
}
}
Spring boot class :
@ImportResource("classpath*:xad-config-all.xml")
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
At the time of resourceManager
bean creation, the field extensionManager
is coming as null
. So the factory-method(getResourceClasses) throws NullPointerException
.
<context:component-scan />
already implies<context:annotation-driven />
. Second Spring Boot uses an application context that has annotations enabled by default. One thing to try is remove theo<context:annotation-config
> and the<component-scan
elements. Move the@ImportResource
after the@SpringBootApplication
and move theApplication
class to the most upper level package you can use (not the root but somewhere in,com.company.app
so that it can automatically scan all the@Compoment
based beans. Then try again. – M. Deinum