Spring Web Contexts (BAEL-82) (#2113)

* spring web contexts example

* applicationContext util added

* java based configuration added

* bean name fixed
This commit is contained in:
Anand kumar 2017-07-03 12:25:31 +05:30 committed by Grzegorz Piwowarek
parent 806c17b41b
commit d2880044df
17 changed files with 381 additions and 1 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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 = "<br><div style='text-align:center;'>" + "<h3> " + greeterService.greet() + "</h3></div>";
return new ModelAndView("welcome", "message", message);
}
}

View File

@ -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 = "<br><div style='text-align:center;'>" + "<h3> " + greeterService.greet() + "</h3></div>";
return new ModelAndView("welcome", "message", message);
}
}

View File

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

View File

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

View File

@ -0,0 +1,5 @@
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

View File

@ -0,0 +1,19 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.baeldung.contexts.normal" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

View File

@ -0,0 +1,16 @@
<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.baeldung.contexts.services" />
<bean id="greeting" class="ccom.baeldung.contexts.Greeting">
<property name="message" value="Hello World !!" />
</bean>
</beans>

View File

@ -0,0 +1,19 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.baeldung.contexts.secure" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/secure/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

View File

@ -0,0 +1,11 @@
<html>
<head>
<title>Spring Web Contexts</title>
</head>
<body>
<br>
<div style="padding: 10px; border-radius: 10px; font-size: 30px; text-align: center;">
Secure Web Application : ${message}
</div>
</body>
</html>

View File

@ -0,0 +1,11 @@
<html>
<head>
<title>Spring Web Contexts</title>
</head>
<body>
<br>
<div style="padding: 10px; border-radius: 10px; font-size: 30px; text-align: center;">
Normal Web Application : ${message}
</div>
</body>
</html>

View File

@ -4,6 +4,47 @@
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"> version="3.0">
<!-- load root application context -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/rootApplicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- secure web app context -->
<servlet>
<servlet-name>secure-webapp</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/secure-webapp-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>secure-webapp</servlet-name>
<url-pattern>/s/api/*</url-pattern>
</servlet-mapping>
<!-- normal web app context -->
<servlet>
<servlet-name>normal-webapp</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>normal-webapp</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
<servlet> <servlet>
<servlet-name>test-mvc</servlet-name> <servlet-name>test-mvc</servlet-name>
<servlet-class> <servlet-class>

View File

@ -0,0 +1,5 @@
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>