diff --git a/spring-all/src/main/java/com/baeldung/contexts/Greeting.java b/spring-all/src/main/java/com/baeldung/contexts/Greeting.java new file mode 100644 index 0000000000..99b473c6c4 --- /dev/null +++ b/spring-all/src/main/java/com/baeldung/contexts/Greeting.java @@ -0,0 +1,19 @@ +package com.baeldung.contexts; + +public class Greeting { + + private String message; + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + @Override + public String toString() { + return "Greeting [message=" + message + "]"; + } +} diff --git a/spring-all/src/main/java/com/baeldung/contexts/config/ApplicationInitializer.java b/spring-all/src/main/java/com/baeldung/contexts/config/ApplicationInitializer.java new file mode 100644 index 0000000000..c3ff90cf39 --- /dev/null +++ b/spring-all/src/main/java/com/baeldung/contexts/config/ApplicationInitializer.java @@ -0,0 +1,35 @@ +package com.baeldung.contexts.config; + +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletRegistration; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.WebApplicationInitializer; +import org.springframework.web.context.ContextLoaderListener; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; +import org.springframework.web.servlet.DispatcherServlet; + +@Configuration +public class ApplicationInitializer implements WebApplicationInitializer { + + @Override + public void onStartup(ServletContext servletContext) throws ServletException { + AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); + rootContext.register(RootApplicationConfig.class); + servletContext.addListener(new ContextLoaderListener(rootContext)); + + AnnotationConfigWebApplicationContext normalWebAppContext = new AnnotationConfigWebApplicationContext(); + normalWebAppContext.register(NormalWebAppConfig.class); + ServletRegistration.Dynamic normal = servletContext.addServlet("normal-webapp", new DispatcherServlet(normalWebAppContext)); + normal.setLoadOnStartup(1); + normal.addMapping("/api/*"); + + AnnotationConfigWebApplicationContext secureWebAppContext = new AnnotationConfigWebApplicationContext(); + secureWebAppContext.register(SecureWebAppConfig.class); + ServletRegistration.Dynamic secure = servletContext.addServlet("secure-webapp", new DispatcherServlet(secureWebAppContext)); + secure.setLoadOnStartup(1); + secure.addMapping("/s/api/*"); + } + +} diff --git a/spring-all/src/main/java/com/baeldung/contexts/config/NormalWebAppConfig.java b/spring-all/src/main/java/com/baeldung/contexts/config/NormalWebAppConfig.java new file mode 100644 index 0000000000..c250cedc49 --- /dev/null +++ b/spring-all/src/main/java/com/baeldung/contexts/config/NormalWebAppConfig.java @@ -0,0 +1,25 @@ +package com.baeldung.contexts.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.view.InternalResourceViewResolver; +import org.springframework.web.servlet.view.JstlView; + +@Configuration +@EnableWebMvc +@ComponentScan(basePackages = { "com.baeldung.contexts.normal" }) +public class NormalWebAppConfig extends WebMvcConfigurerAdapter { + + @Bean + public ViewResolver viewResolver() { + InternalResourceViewResolver resolver = new InternalResourceViewResolver(); + resolver.setPrefix("/WEB-INF/view/"); + resolver.setSuffix(".jsp"); + resolver.setViewClass(JstlView.class); + return resolver; + } +} diff --git a/spring-all/src/main/java/com/baeldung/contexts/config/RootApplicationConfig.java b/spring-all/src/main/java/com/baeldung/contexts/config/RootApplicationConfig.java new file mode 100644 index 0000000000..59821076d2 --- /dev/null +++ b/spring-all/src/main/java/com/baeldung/contexts/config/RootApplicationConfig.java @@ -0,0 +1,19 @@ +package com.baeldung.contexts.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +import com.baeldung.contexts.Greeting; + +@Configuration +@ComponentScan(basePackages = { "com.baeldung.contexts.services" }) +public class RootApplicationConfig { + + @Bean + public Greeting greeting() { + Greeting greeting = new Greeting(); + greeting.setMessage("Hello World !!"); + return greeting; + } +} diff --git a/spring-all/src/main/java/com/baeldung/contexts/config/SecureWebAppConfig.java b/spring-all/src/main/java/com/baeldung/contexts/config/SecureWebAppConfig.java new file mode 100644 index 0000000000..f499a4ceba --- /dev/null +++ b/spring-all/src/main/java/com/baeldung/contexts/config/SecureWebAppConfig.java @@ -0,0 +1,25 @@ +package com.baeldung.contexts.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.view.InternalResourceViewResolver; +import org.springframework.web.servlet.view.JstlView; + +@Configuration +@EnableWebMvc +@ComponentScan(basePackages = { "com.baeldung.contexts.secure" }) +public class SecureWebAppConfig extends WebMvcConfigurerAdapter { + + @Bean + public ViewResolver viewResolver() { + InternalResourceViewResolver resolver = new InternalResourceViewResolver(); + resolver.setPrefix("/WEB-INF/secure/view/"); + resolver.setSuffix(".jsp"); + resolver.setViewClass(JstlView.class); + return resolver; + } +} diff --git a/spring-all/src/main/java/com/baeldung/contexts/normal/HelloWorldController.java b/spring-all/src/main/java/com/baeldung/contexts/normal/HelloWorldController.java new file mode 100644 index 0000000000..dd70ce8e97 --- /dev/null +++ b/spring-all/src/main/java/com/baeldung/contexts/normal/HelloWorldController.java @@ -0,0 +1,41 @@ +package com.baeldung.contexts.normal; + +import java.util.Arrays; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.context.ContextLoader; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.servlet.ModelAndView; + +import com.baeldung.contexts.services.ApplicationContextUtilService; +import com.baeldung.contexts.services.GreeterService; + +@Controller +public class HelloWorldController { + + @Autowired + WebApplicationContext webApplicationContext; + + @Autowired + private GreeterService greeterService; + + private void processContext() { + WebApplicationContext rootContext = ContextLoader.getCurrentWebApplicationContext(); + + System.out.println("root context : " + rootContext); + System.out.println("root context Beans: " + Arrays.asList(rootContext.getBeanDefinitionNames())); + + System.out.println("context : " + webApplicationContext); + System.out.println("context Beans: " + Arrays.asList(webApplicationContext.getBeanDefinitionNames())); + } + + @RequestMapping(path = "/welcome") + public ModelAndView helloWorld() { + processContext(); + String message = "
" + "

" + greeterService.greet() + "

"; + return new ModelAndView("welcome", "message", message); + } +} diff --git a/spring-all/src/main/java/com/baeldung/contexts/secure/HelloWorldSecureController.java b/spring-all/src/main/java/com/baeldung/contexts/secure/HelloWorldSecureController.java new file mode 100644 index 0000000000..b46ace91ae --- /dev/null +++ b/spring-all/src/main/java/com/baeldung/contexts/secure/HelloWorldSecureController.java @@ -0,0 +1,49 @@ +package com.baeldung.contexts.secure; + +import java.util.Arrays; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.context.ContextLoader; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.servlet.ModelAndView; + +import com.baeldung.contexts.services.ApplicationContextUtilService; +import com.baeldung.contexts.services.GreeterService; + +@Controller +public class HelloWorldSecureController { + + @Autowired + WebApplicationContext webApplicationContext; + + @Autowired + private GreeterService greeterService; + + @Autowired + @Qualifier("contextAware") + private ApplicationContextUtilService contextUtilService; + + private void processContext() { + ApplicationContext context = contextUtilService.getApplicationContext(); + System.out.println("application context : " + context); + System.out.println("application context Beans: " + Arrays.asList(context.getBeanDefinitionNames())); + + WebApplicationContext rootContext = ContextLoader.getCurrentWebApplicationContext(); + System.out.println("context : " + rootContext); + System.out.println("context Beans: " + Arrays.asList(rootContext.getBeanDefinitionNames())); + + System.out.println("context : " + webApplicationContext); + System.out.println("context Beans: " + Arrays.asList(webApplicationContext.getBeanDefinitionNames())); + } + + @RequestMapping(path = "/welcome") + public ModelAndView helloWorld() { + processContext(); + String message = "
" + "

" + greeterService.greet() + "

"; + return new ModelAndView("welcome", "message", message); + } +} diff --git a/spring-all/src/main/java/com/baeldung/contexts/services/ApplicationContextUtilService.java b/spring-all/src/main/java/com/baeldung/contexts/services/ApplicationContextUtilService.java new file mode 100644 index 0000000000..332cb10620 --- /dev/null +++ b/spring-all/src/main/java/com/baeldung/contexts/services/ApplicationContextUtilService.java @@ -0,0 +1,21 @@ +package com.baeldung.contexts.services; + +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Service; + +@Service(value="contextAware") +public class ApplicationContextUtilService implements ApplicationContextAware { + + private ApplicationContext applicationContext; + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } + + public ApplicationContext getApplicationContext() { + return applicationContext; + } +} diff --git a/spring-all/src/main/java/com/baeldung/contexts/services/GreeterService.java b/spring-all/src/main/java/com/baeldung/contexts/services/GreeterService.java new file mode 100644 index 0000000000..f68b2fe5af --- /dev/null +++ b/spring-all/src/main/java/com/baeldung/contexts/services/GreeterService.java @@ -0,0 +1,19 @@ +package com.baeldung.contexts.services; + +import javax.annotation.Resource; + +import org.springframework.stereotype.Service; + +import com.baeldung.contexts.Greeting; + +@Service +public class GreeterService { + + @Resource + private Greeting greeting; + + public String greet(){ + return greeting.getMessage(); + } + +} diff --git a/spring-all/src/main/webapp/WEB-INF/index.jsp b/spring-all/src/main/webapp/WEB-INF/index.jsp new file mode 100644 index 0000000000..c38169bb95 --- /dev/null +++ b/spring-all/src/main/webapp/WEB-INF/index.jsp @@ -0,0 +1,5 @@ + + +

Hello World!

+ + diff --git a/spring-all/src/main/webapp/WEB-INF/normal-webapp-servlet.xml b/spring-all/src/main/webapp/WEB-INF/normal-webapp-servlet.xml new file mode 100644 index 0000000000..d358e2d62b --- /dev/null +++ b/spring-all/src/main/webapp/WEB-INF/normal-webapp-servlet.xml @@ -0,0 +1,19 @@ + + + + + + + + + + \ No newline at end of file diff --git a/spring-all/src/main/webapp/WEB-INF/rootApplicationContext.xml b/spring-all/src/main/webapp/WEB-INF/rootApplicationContext.xml new file mode 100644 index 0000000000..cd79c64e79 --- /dev/null +++ b/spring-all/src/main/webapp/WEB-INF/rootApplicationContext.xml @@ -0,0 +1,16 @@ + + + + + + + + + + \ No newline at end of file diff --git a/spring-all/src/main/webapp/WEB-INF/secure-webapp-servlet.xml b/spring-all/src/main/webapp/WEB-INF/secure-webapp-servlet.xml new file mode 100644 index 0000000000..5bca724670 --- /dev/null +++ b/spring-all/src/main/webapp/WEB-INF/secure-webapp-servlet.xml @@ -0,0 +1,19 @@ + + + + + + + + + + \ No newline at end of file diff --git a/spring-all/src/main/webapp/WEB-INF/secure/view/welcome.jsp b/spring-all/src/main/webapp/WEB-INF/secure/view/welcome.jsp new file mode 100644 index 0000000000..49ca0f8e87 --- /dev/null +++ b/spring-all/src/main/webapp/WEB-INF/secure/view/welcome.jsp @@ -0,0 +1,11 @@ + + + Spring Web Contexts + + +
+
+ Secure Web Application : ${message} +
+ + \ No newline at end of file diff --git a/spring-all/src/main/webapp/WEB-INF/view/welcome.jsp b/spring-all/src/main/webapp/WEB-INF/view/welcome.jsp new file mode 100644 index 0000000000..6a82fb1d5a --- /dev/null +++ b/spring-all/src/main/webapp/WEB-INF/view/welcome.jsp @@ -0,0 +1,11 @@ + + + Spring Web Contexts + + +
+
+ Normal Web Application : ${message} +
+ + \ No newline at end of file diff --git a/spring-all/src/main/webapp/WEB-INF/web.xml b/spring-all/src/main/webapp/WEB-INF/web.xml index 3ac9e9ed8c..2050f28f81 100644 --- a/spring-all/src/main/webapp/WEB-INF/web.xml +++ b/spring-all/src/main/webapp/WEB-INF/web.xml @@ -3,7 +3,48 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> - + + + + contextConfigLocation + /WEB-INF/rootApplicationContext.xml + + + + org.springframework.web.context.ContextLoaderListener + + + + + + secure-webapp + + org.springframework.web.servlet.DispatcherServlet + + 1 + + contextConfigLocation + /WEB-INF/secure-webapp-servlet.xml + + + + secure-webapp + /s/api/* + + + + + normal-webapp + + org.springframework.web.servlet.DispatcherServlet + + 1 + + + normal-webapp + /api/* + + test-mvc diff --git a/spring-all/src/main/webapp/index.jsp b/spring-all/src/main/webapp/index.jsp new file mode 100644 index 0000000000..c38169bb95 --- /dev/null +++ b/spring-all/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + + +

Hello World!

+ +