0

I refactored my project as a Spring Boot application from inside Spring Tool Suite. All the documentation shows how to create a self contained application with an embedded Tomcat instance which works and is great.

Before this however I had my own Maven Web-MVC project with XML-based configuration for Spring. I could deploy this directly to TC Server and it worked great. What I like most: Hot Swapping! I could add new functions and classes and they immediately were picked up by TC Server without any additional configuration.

Using Spring Boot and embedded Tomcat, however, I've found this is not the case. Hotswapping is very limited and a restart is required for a lot of basic changes. From what I've been reading TC Server is a much more sophisticated version of Tomcat so this leads me to my two questions:

1) How do I get my Maven Spring Boot application running on TC Server via STS? Starting a new Pivotal TC Run Configuration is not working for me.

2) Why don't Spring Boot documentation examples emphasize usage of TC Server over the embedded Tomcat when the former appears to work so much better?

IcedDante
  • 6,145
  • 12
  • 57
  • 100

1 Answers1

4

You can deploy a Spring Boot application to tc Server in the same way that you'd deploy it to any other standalone servlet container. There are three changes that you need to make:

  1. Extend SpringBootServletInitializer so that the container will bootstrap your application correctly:

    @Configuration
    @EnableAutoConfiguration
    @ComponentScan
    public class Application extends SpringBootServletInitializer {
    
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(Application.class);
        }
    }
    
  2. Convert the project to use war packaging. Maven example:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <!-- ... -->
        <packaging>war</packaging>
        <!-- ... -->
    </project>
    
  3. Mark your spring-boot-starter-tomcat dependency as provided so that embedded Tomcat doesn't conflict with the classes in tc Server. Maven example:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    

I'm not aware of any differences between the class-reloading capabilities of Tomcat and tc Server. Perhaps you have Spring Loaded configured in your tc Server instance? If so, you can use it with Spring Boot too.

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • I only needed to make change #1 to my project, but now I am not sure how to proceed. With my Web Maven project I could right click on the project in STS and select "Run On Server". Pivotal TC Server would show up and I could send the application there. If I right click on my Spring Boot project, however, the only two options are Run as "Spring Boot App" or "Java Application" – IcedDante Dec 04 '14 at 05:46
  • 2
    @IcedDante I just drag the generated target/proj to the server, then hit run. Does this not work for you? Also, all three steps listed above (including a change I mention below) ARE required to deploy a Spring Boot application as a proper WAR. You'll run into very weird issues if you dont. As a side note to @Andy_Wilkinson, Step two is incorrect. It should be `war` – medge Dec 18 '14 at 00:16
  • Matt- war packaging was what I needed. Thanks! – IcedDante Dec 22 '14 at 23:10