-1

I have a SpringBoot application that uses @Component annotations to create beans rather than the old XML-based applicationContext files.

I need to use a library that uses XML-based beans. I need those beans initialized and available in my application, otherwise I'd end up rewriting quite a bit of code.

I've tried using @ComponentScan to find them, which as expected does not work as the classes are not annotated with @Component. I've tried using @ImportResource, as described here but no luck.

So the question is, how can I instantiate the beans in a jar dependency, which are defined in the XML style while my app does not use XML applicationContext files and then use them in my application?

Note:

The library I need to import is quite old and the chance of having it use component annotations is pretty much 0. Whatever I change has to be in my project.

I am aware that starting to use applicationContexts might work, but I really want to move away from XML-based beans.


Here's the SpringBootApplication class in case it might be useful:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyApp extends SpringBootServletInitializer {
    public static void main(final String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}
1

2 Answers 2

1

I think you should use ClassPathXmlApplicationContext to load the classes present in the JAR file as Spring beans. You may need to workout the dependencies of the beans properly and add them as properties to the main bean. Otherwise, you may get some run time exceptions.

Sample code:-

ApplicationContext context 
      = new ClassPathXmlApplicationContext(
        "classpathxmlapplicationcontext-example.xml");

BeanClassName bean = context.getBean("beanName", BeanClassName.class);
1
  • +1 This does indeed work but as you mentioned, the properties are not found and going this way will be a lot of work. Hence I will not accept it as an answer yet, just in case some better solutions are mentioned. Mar 14, 2018 at 14:41
0

You can tell your Spring Boot component scan to look for those JARs directly in their package like the following:

@ComponentScan(basePackages = {"org.thirdparty.thirdpartyjar"})

This is a question/answer I believe to be similar to yours.

2
  • As I mentioned in the question, I've tried this and it doesn't work as the library classes are not annotated with @Component so they won't be found. Mar 14, 2018 at 14:07
  • Well then there might be no other option than declaring their application context XML and getting the beans from it. And yes, my bad, didn't read your question carefully enough at first :) Mar 14, 2018 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.