Merge pull request #213 from gauravrmazra/master

Common Configuration for Spring Boot Application
This commit is contained in:
Eugen 2015-06-03 10:28:34 +01:00
commit de5fef0131
11 changed files with 316 additions and 29 deletions

View File

@ -19,8 +19,24 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- <exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions> -->
</dependency>
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency> -->
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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!!!";
}
}

View File

@ -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"));
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -1,3 +1,5 @@
server.port=8083
server.contextPath=/springbootapp
management.port=8081
management.address=127.0.0.1