1

I need to upgrade an application from spring-boot-1.2.5.RELEASE to spring-boot-2.0.0.RELEASE.

I have this following code:

@Configuration
@ComponentScan
@EnableAutoConfiguration(exclude = {HibernateJpaAutoConfiguration.class, RedisAutoConfiguration.class})
public class NiceBootApplicationWithoutDB extends AbstractBootApplication {

    public static final String APPLICATION_CONTEXT_XML = "classpath:/META-INF/application-context-nodb.xml";

    public static void main(String[] args) {
        SpringApplication.run(APPLICATION_CONTEXT_XML, getFullArgList(args));
    }

}

The overload of SpringApplication.run(APPLICATION_CONTEXT_XML, getFullArgList(args)) is:

/**
 * Static helper that can be used to run a {@link SpringApplication} from the
 * specified source using default settings.
 * @param source the source to load
 * @param args the application arguments (usually passed from a Java main method)
 * @return the running {@link ApplicationContext}
 */
public static ConfigurableApplicationContext run(Object source, String... args) {
    return run(new Object[] { source }, args);
}

/**
 * Static helper that can be used to run a {@link SpringApplication} from the
 * specified sources using default settings and user supplied arguments.
 * @param sources the sources to load
 * @param args the application arguments (usually passed from a Java main method)
 * @return the running {@link ApplicationContext}
 */
public static ConfigurableApplicationContext run(Object[] sources, String[] args) {
    return new SpringApplication(sources).run(args);
}

Both the overloads are not present in spring-boot-2.0.0.RELEASE.

My question is - How do I upgrade the above code?

Stav Alfi
  • 13,139
  • 23
  • 99
  • 171
  • The SpringApplication [constructor](https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/SpringApplication.html#SpringApplication-java.lang.Class...-) and [run](https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/SpringApplication.html#run-java.lang.Class:A-java.lang.String:A-) method now use a `Class>[] primarySources` argument... did you try that? – Marc Tarin Mar 20 '18 at 09:08

3 Answers3

3

You are right : the API of the SpringApplication class in the version 2 of Spring Boot doesn't provide an equivalence.
So no direct way to provide a XML Spring configuration file.

According to this answer, you could annotate your Spring Boot class with @ImportResource.

@ImportResource("classpath:/META-INF/application-context-nodb.xml")

It works as @Import but that it imports XML spring configuration files instead of class files.

Javadoc information :

Indicates one or more resources containing bean definitions to import.

Like @Import, this annotation provides functionality similar to the element in Spring XML. It is typically used when designing @Configuration classes to be bootstrapped by an AnnotationConfigApplicationContext, but where some XML functionality such as namespaces is still necessary.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
1

You can use annotation @ImportResourcefor import configuration XML

    @Configuration
    @ComponentScan
    @EnableAutoConfiguration(exclude = {HibernateJpaAutoConfiguration.class, RedisAutoConfiguration.class})
    @ImportResource(APPLICATION_CONTEXT_XML)
    public class NiceBootApplicationWithoutDB extends AbstractBootApplication {

        public static final String APPLICATION_CONTEXT_XML = "classpath:/META-INF/application-context-nodb.xml";

        public static void main(String[] args) {
            SpringApplication.run(AbstractBootApplication.class, getFullArgList(args));
        }        
    }
borino
  • 1,720
  • 1
  • 16
  • 24
  • Why you are using `SpringApplication.main` instead of `SpringApplication.run` ? – Stav Alfi Mar 20 '18 at 09:03
  • Sorry for that, copy past from my IDE... of course we should use run, without configuration source class it will not works. If we wanna use main we should add configuration in parameters for main method – borino Mar 20 '18 at 09:23
0

You can add a Configuration class like this, that will take in considération your spring xml confiiguration :)

@Configuration
@ImportResource({"classpath*:applicationContext.xml"})
public class XmlConfiguration {

}
A ELKAS
  • 146
  • 1
  • 5