31

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?

gotson
  • 3,613
  • 1
  • 23
  • 40
David Newcomb
  • 10,639
  • 3
  • 49
  • 62

3 Answers3

46

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 {
  //
}
bvulaj
  • 5,023
  • 5
  • 31
  • 45
  • Do I have to put @ImportResource on top of every spring application and test case I write or is there a default name/location that spring automatically looks in? – David Newcomb Jul 28 '15 at 13:37
  • @DavidNewcomb You just need to make sure that a configuration class with the annotation is reachable from your configuration root. If it's something you use all the time, you might even add your own autoconfiguration class for it. – chrylis -cautiouslyoptimistic- Jul 28 '15 at 13:39
  • @DavidNewcomb you traditionally only want one `@SpringBootApplication` in your application, as it itself is a combination of `@Configuration` and a few other must-haves. Your `@ImportResource` only needs to be on a single `@Configuration` class that gets scanned by Spring, just like the auto-scanning that happens / happened in XML land. – bvulaj Jul 28 '15 at 13:43
  • you saved my day. – Parasu Mar 08 '19 at 15:04
  • I read that the one thing `Spring Boot` is very good at is making the "configuration" process easy and that it can be used without involving configuration `xml` at all, but `xml` can join the configuration process by applying what @bvulaj mentioned. Is it possible to fully configure my application without the use of any `xml`? – Mike Jul 21 '19 at 18:11
  • @Mike I would say that most modern Spring Boot apps use zero xml. – bvulaj Jul 22 '19 at 19:03
3

You also can translate the XML config to a Java config. In your case it would look like:

@Bean
public DefaultSftpSessionFactory sftpSessionFactory() {
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory();
    factory.setHost("localhost");
    factory.setPrivateKey(new ClassPathResource("classpath:META-INF/keys/sftpTest"));
    factory.setPrivateKeyPassphrase("springIntegration");
    factory.setPort(22);
    factory.setUser("kermit");
    return factory;
}

You can put this method in the class with the @SpringBootApplication annotation.

dunni
  • 43,386
  • 10
  • 104
  • 99
0

Spring boot ideal concept is avoid xml file. but if you want to keep xml bean, you can just add @ImportResource("classPath:beanFileName.xml").

I would recommend remove the spring-sftp-config.xml file. And, convert this file to spring annotation based bean. So, whatever class has been created as bean. Just write @Service or @Component annotation before class name. for example:

XML based:

<bean ID="id name" class="com.example.Employee">

Annotation:

@Service or @Component
class Employee{
}

And, add @ComponentScan("Give the package name"). This is the best approach.

David Newcomb
  • 10,639
  • 3
  • 49
  • 62
Md Jalal
  • 109
  • 1
  • 2
  • 2
    The question was "How do I link in existing xml files?" and not "How do I convert all my beans to annotations?". Your answer is the same as bvulaj's. – David Newcomb Jan 31 '18 at 11:46
  • Please add this is a ~comment~ if you are pro-annotation vs xml. By the way, not everyone agrees with you. http://literatejava.com/spring/explicit-vs-implicit-configuration-spring/ #justSayin #notHatin – granadaCoder Feb 26 '19 at 20:53
  • 1
    Nope. Avoiding XML is not ideal. I have SQL, SAS scripts, HTML blocks in String beans in XML context files, exploiting the the context variable substitution feature. – Blessed Geek Jul 08 '19 at 00:26
  • Add @ImportResource("classpath:beanFileName.xml"), not @ImportResource("classPath:beanFileName.xml") – sunzy Feb 14 '21 at 14:40