Skip to content

Commit de5fef0

Browse files
author
Eugen
committed
Merge pull request eugenp#213 from gauravrmazra/master
Common Configuration for Spring Boot Application
2 parents f1fc00a + 9c9351d commit de5fef0

File tree

11 files changed

+316
-29
lines changed

11 files changed

+316
-29
lines changed

spring-boot/pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,23 @@
1919
<dependency>
2020
<groupId>org.springframework.boot</groupId>
2121
<artifactId>spring-boot-starter-web</artifactId>
22+
<!-- <exclusions>
23+
<exclusion>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-tomcat</artifactId>
26+
</exclusion>
27+
</exclusions> -->
2228
</dependency>
29+
30+
<!-- <dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-jetty</artifactId>
33+
</dependency> -->
34+
35+
<!-- <dependency>
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-starter-undertow</artifactId>
38+
</dependency> -->
2339

2440
<dependency>
2541
<groupId>org.springframework.boot</groupId>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.baeldung.common.error;
2+
3+
import org.springframework.boot.autoconfigure.web.ErrorController;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
6+
public class MyCustomErrorController implements ErrorController {
7+
8+
private static final String PATH = "/error";
9+
10+
public MyCustomErrorController() {
11+
// TODO Auto-generated constructor stub
12+
}
13+
14+
@RequestMapping(value=PATH)
15+
public String error() {
16+
return "Error heaven";
17+
}
18+
19+
@Override
20+
public String getErrorPath() {
21+
return PATH;
22+
}
23+
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.baeldung.common.error;
2+
3+
import javax.servlet.Servlet;
4+
5+
import org.springframework.boot.context.embedded.ServletRegistrationBean;
6+
7+
public class SpringHelloServletRegistrationBean extends ServletRegistrationBean {
8+
9+
public SpringHelloServletRegistrationBean() {
10+
}
11+
12+
public SpringHelloServletRegistrationBean(Servlet servlet, String... urlMappings) {
13+
super(servlet, urlMappings);
14+
}
15+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.baeldung.common.error.controller;
2+
3+
import org.springframework.web.bind.annotation.RequestMapping;
4+
import org.springframework.web.bind.annotation.RestController;
5+
6+
@RestController
7+
public class ErrorController {
8+
9+
public ErrorController() {
10+
}
11+
12+
@RequestMapping("/400")
13+
String error400() {
14+
return "Error Code: 400 occured.";
15+
}
16+
17+
@RequestMapping("/errorHeaven")
18+
String errorHeaven() {
19+
return "You have reached the heaven of errors!!!";
20+
}
21+
22+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.baeldung.common.properties;
2+
3+
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
4+
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
5+
import org.springframework.boot.context.embedded.ErrorPage;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.stereotype.Component;
8+
9+
@Component
10+
public class MyServletContainerCustomizationBean implements EmbeddedServletContainerCustomizer {
11+
12+
public MyServletContainerCustomizationBean() {
13+
14+
}
15+
16+
@Override
17+
public void customize(ConfigurableEmbeddedServletContainer container) {
18+
container.setPort(8084);
19+
container.setContextPath("/springbootapp");
20+
21+
container.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/400"));
22+
container.addErrorPages(new ErrorPage("/errorHeaven"));
23+
}
24+
25+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.baeldung.common.resources;
2+
3+
import java.util.Objects;
4+
import java.util.concurrent.ExecutorService;
5+
6+
import org.springframework.boot.ExitCodeGenerator;
7+
8+
public class ExecutorServiceExitCodeGenerator implements ExitCodeGenerator {
9+
10+
private ExecutorService executorService;
11+
12+
public ExecutorServiceExitCodeGenerator(ExecutorService executorService) {
13+
}
14+
15+
@Override
16+
public int getExitCode() {
17+
int returnCode = 0;
18+
try {
19+
if (!Objects.isNull(executorService)) {
20+
executorService.shutdownNow();
21+
returnCode = 1;
22+
}
23+
}
24+
catch (SecurityException ex) {
25+
returnCode = 0;
26+
}
27+
return returnCode;
28+
}
29+
30+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.baeldung.controller.servlet;
2+
3+
import java.io.IOException;
4+
import java.io.PrintWriter;
5+
import java.util.Objects;
6+
7+
import javax.servlet.ServletException;
8+
import javax.servlet.http.HttpServlet;
9+
import javax.servlet.http.HttpServletRequest;
10+
import javax.servlet.http.HttpServletResponse;
11+
12+
public class HelloWorldServlet extends HttpServlet {
13+
private static final long serialVersionUID = 1L;
14+
15+
public HelloWorldServlet() {
16+
super();
17+
}
18+
19+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
20+
PrintWriter out = null;
21+
try {
22+
out = response.getWriter();
23+
out.println("HelloWorldServlet: GET METHOD");
24+
out.flush();
25+
}
26+
finally {
27+
if (!Objects.isNull(out))
28+
out.close();
29+
}
30+
}
31+
32+
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
33+
PrintWriter out = null;
34+
try {
35+
out = response.getWriter();
36+
out.println("HelloWorldServlet: POST METHOD");
37+
out.flush();
38+
}
39+
finally {
40+
if (!Objects.isNull(out))
41+
out.close();
42+
}
43+
}
44+
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.baeldung.controller.servlet;
2+
3+
import java.io.IOException;
4+
import java.io.PrintWriter;
5+
import java.util.Objects;
6+
7+
import javax.servlet.ServletException;
8+
import javax.servlet.http.HttpServlet;
9+
import javax.servlet.http.HttpServletRequest;
10+
import javax.servlet.http.HttpServletResponse;
11+
12+
public class SpringHelloWorldServlet extends HttpServlet {
13+
private static final long serialVersionUID = 1L;
14+
15+
public SpringHelloWorldServlet() {
16+
super();
17+
}
18+
19+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
20+
PrintWriter out = null;
21+
try {
22+
out = response.getWriter();
23+
out.println("SpringHelloWorldServlet: GET METHOD");
24+
out.flush();
25+
}
26+
finally {
27+
if (!Objects.isNull(out))
28+
out.close();
29+
}
30+
}
31+
32+
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
33+
PrintWriter out = null;
34+
try {
35+
out = response.getWriter();
36+
out.println("SpringHelloWorldServlet: POST METHOD");
37+
out.flush();
38+
}
39+
finally {
40+
if (!Objects.isNull(out))
41+
out.close();
42+
}
43+
}
44+
45+
}

spring-boot/src/main/java/org/baeldung/main/SpringBootActuatorApplication.java

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package org.baeldung.main;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
6+
import org.baeldung.common.error.SpringHelloServletRegistrationBean;
7+
import org.baeldung.common.resources.ExecutorServiceExitCodeGenerator;
8+
import org.baeldung.controller.servlet.HelloWorldServlet;
9+
import org.baeldung.controller.servlet.SpringHelloWorldServlet;
10+
import org.baeldung.service.LoginService;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.boot.SpringApplication;
13+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
14+
import org.springframework.context.ApplicationContext;
15+
import org.springframework.context.annotation.Bean;
16+
import org.springframework.context.annotation.ComponentScan;
17+
import org.springframework.web.bind.annotation.RequestMapping;
18+
import org.springframework.web.bind.annotation.RestController;
19+
20+
@RestController
21+
@EnableAutoConfiguration
22+
@ComponentScan({ "org.baeldung.common.error", "org.baeldung.common.error.controller",
23+
"org.baeldung.common.properties", "org.baeldung.common.resources", "org.baeldung.endpoints",
24+
"org.baeldung.service", "org.baeldung.monitor.jmx", "org.baeldung.service"})
25+
public class SpringBootApplication {
26+
27+
private static ApplicationContext applicationContext;
28+
29+
@Autowired
30+
private LoginService service;
31+
32+
@RequestMapping("/")
33+
String home() {
34+
service.login("admin", "admin".toCharArray());
35+
return "TADA!!! You are in Spring Boot Actuator test application.";
36+
}
37+
38+
public static void main(String[] args) {
39+
applicationContext = SpringApplication.run(SpringBootApplication.class, args);
40+
}
41+
42+
@Bean
43+
public ExecutorService executorService() {
44+
return Executors.newFixedThreadPool(10);
45+
}
46+
47+
@Bean
48+
public HelloWorldServlet helloWorldServlet() {
49+
return new HelloWorldServlet();
50+
}
51+
52+
@Bean
53+
public SpringHelloServletRegistrationBean servletRegistrationBean() {
54+
SpringHelloServletRegistrationBean bean = new SpringHelloServletRegistrationBean(new SpringHelloWorldServlet(), "/springHelloWorld/*");
55+
bean.setLoadOnStartup(1);
56+
bean.addInitParameter("message", "SpringHelloWorldServlet special message");
57+
return bean;
58+
}
59+
60+
/* @Bean
61+
public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() {
62+
JettyEmbeddedServletContainerFactory jettyContainer = new JettyEmbeddedServletContainerFactory();
63+
jettyContainer.setPort(9000);
64+
jettyContainer.setContextPath("/springbootapp");
65+
return jettyContainer;
66+
}
67+
68+
@Bean
69+
public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory() {
70+
UndertowEmbeddedServletContainerFactory factory = new UndertowEmbeddedServletContainerFactory();
71+
factory.addBuilderCustomizers(new UndertowBuilderCustomizer() {
72+
73+
@Override
74+
public void customize(io.undertow.Undertow.Builde builder) {
75+
builder.addHttpListener(8080, "0.0.0.0");
76+
}
77+
78+
});
79+
return factory;
80+
}*/
81+
82+
@Bean
83+
@Autowired
84+
public ExecutorServiceExitCodeGenerator executorServiceExitCodeGenerator(ExecutorService executorService) {
85+
return new ExecutorServiceExitCodeGenerator(executorService);
86+
}
87+
88+
public void shutDown(ExecutorServiceExitCodeGenerator executorServiceExitCodeGenerator) {
89+
SpringApplication.exit(applicationContext, executorServiceExitCodeGenerator);
90+
}
91+
92+
}

0 commit comments

Comments
 (0)