I'm getting back into Spring (currently v4). It's all wonderful now with @SpringBootApplication
and the other annotations but all the documentation seems to forget to mention how I define other beans in XML!
For example I'd like to create an "SFTP Session Factory" as defined at: http://docs.spring.io/spring-integration/reference/html/sftp.html
There is a nice bit of XML to define the bean but where on earth do I put it and how do I link it in? Previously I did a:
ApplicationContext context = new ClassPathXmlApplicationContext( "classpath:applicationContext.xml");
to specify the file name and location but now that I'm trying to use:
ApplicationContext ctx = SpringApplication.run(Application.class);
Where do I put the XML file? Is there a magic spring name to call it?
For a web application, the beans. xml file must be in the WEB-INF directory. For EJB modules or JAR files, the beans. xml file must be in the META-INF directory.
We will import our XML file in java configuration. We need to use @ImportResource with @Configuration in our spring boot application. We can keep our XML files in project classpath. Here we will create a spring boot web application that will work as REST web service.
It depends on where the main class is located which has generally @SpringBootApplication annotations. You can annotate this main class with @ComponentScan(basePackageClasses = HelloWorld. class) . Here HelloWorld has bean definitions annotated with @Beans and the class is annotated with @Configurations .
Before Spring 3.0, XML was the only way to define and configure beans. Spring 3.0 introduced JavaConfig, allowing us to configure beans using Java classes. However, XML configuration files are still used today. In this tutorial, we'll discuss how to integrate XML configurations into Spring Boot.
As long as you're starting with a base @Configuration
class to begin with, which it maybe sounds like you are with @SpringBootApplication
, you can use the @ImportResource
annotation to include an XML configuration file as well.
@SpringBootApplication @ImportResource("classpath:spring-sftp-config.xml") public class SpringConfiguration { // }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With