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
.
You need to provide path to the xml configuration file at the beggining, when Spring didn't started building application context. The @ImportResource annotation are processed in the middle of this process, after many of PostProcessors have been registered.
You need to provide a path to the xml configuration using SpringApplicationBuilder.sources method.
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources("classpath:/WEB-INF/context/payables-profile.xml", Application.class);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.