From b5e0e559e370178199efda67e5509d60bdecac64 Mon Sep 17 00:00:00 2001 From: sharifi Date: Sat, 3 Oct 2020 11:32:38 +0330 Subject: [PATCH 01/10] add new module to parent pom --- spring-boot-modules/pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-boot-modules/pom.xml b/spring-boot-modules/pom.xml index fa70a9f058..19dfcbb962 100644 --- a/spring-boot-modules/pom.xml +++ b/spring-boot-modules/pom.xml @@ -26,6 +26,7 @@ spring-boot-artifacts spring-boot-autoconfiguration spring-boot-basic-customization + spring-boot-basic-customization-2 spring-boot-bootstrap spring-boot-client spring-boot-config-jpa-error From 7e56ea33036c8748faa9748244cc7a01ba7c9e24 Mon Sep 17 00:00:00 2001 From: sharifi Date: Sat, 3 Oct 2020 11:33:33 +0330 Subject: [PATCH 02/10] add README file --- .../spring-boot-basic-customization-2/README.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 spring-boot-modules/spring-boot-basic-customization-2/README.md diff --git a/spring-boot-modules/spring-boot-basic-customization-2/README.md b/spring-boot-modules/spring-boot-basic-customization-2/README.md new file mode 100644 index 0000000000..faaee0962e --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization-2/README.md @@ -0,0 +1,7 @@ +## Spring Boot Basic Customization 2 + +This module contains articles about Spring Boot customization 2 + +### Relevant Articles: + + - [DispatcherServlet and web.xml in Spring Boot](https://www.baeldung.com/) From 7ffa8ce6a521c5570b305469f8fd7220f1f3e089 Mon Sep 17 00:00:00 2001 From: sharifi Date: Sat, 3 Oct 2020 11:33:57 +0330 Subject: [PATCH 03/10] add gitignore file --- .../.gitignore | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 spring-boot-modules/spring-boot-basic-customization-2/.gitignore diff --git a/spring-boot-modules/spring-boot-basic-customization-2/.gitignore b/spring-boot-modules/spring-boot-basic-customization-2/.gitignore new file mode 100644 index 0000000000..2af7cefb0a --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization-2/.gitignore @@ -0,0 +1,24 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +nbproject/private/ +build/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ \ No newline at end of file From 17d3411f7c61ef40446e397b55a6f2cd4ed9dc55 Mon Sep 17 00:00:00 2001 From: sharifi Date: Sat, 3 Oct 2020 11:35:16 +0330 Subject: [PATCH 04/10] add main source --- .../spring-boot-basic-customization-2/pom.xml | 33 +++++++++++++++++++ .../com.baeldung.demo/DemoApplication.java | 15 +++++++++ .../java/com.baeldung.demo/conf/WebConf.java | 29 ++++++++++++++++ .../controller/Controller.java | 15 +++++++++ .../filter/CustomFilter.java | 30 +++++++++++++++++ .../listener/CustomListener.java | 22 +++++++++++++ .../servlet/CustomServlet.java | 29 ++++++++++++++++ .../src/main/resources/application.properties | 2 ++ 8 files changed, 175 insertions(+) create mode 100644 spring-boot-modules/spring-boot-basic-customization-2/pom.xml create mode 100644 spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/DemoApplication.java create mode 100644 spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/conf/WebConf.java create mode 100644 spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/controller/Controller.java create mode 100644 spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/filter/CustomFilter.java create mode 100644 spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/listener/CustomListener.java create mode 100644 spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/servlet/CustomServlet.java create mode 100644 spring-boot-modules/spring-boot-basic-customization-2/src/main/resources/application.properties diff --git a/spring-boot-modules/spring-boot-basic-customization-2/pom.xml b/spring-boot-modules/spring-boot-basic-customization-2/pom.xml new file mode 100644 index 0000000000..3ce9266ebe --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization-2/pom.xml @@ -0,0 +1,33 @@ + + + 4.0.0 + + + com.baeldung.spring-boot-modules + spring-boot-modules + 1.0.0-SNAPSHOT + ../ + + + spring-boot-basic-customization-2 + jar + + spring-boot-basic-customization-2 + Module For Spring Boot Basic Customization 2 + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + diff --git a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/DemoApplication.java b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/DemoApplication.java new file mode 100644 index 0000000000..9e8148f043 --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/DemoApplication.java @@ -0,0 +1,15 @@ +package com.baeldung.demo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.servlet.ServletComponentScan; +import org.springframework.context.annotation.Configuration; + +@SpringBootApplication +public class DemoApplication { + + public static void main(String[] args) { + SpringApplication.run(DemoApplication.class, args); + } + +} diff --git a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/conf/WebConf.java b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/conf/WebConf.java new file mode 100644 index 0000000000..02fcf152f1 --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/conf/WebConf.java @@ -0,0 +1,29 @@ +package com.baeldung.demo.conf; + +import com.baeldung.demo.listener.CustomListener; +import com.baeldung.demo.servlet.CustomServlet; +import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; +import org.springframework.boot.web.servlet.ServletRegistrationBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import javax.servlet.ServletContextListener; + +@Configuration +public class WebConf { + + @Bean + public ServletRegistrationBean customServletBean() { + ServletRegistrationBean bean = + new ServletRegistrationBean(new CustomServlet(), "/servlet"); + return bean; + } + + @Bean + public ServletListenerRegistrationBean customListenerBean() { + ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean(); + bean.setListener(new CustomListener()); + return bean; + } + +} diff --git a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/controller/Controller.java b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/controller/Controller.java new file mode 100644 index 0000000000..a6d1812511 --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/controller/Controller.java @@ -0,0 +1,15 @@ +package com.baeldung.demo.controller; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping(value = "/") +public class Controller { + + @GetMapping + public String getRequest(){ + return "Baeldung DispatcherServlet"; + } +} diff --git a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/filter/CustomFilter.java b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/filter/CustomFilter.java new file mode 100644 index 0000000000..c661aecf6d --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/filter/CustomFilter.java @@ -0,0 +1,30 @@ +package com.baeldung.demo.filter; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import javax.servlet.*; +import java.io.IOException; + +@Component +public class CustomFilter implements Filter { + + Logger logger = LoggerFactory.getLogger(CustomFilter.class); + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + logger.info("CustomFilter is invoked"); + chain.doFilter(request, response); + } + + @Override + public void destroy() { + + } +} diff --git a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/listener/CustomListener.java b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/listener/CustomListener.java new file mode 100644 index 0000000000..a9e3ad680f --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/listener/CustomListener.java @@ -0,0 +1,22 @@ +package com.baeldung.demo.listener; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; + +public class CustomListener implements ServletContextListener { + + Logger logger = LoggerFactory.getLogger(CustomListener.class); + + @Override + public void contextInitialized(ServletContextEvent sce) { + logger.info("CustomListener is initialized"); + } + + @Override + public void contextDestroyed(ServletContextEvent sce) { + logger.info("CustomListener is destroyed"); + } +} diff --git a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/servlet/CustomServlet.java b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/servlet/CustomServlet.java new file mode 100644 index 0000000000..fd3e92bedc --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/servlet/CustomServlet.java @@ -0,0 +1,29 @@ +package com.baeldung.demo.servlet; + +import com.baeldung.demo.filter.CustomFilter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +public class CustomServlet extends HttpServlet { + + Logger logger = LoggerFactory.getLogger(CustomServlet.class); + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + logger.info("CustomServlet doGet() method is invoked"); + super.doGet(req, resp); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + logger.info("CustomServlet doPost() method is invoked"); + super.doPost(req, resp); + } +} diff --git a/spring-boot-modules/spring-boot-basic-customization-2/src/main/resources/application.properties b/spring-boot-modules/spring-boot-basic-customization-2/src/main/resources/application.properties new file mode 100644 index 0000000000..8c4690dedc --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization-2/src/main/resources/application.properties @@ -0,0 +1,2 @@ +server.servlet.context-path=/demo +spring.mvc.servlet.path=/baeldung \ No newline at end of file From da6fb652fbf0c732dd7a0a4d34736a15d06aa343 Mon Sep 17 00:00:00 2001 From: sharifi Date: Fri, 9 Oct 2020 11:36:07 +0330 Subject: [PATCH 05/10] remove gitignore file --- .../.gitignore | 24 ------------------- 1 file changed, 24 deletions(-) delete mode 100644 spring-boot-modules/spring-boot-basic-customization-2/.gitignore diff --git a/spring-boot-modules/spring-boot-basic-customization-2/.gitignore b/spring-boot-modules/spring-boot-basic-customization-2/.gitignore deleted file mode 100644 index 2af7cefb0a..0000000000 --- a/spring-boot-modules/spring-boot-basic-customization-2/.gitignore +++ /dev/null @@ -1,24 +0,0 @@ -target/ -!.mvn/wrapper/maven-wrapper.jar - -### STS ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans - -### IntelliJ IDEA ### -.idea -*.iws -*.iml -*.ipr - -### NetBeans ### -nbproject/private/ -build/ -nbbuild/ -dist/ -nbdist/ -.nb-gradle/ \ No newline at end of file From 5cb3bc85bea94ab6414df0e218ec0c7898f27d55 Mon Sep 17 00:00:00 2001 From: sharifi Date: Fri, 9 Oct 2020 11:48:10 +0330 Subject: [PATCH 06/10] change package name and main class's name --- .../DispatchServletApplication.java} | 6 +++--- .../conf/WebConf.java | 6 +++--- .../controller/Controller.java | 2 +- .../filter/CustomFilter.java | 2 +- .../listener/CustomListener.java | 2 +- .../servlet/CustomServlet.java | 4 ++-- 6 files changed, 11 insertions(+), 11 deletions(-) rename spring-boot-modules/spring-boot-basic-customization-2/src/main/java/{com.baeldung.demo/DemoApplication.java => com.baeldung.dispatchservlet/DispatchServletApplication.java} (68%) rename spring-boot-modules/spring-boot-basic-customization-2/src/main/java/{com.baeldung.demo => com.baeldung.dispatchservlet}/conf/WebConf.java (83%) rename spring-boot-modules/spring-boot-basic-customization-2/src/main/java/{com.baeldung.demo => com.baeldung.dispatchservlet}/controller/Controller.java (88%) rename spring-boot-modules/spring-boot-basic-customization-2/src/main/java/{com.baeldung.demo => com.baeldung.dispatchservlet}/filter/CustomFilter.java (93%) rename spring-boot-modules/spring-boot-basic-customization-2/src/main/java/{com.baeldung.demo => com.baeldung.dispatchservlet}/listener/CustomListener.java (92%) rename spring-boot-modules/spring-boot-basic-customization-2/src/main/java/{com.baeldung.demo => com.baeldung.dispatchservlet}/servlet/CustomServlet.java (89%) diff --git a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/DemoApplication.java b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/DispatchServletApplication.java similarity index 68% rename from spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/DemoApplication.java rename to spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/DispatchServletApplication.java index 9e8148f043..4d58715d88 100644 --- a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/DemoApplication.java +++ b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/DispatchServletApplication.java @@ -1,4 +1,4 @@ -package com.baeldung.demo; +package com.baeldung.dispatchservlet; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -6,10 +6,10 @@ import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.context.annotation.Configuration; @SpringBootApplication -public class DemoApplication { +public class DispatchServletApplication { public static void main(String[] args) { - SpringApplication.run(DemoApplication.class, args); + SpringApplication.run(DispatchServletApplication.class, args); } } diff --git a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/conf/WebConf.java b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/conf/WebConf.java similarity index 83% rename from spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/conf/WebConf.java rename to spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/conf/WebConf.java index 02fcf152f1..9a1170ca34 100644 --- a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/conf/WebConf.java +++ b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/conf/WebConf.java @@ -1,7 +1,7 @@ -package com.baeldung.demo.conf; +package com.baeldung.dispatchservlet.conf; -import com.baeldung.demo.listener.CustomListener; -import com.baeldung.demo.servlet.CustomServlet; +import com.baeldung.dispatchservlet.listener.CustomListener; +import com.baeldung.dispatchservlet.servlet.CustomServlet; import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; diff --git a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/controller/Controller.java b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/controller/Controller.java similarity index 88% rename from spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/controller/Controller.java rename to spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/controller/Controller.java index a6d1812511..14d71c60fb 100644 --- a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/controller/Controller.java +++ b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/controller/Controller.java @@ -1,4 +1,4 @@ -package com.baeldung.demo.controller; +package com.baeldung.dispatchservlet.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; diff --git a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/filter/CustomFilter.java b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/filter/CustomFilter.java similarity index 93% rename from spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/filter/CustomFilter.java rename to spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/filter/CustomFilter.java index c661aecf6d..8429fc855f 100644 --- a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/filter/CustomFilter.java +++ b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/filter/CustomFilter.java @@ -1,4 +1,4 @@ -package com.baeldung.demo.filter; +package com.baeldung.dispatchservlet.filter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/listener/CustomListener.java b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/listener/CustomListener.java similarity index 92% rename from spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/listener/CustomListener.java rename to spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/listener/CustomListener.java index a9e3ad680f..62b316c012 100644 --- a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/listener/CustomListener.java +++ b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/listener/CustomListener.java @@ -1,4 +1,4 @@ -package com.baeldung.demo.listener; +package com.baeldung.dispatchservlet.listener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/servlet/CustomServlet.java b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/servlet/CustomServlet.java similarity index 89% rename from spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/servlet/CustomServlet.java rename to spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/servlet/CustomServlet.java index fd3e92bedc..2a99e797ce 100644 --- a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.demo/servlet/CustomServlet.java +++ b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/servlet/CustomServlet.java @@ -1,6 +1,6 @@ -package com.baeldung.demo.servlet; +package com.baeldung.dispatchservlet.servlet; -import com.baeldung.demo.filter.CustomFilter; +import com.baeldung.dispatchservlet.filter.CustomFilter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; From 271ad4e3c7224b1ea65fe66146767622598832fe Mon Sep 17 00:00:00 2001 From: sharifi Date: Fri, 9 Oct 2020 11:48:57 +0330 Subject: [PATCH 07/10] remove context-path and path --- .../src/main/resources/application.properties | 2 -- 1 file changed, 2 deletions(-) diff --git a/spring-boot-modules/spring-boot-basic-customization-2/src/main/resources/application.properties b/spring-boot-modules/spring-boot-basic-customization-2/src/main/resources/application.properties index 8c4690dedc..e69de29bb2 100644 --- a/spring-boot-modules/spring-boot-basic-customization-2/src/main/resources/application.properties +++ b/spring-boot-modules/spring-boot-basic-customization-2/src/main/resources/application.properties @@ -1,2 +0,0 @@ -server.servlet.context-path=/demo -spring.mvc.servlet.path=/baeldung \ No newline at end of file From f0b9b3db29ccb0b59b7cbefb219c0584a06278bd Mon Sep 17 00:00:00 2001 From: sharifi Date: Mon, 12 Oct 2020 23:07:48 +0330 Subject: [PATCH 08/10] separate directories --- .../DispatchServletApplication.java | 15 ++++++++++ .../dispatchservlet/conf/WebConf.java | 29 ++++++++++++++++++ .../controller/Controller.java | 15 ++++++++++ .../dispatchservlet/filter/CustomFilter.java | 30 +++++++++++++++++++ .../listener/CustomListener.java | 22 ++++++++++++++ .../servlet/CustomServlet.java | 29 ++++++++++++++++++ 6 files changed, 140 insertions(+) create mode 100644 spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com/baeldung/dispatchservlet/DispatchServletApplication.java create mode 100644 spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com/baeldung/dispatchservlet/conf/WebConf.java create mode 100644 spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com/baeldung/dispatchservlet/controller/Controller.java create mode 100644 spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com/baeldung/dispatchservlet/filter/CustomFilter.java create mode 100644 spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com/baeldung/dispatchservlet/listener/CustomListener.java create mode 100644 spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com/baeldung/dispatchservlet/servlet/CustomServlet.java diff --git a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com/baeldung/dispatchservlet/DispatchServletApplication.java b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com/baeldung/dispatchservlet/DispatchServletApplication.java new file mode 100644 index 0000000000..4d58715d88 --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com/baeldung/dispatchservlet/DispatchServletApplication.java @@ -0,0 +1,15 @@ +package com.baeldung.dispatchservlet; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.servlet.ServletComponentScan; +import org.springframework.context.annotation.Configuration; + +@SpringBootApplication +public class DispatchServletApplication { + + public static void main(String[] args) { + SpringApplication.run(DispatchServletApplication.class, args); + } + +} diff --git a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com/baeldung/dispatchservlet/conf/WebConf.java b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com/baeldung/dispatchservlet/conf/WebConf.java new file mode 100644 index 0000000000..9a1170ca34 --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com/baeldung/dispatchservlet/conf/WebConf.java @@ -0,0 +1,29 @@ +package com.baeldung.dispatchservlet.conf; + +import com.baeldung.dispatchservlet.listener.CustomListener; +import com.baeldung.dispatchservlet.servlet.CustomServlet; +import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; +import org.springframework.boot.web.servlet.ServletRegistrationBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import javax.servlet.ServletContextListener; + +@Configuration +public class WebConf { + + @Bean + public ServletRegistrationBean customServletBean() { + ServletRegistrationBean bean = + new ServletRegistrationBean(new CustomServlet(), "/servlet"); + return bean; + } + + @Bean + public ServletListenerRegistrationBean customListenerBean() { + ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean(); + bean.setListener(new CustomListener()); + return bean; + } + +} diff --git a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com/baeldung/dispatchservlet/controller/Controller.java b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com/baeldung/dispatchservlet/controller/Controller.java new file mode 100644 index 0000000000..14d71c60fb --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com/baeldung/dispatchservlet/controller/Controller.java @@ -0,0 +1,15 @@ +package com.baeldung.dispatchservlet.controller; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping(value = "/") +public class Controller { + + @GetMapping + public String getRequest(){ + return "Baeldung DispatcherServlet"; + } +} diff --git a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com/baeldung/dispatchservlet/filter/CustomFilter.java b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com/baeldung/dispatchservlet/filter/CustomFilter.java new file mode 100644 index 0000000000..8429fc855f --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com/baeldung/dispatchservlet/filter/CustomFilter.java @@ -0,0 +1,30 @@ +package com.baeldung.dispatchservlet.filter; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import javax.servlet.*; +import java.io.IOException; + +@Component +public class CustomFilter implements Filter { + + Logger logger = LoggerFactory.getLogger(CustomFilter.class); + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + logger.info("CustomFilter is invoked"); + chain.doFilter(request, response); + } + + @Override + public void destroy() { + + } +} diff --git a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com/baeldung/dispatchservlet/listener/CustomListener.java b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com/baeldung/dispatchservlet/listener/CustomListener.java new file mode 100644 index 0000000000..62b316c012 --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com/baeldung/dispatchservlet/listener/CustomListener.java @@ -0,0 +1,22 @@ +package com.baeldung.dispatchservlet.listener; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; + +public class CustomListener implements ServletContextListener { + + Logger logger = LoggerFactory.getLogger(CustomListener.class); + + @Override + public void contextInitialized(ServletContextEvent sce) { + logger.info("CustomListener is initialized"); + } + + @Override + public void contextDestroyed(ServletContextEvent sce) { + logger.info("CustomListener is destroyed"); + } +} diff --git a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com/baeldung/dispatchservlet/servlet/CustomServlet.java b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com/baeldung/dispatchservlet/servlet/CustomServlet.java new file mode 100644 index 0000000000..2a99e797ce --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com/baeldung/dispatchservlet/servlet/CustomServlet.java @@ -0,0 +1,29 @@ +package com.baeldung.dispatchservlet.servlet; + +import com.baeldung.dispatchservlet.filter.CustomFilter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +public class CustomServlet extends HttpServlet { + + Logger logger = LoggerFactory.getLogger(CustomServlet.class); + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + logger.info("CustomServlet doGet() method is invoked"); + super.doGet(req, resp); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + logger.info("CustomServlet doPost() method is invoked"); + super.doPost(req, resp); + } +} From eeadbb04f0521eb0db15f908be861320a04ad8f7 Mon Sep 17 00:00:00 2001 From: sharifi Date: Wed, 14 Oct 2020 15:32:29 +0330 Subject: [PATCH 09/10] delete the original classes related to separate the directories --- .../DispatchServletApplication.java | 15 ---------- .../conf/WebConf.java | 29 ------------------ .../controller/Controller.java | 15 ---------- .../filter/CustomFilter.java | 30 ------------------- .../listener/CustomListener.java | 22 -------------- .../servlet/CustomServlet.java | 29 ------------------ 6 files changed, 140 deletions(-) delete mode 100644 spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/DispatchServletApplication.java delete mode 100644 spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/conf/WebConf.java delete mode 100644 spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/controller/Controller.java delete mode 100644 spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/filter/CustomFilter.java delete mode 100644 spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/listener/CustomListener.java delete mode 100644 spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/servlet/CustomServlet.java diff --git a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/DispatchServletApplication.java b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/DispatchServletApplication.java deleted file mode 100644 index 4d58715d88..0000000000 --- a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/DispatchServletApplication.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.baeldung.dispatchservlet; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.web.servlet.ServletComponentScan; -import org.springframework.context.annotation.Configuration; - -@SpringBootApplication -public class DispatchServletApplication { - - public static void main(String[] args) { - SpringApplication.run(DispatchServletApplication.class, args); - } - -} diff --git a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/conf/WebConf.java b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/conf/WebConf.java deleted file mode 100644 index 9a1170ca34..0000000000 --- a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/conf/WebConf.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.baeldung.dispatchservlet.conf; - -import com.baeldung.dispatchservlet.listener.CustomListener; -import com.baeldung.dispatchservlet.servlet.CustomServlet; -import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; -import org.springframework.boot.web.servlet.ServletRegistrationBean; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import javax.servlet.ServletContextListener; - -@Configuration -public class WebConf { - - @Bean - public ServletRegistrationBean customServletBean() { - ServletRegistrationBean bean = - new ServletRegistrationBean(new CustomServlet(), "/servlet"); - return bean; - } - - @Bean - public ServletListenerRegistrationBean customListenerBean() { - ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean(); - bean.setListener(new CustomListener()); - return bean; - } - -} diff --git a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/controller/Controller.java b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/controller/Controller.java deleted file mode 100644 index 14d71c60fb..0000000000 --- a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/controller/Controller.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.baeldung.dispatchservlet.controller; - -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -@RequestMapping(value = "/") -public class Controller { - - @GetMapping - public String getRequest(){ - return "Baeldung DispatcherServlet"; - } -} diff --git a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/filter/CustomFilter.java b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/filter/CustomFilter.java deleted file mode 100644 index 8429fc855f..0000000000 --- a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/filter/CustomFilter.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.baeldung.dispatchservlet.filter; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; - -import javax.servlet.*; -import java.io.IOException; - -@Component -public class CustomFilter implements Filter { - - Logger logger = LoggerFactory.getLogger(CustomFilter.class); - - @Override - public void init(FilterConfig filterConfig) throws ServletException { - - } - - @Override - public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { - logger.info("CustomFilter is invoked"); - chain.doFilter(request, response); - } - - @Override - public void destroy() { - - } -} diff --git a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/listener/CustomListener.java b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/listener/CustomListener.java deleted file mode 100644 index 62b316c012..0000000000 --- a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/listener/CustomListener.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.baeldung.dispatchservlet.listener; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.servlet.ServletContextEvent; -import javax.servlet.ServletContextListener; - -public class CustomListener implements ServletContextListener { - - Logger logger = LoggerFactory.getLogger(CustomListener.class); - - @Override - public void contextInitialized(ServletContextEvent sce) { - logger.info("CustomListener is initialized"); - } - - @Override - public void contextDestroyed(ServletContextEvent sce) { - logger.info("CustomListener is destroyed"); - } -} diff --git a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/servlet/CustomServlet.java b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/servlet/CustomServlet.java deleted file mode 100644 index 2a99e797ce..0000000000 --- a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com.baeldung.dispatchservlet/servlet/CustomServlet.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.baeldung.dispatchservlet.servlet; - -import com.baeldung.dispatchservlet.filter.CustomFilter; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.servlet.ServletException; -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; - -public class CustomServlet extends HttpServlet { - - Logger logger = LoggerFactory.getLogger(CustomServlet.class); - - @Override - protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - logger.info("CustomServlet doGet() method is invoked"); - super.doGet(req, resp); - } - - @Override - protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - logger.info("CustomServlet doPost() method is invoked"); - super.doPost(req, resp); - } -} From d790913cbd2d9355e77583dbe379bb1bcb69af9f Mon Sep 17 00:00:00 2001 From: sharifi Date: Sat, 24 Oct 2020 17:51:06 +0330 Subject: [PATCH 10/10] improve code quality --- .../main/java/com/baeldung/dispatchservlet/conf/WebConf.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com/baeldung/dispatchservlet/conf/WebConf.java b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com/baeldung/dispatchservlet/conf/WebConf.java index 9a1170ca34..7c52b117fd 100644 --- a/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com/baeldung/dispatchservlet/conf/WebConf.java +++ b/spring-boot-modules/spring-boot-basic-customization-2/src/main/java/com/baeldung/dispatchservlet/conf/WebConf.java @@ -14,8 +14,8 @@ public class WebConf { @Bean public ServletRegistrationBean customServletBean() { - ServletRegistrationBean bean = - new ServletRegistrationBean(new CustomServlet(), "/servlet"); + ServletRegistrationBean bean + = new ServletRegistrationBean(new CustomServlet(), "/servlet"); return bean; }