1
votes

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.

1
Post your main class. Spring Boot should already register the processor for you so you could remove that line from your XML. Looks like you are doing something wrong in your main class. Also please read stackoverflow.com/help/how-to-ask and please expand your question.M. Deinum
Post the class where you're calling that method.chrylis -cautiouslyoptimistic-
Updated my question with more information.jo1984
First <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 the Application 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
SInce I have to scan specific packages from the jar, I have to use <component-scan elements.jo1984

1 Answers

0
votes

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);
}