Where do I put my XML beans in a Spring Boot application?

49,594

Solution 1

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 {
  //
}

Solution 2

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.

Share:
49,594
David Newcomb
Author by

David Newcomb

Managing Director of BigSoft Limited with 30 years of programming experience. Currently working for a global tech company you have definitely heard of!

Updated on June 27, 2020

Comments

  • David Newcomb
    David Newcomb almost 4 years

    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?

  • David Newcomb
    David Newcomb almost 9 years
    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?
  • chrylis -cautiouslyoptimistic-
    chrylis -cautiouslyoptimistic- almost 9 years
    @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.
  • bvulaj
    bvulaj almost 9 years
    @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.
  • David Newcomb
    David Newcomb over 6 years
    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.
  • granadaCoder
    granadaCoder about 5 years
    Please add this is a ~comment~ if you are pro-annotation vs xml. By the way, not everyone agrees with you. literatejava.com/spring/… #justSayin #notHatin
  • Parasu
    Parasu about 5 years
    you saved my day.
  • Blessed Geek
    Blessed Geek almost 5 years
    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.
  • Mike
    Mike almost 5 years
    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?
  • bvulaj
    bvulaj almost 5 years
    @Mike I would say that most modern Spring Boot apps use zero xml.
  • sunzy
    sunzy about 3 years
    Add @ImportResource("classpath:beanFileName.xml"), not @ImportResource("classPath:beanFileName.xml")