Close

Spring Boot - SpringBootServletInitializer Examples

Spring Boot 


SpringBootServletInitializer is an extension of WebApplicationInitializer. It is needed to run a SpringApplication from a WAR deployment. The implementing classes are supposed to provided the source configuration class by extending configure() method. The configuration source class should be annotated with @SpringBootApplication or its variant.


package com.logicbig.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@SpringBootApplication
public class WarStructureExample extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure (SpringApplicationBuilder builder) {
return builder.sources(WarStructureExample.class);
}

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

@Controller
public static class MyController {

@RequestMapping("/")
public String handler (Model model) {
model.addAttribute("msg",
"a spring-boot war example");
return "myPage";
}
}
}
Original Post




package com.logicbig.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.time.LocalDateTime;

@SpringBootApplication
public class WarExplodedExample extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure (SpringApplicationBuilder builder) {
return builder.sources(WarExplodedExample.class);
}

public static void main (String[] args) {
SpringApplication sa = new SpringApplication(WarExplodedExample.class);
sa.setLogStartupInfo(false);
sa.run(args);
}

@Controller
public static class MyController {

@RequestMapping("/")
public String handler (Model model) {
model.addAttribute("date",
LocalDateTime.now());
return "myPage";
}
}
}
Original Post




@SpringBootApplication
public class JarWebappExplodedExample extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure (SpringApplicationBuilder builder) {
return builder.sources(JarWebappExplodedExample.class);
}

public static void main (String[] args) {
SpringApplication sa = new SpringApplication(JarWebappExplodedExample.class);
sa.setLogStartupInfo(false);
sa.run(args);
}

@Controller
public static class MyController {

@RequestMapping("/")
public String handler (Model model) {
model.addAttribute("date",
LocalDateTime.now());
return "myPage";
}
}
}
Original Post




See Also