From 9c9351d5e21d621ecbe3a601ab72ac0a33953248 Mon Sep 17 00:00:00 2001 From: Gaurav Rai Mazra Date: Tue, 2 Jun 2015 20:29:02 +0530 Subject: [PATCH] Common Configuration for Spring Boot Application --- spring-boot/pom.xml | 16 ++++ .../common/error/MyCustomErrorController.java | 24 +++++ .../SpringHelloServletRegistrationBean.java | 15 +++ .../error/controller/ErrorController.java | 22 +++++ .../MyServletContainerCustomizationBean.java | 25 +++++ .../ExecutorServiceExitCodeGenerator.java | 30 ++++++ .../controller/servlet/HelloWorldServlet.java | 45 +++++++++ .../servlet/SpringHelloWorldServlet.java | 45 +++++++++ .../main/SpringBootActuatorApplication.java | 29 ------ .../baeldung/main/SpringBootApplication.java | 92 +++++++++++++++++++ .../src/main/resources/application.properties | 2 + 11 files changed, 316 insertions(+), 29 deletions(-) create mode 100644 spring-boot/src/main/java/org/baeldung/common/error/MyCustomErrorController.java create mode 100644 spring-boot/src/main/java/org/baeldung/common/error/SpringHelloServletRegistrationBean.java create mode 100644 spring-boot/src/main/java/org/baeldung/common/error/controller/ErrorController.java create mode 100644 spring-boot/src/main/java/org/baeldung/common/properties/MyServletContainerCustomizationBean.java create mode 100644 spring-boot/src/main/java/org/baeldung/common/resources/ExecutorServiceExitCodeGenerator.java create mode 100644 spring-boot/src/main/java/org/baeldung/controller/servlet/HelloWorldServlet.java create mode 100644 spring-boot/src/main/java/org/baeldung/controller/servlet/SpringHelloWorldServlet.java delete mode 100644 spring-boot/src/main/java/org/baeldung/main/SpringBootActuatorApplication.java create mode 100644 spring-boot/src/main/java/org/baeldung/main/SpringBootApplication.java diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index 6560c159e9..df759e8d83 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -19,7 +19,23 @@ org.springframework.boot spring-boot-starter-web + + + + + org.springframework.boot diff --git a/spring-boot/src/main/java/org/baeldung/common/error/MyCustomErrorController.java b/spring-boot/src/main/java/org/baeldung/common/error/MyCustomErrorController.java new file mode 100644 index 0000000000..0b644143c0 --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/common/error/MyCustomErrorController.java @@ -0,0 +1,24 @@ +package org.baeldung.common.error; + +import org.springframework.boot.autoconfigure.web.ErrorController; +import org.springframework.web.bind.annotation.RequestMapping; + +public class MyCustomErrorController implements ErrorController { + + private static final String PATH = "/error"; + + public MyCustomErrorController() { + // TODO Auto-generated constructor stub + } + + @RequestMapping(value=PATH) + public String error() { + return "Error heaven"; + } + + @Override + public String getErrorPath() { + return PATH; + } + +} diff --git a/spring-boot/src/main/java/org/baeldung/common/error/SpringHelloServletRegistrationBean.java b/spring-boot/src/main/java/org/baeldung/common/error/SpringHelloServletRegistrationBean.java new file mode 100644 index 0000000000..78680baf7d --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/common/error/SpringHelloServletRegistrationBean.java @@ -0,0 +1,15 @@ +package org.baeldung.common.error; + +import javax.servlet.Servlet; + +import org.springframework.boot.context.embedded.ServletRegistrationBean; + +public class SpringHelloServletRegistrationBean extends ServletRegistrationBean { + + public SpringHelloServletRegistrationBean() { + } + + public SpringHelloServletRegistrationBean(Servlet servlet, String... urlMappings) { + super(servlet, urlMappings); + } +} diff --git a/spring-boot/src/main/java/org/baeldung/common/error/controller/ErrorController.java b/spring-boot/src/main/java/org/baeldung/common/error/controller/ErrorController.java new file mode 100644 index 0000000000..14ba885168 --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/common/error/controller/ErrorController.java @@ -0,0 +1,22 @@ +package org.baeldung.common.error.controller; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class ErrorController { + + public ErrorController() { + } + + @RequestMapping("/400") + String error400() { + return "Error Code: 400 occured."; + } + + @RequestMapping("/errorHeaven") + String errorHeaven() { + return "You have reached the heaven of errors!!!"; + } + +} diff --git a/spring-boot/src/main/java/org/baeldung/common/properties/MyServletContainerCustomizationBean.java b/spring-boot/src/main/java/org/baeldung/common/properties/MyServletContainerCustomizationBean.java new file mode 100644 index 0000000000..b2a776e751 --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/common/properties/MyServletContainerCustomizationBean.java @@ -0,0 +1,25 @@ +package org.baeldung.common.properties; + +import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; +import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; +import org.springframework.boot.context.embedded.ErrorPage; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Component; + +@Component +public class MyServletContainerCustomizationBean implements EmbeddedServletContainerCustomizer { + + public MyServletContainerCustomizationBean() { + + } + + @Override + public void customize(ConfigurableEmbeddedServletContainer container) { + container.setPort(8084); + container.setContextPath("/springbootapp"); + + container.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/400")); + container.addErrorPages(new ErrorPage("/errorHeaven")); + } + +} diff --git a/spring-boot/src/main/java/org/baeldung/common/resources/ExecutorServiceExitCodeGenerator.java b/spring-boot/src/main/java/org/baeldung/common/resources/ExecutorServiceExitCodeGenerator.java new file mode 100644 index 0000000000..1af0227f81 --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/common/resources/ExecutorServiceExitCodeGenerator.java @@ -0,0 +1,30 @@ +package org.baeldung.common.resources; + +import java.util.Objects; +import java.util.concurrent.ExecutorService; + +import org.springframework.boot.ExitCodeGenerator; + +public class ExecutorServiceExitCodeGenerator implements ExitCodeGenerator { + + private ExecutorService executorService; + + public ExecutorServiceExitCodeGenerator(ExecutorService executorService) { + } + + @Override + public int getExitCode() { + int returnCode = 0; + try { + if (!Objects.isNull(executorService)) { + executorService.shutdownNow(); + returnCode = 1; + } + } + catch (SecurityException ex) { + returnCode = 0; + } + return returnCode; + } + +} diff --git a/spring-boot/src/main/java/org/baeldung/controller/servlet/HelloWorldServlet.java b/spring-boot/src/main/java/org/baeldung/controller/servlet/HelloWorldServlet.java new file mode 100644 index 0000000000..0f874f759b --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/controller/servlet/HelloWorldServlet.java @@ -0,0 +1,45 @@ +package org.baeldung.controller.servlet; + +import java.io.IOException; +import java.io.PrintWriter; +import java.util.Objects; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class HelloWorldServlet extends HttpServlet { + private static final long serialVersionUID = 1L; + + public HelloWorldServlet() { + super(); + } + + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + PrintWriter out = null; + try { + out = response.getWriter(); + out.println("HelloWorldServlet: GET METHOD"); + out.flush(); + } + finally { + if (!Objects.isNull(out)) + out.close(); + } + } + + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + PrintWriter out = null; + try { + out = response.getWriter(); + out.println("HelloWorldServlet: POST METHOD"); + out.flush(); + } + finally { + if (!Objects.isNull(out)) + out.close(); + } + } + +} diff --git a/spring-boot/src/main/java/org/baeldung/controller/servlet/SpringHelloWorldServlet.java b/spring-boot/src/main/java/org/baeldung/controller/servlet/SpringHelloWorldServlet.java new file mode 100644 index 0000000000..df14b2af4b --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/controller/servlet/SpringHelloWorldServlet.java @@ -0,0 +1,45 @@ +package org.baeldung.controller.servlet; + +import java.io.IOException; +import java.io.PrintWriter; +import java.util.Objects; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class SpringHelloWorldServlet extends HttpServlet { + private static final long serialVersionUID = 1L; + + public SpringHelloWorldServlet() { + super(); + } + + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + PrintWriter out = null; + try { + out = response.getWriter(); + out.println("SpringHelloWorldServlet: GET METHOD"); + out.flush(); + } + finally { + if (!Objects.isNull(out)) + out.close(); + } + } + + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + PrintWriter out = null; + try { + out = response.getWriter(); + out.println("SpringHelloWorldServlet: POST METHOD"); + out.flush(); + } + finally { + if (!Objects.isNull(out)) + out.close(); + } + } + +} diff --git a/spring-boot/src/main/java/org/baeldung/main/SpringBootActuatorApplication.java b/spring-boot/src/main/java/org/baeldung/main/SpringBootActuatorApplication.java deleted file mode 100644 index a8de99f149..0000000000 --- a/spring-boot/src/main/java/org/baeldung/main/SpringBootActuatorApplication.java +++ /dev/null @@ -1,29 +0,0 @@ -package org.baeldung.main; - -import org.baeldung.service.LoginService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -@EnableAutoConfiguration -@ComponentScan({ "org.baeldung.endpoints", "org.baeldung.service", "org.baeldung.monitor.jmx" }) -public class SpringBootActuatorApplication { - - @Autowired - private LoginService service; - - @RequestMapping("/") - String home() { - service.login("admin", "admin".toCharArray()); - return "TADA!!! You are in Spring Boot Actuator test application."; - } - - public static void main(String[] args) { - SpringApplication.run(SpringBootActuatorApplication.class, args); - } - -} diff --git a/spring-boot/src/main/java/org/baeldung/main/SpringBootApplication.java b/spring-boot/src/main/java/org/baeldung/main/SpringBootApplication.java new file mode 100644 index 0000000000..c71082ba0a --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/main/SpringBootApplication.java @@ -0,0 +1,92 @@ +package org.baeldung.main; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import org.baeldung.common.error.SpringHelloServletRegistrationBean; +import org.baeldung.common.resources.ExecutorServiceExitCodeGenerator; +import org.baeldung.controller.servlet.HelloWorldServlet; +import org.baeldung.controller.servlet.SpringHelloWorldServlet; +import org.baeldung.service.LoginService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@EnableAutoConfiguration +@ComponentScan({ "org.baeldung.common.error", "org.baeldung.common.error.controller", + "org.baeldung.common.properties", "org.baeldung.common.resources", "org.baeldung.endpoints", + "org.baeldung.service", "org.baeldung.monitor.jmx", "org.baeldung.service"}) +public class SpringBootApplication { + + private static ApplicationContext applicationContext; + + @Autowired + private LoginService service; + + @RequestMapping("/") + String home() { + service.login("admin", "admin".toCharArray()); + return "TADA!!! You are in Spring Boot Actuator test application."; + } + + public static void main(String[] args) { + applicationContext = SpringApplication.run(SpringBootApplication.class, args); + } + + @Bean + public ExecutorService executorService() { + return Executors.newFixedThreadPool(10); + } + + @Bean + public HelloWorldServlet helloWorldServlet() { + return new HelloWorldServlet(); + } + + @Bean + public SpringHelloServletRegistrationBean servletRegistrationBean() { + SpringHelloServletRegistrationBean bean = new SpringHelloServletRegistrationBean(new SpringHelloWorldServlet(), "/springHelloWorld/*"); + bean.setLoadOnStartup(1); + bean.addInitParameter("message", "SpringHelloWorldServlet special message"); + return bean; + } + +/* @Bean + public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() { + JettyEmbeddedServletContainerFactory jettyContainer = new JettyEmbeddedServletContainerFactory(); + jettyContainer.setPort(9000); + jettyContainer.setContextPath("/springbootapp"); + return jettyContainer; + } + + @Bean + public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory() { + UndertowEmbeddedServletContainerFactory factory = new UndertowEmbeddedServletContainerFactory(); + factory.addBuilderCustomizers(new UndertowBuilderCustomizer() { + + @Override + public void customize(io.undertow.Undertow.Builde builder) { + builder.addHttpListener(8080, "0.0.0.0"); + } + + }); + return factory; + }*/ + + @Bean + @Autowired + public ExecutorServiceExitCodeGenerator executorServiceExitCodeGenerator(ExecutorService executorService) { + return new ExecutorServiceExitCodeGenerator(executorService); + } + + public void shutDown(ExecutorServiceExitCodeGenerator executorServiceExitCodeGenerator) { + SpringApplication.exit(applicationContext, executorServiceExitCodeGenerator); + } + +} diff --git a/spring-boot/src/main/resources/application.properties b/spring-boot/src/main/resources/application.properties index c876ff740e..8ee0ed29bc 100644 --- a/spring-boot/src/main/resources/application.properties +++ b/spring-boot/src/main/resources/application.properties @@ -1,3 +1,5 @@ +server.port=8083 +server.contextPath=/springbootapp management.port=8081 management.address=127.0.0.1