parent
							
								
									86498b66d8
								
							
						
					
					
						commit
						58612d0fb8
					
				| @ -170,6 +170,7 @@ | |||||||
|             <dependency> |             <dependency> | ||||||
|                 <groupId>org.springframework.boot</groupId> |                 <groupId>org.springframework.boot</groupId> | ||||||
|                 <artifactId>spring-boot-starter-thymeleaf</artifactId> |                 <artifactId>spring-boot-starter-thymeleaf</artifactId> | ||||||
|  |                 <version>${org.springframework.version}</version> | ||||||
|             </dependency> |             </dependency> | ||||||
|         </dependencies> |         </dependencies> | ||||||
|     </dependencyManagement> |     </dependencyManagement> | ||||||
|  | |||||||
| @ -0,0 +1,28 @@ | |||||||
|  | package com.baeldung.contexts.config; | ||||||
|  | 
 | ||||||
|  | import org.springframework.web.context.AbstractContextLoaderInitializer; | ||||||
|  | import org.springframework.web.context.WebApplicationContext; | ||||||
|  | import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; | ||||||
|  | import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer; | ||||||
|  | 
 | ||||||
|  | public class AnnotationsBasedApplicationAndServletInitializer extends AbstractDispatcherServletInitializer { | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     protected WebApplicationContext createRootApplicationContext() { | ||||||
|  |         AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); | ||||||
|  |         rootContext.register(RootApplicationConfig.class); | ||||||
|  |         return rootContext; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     protected WebApplicationContext createServletApplicationContext() { | ||||||
|  |         AnnotationConfigWebApplicationContext secureWebAppContext = new AnnotationConfigWebApplicationContext(); | ||||||
|  |         secureWebAppContext.register(SecureWebAppConfig.class); | ||||||
|  |         return secureWebAppContext; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     protected String[] getServletMappings() { | ||||||
|  |         return new String[] { "/s/api/*" }; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,16 @@ | |||||||
|  | package com.baeldung.contexts.config; | ||||||
|  | 
 | ||||||
|  | import org.springframework.web.context.AbstractContextLoaderInitializer; | ||||||
|  | import org.springframework.web.context.WebApplicationContext; | ||||||
|  | import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; | ||||||
|  | 
 | ||||||
|  | public class AnnotationsBasedApplicationInitializer extends AbstractContextLoaderInitializer { | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     protected WebApplicationContext createRootApplicationContext() { | ||||||
|  |         AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); | ||||||
|  |         rootContext.register(RootApplicationConfig.class); | ||||||
|  |         return rootContext; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -5,31 +5,31 @@ import javax.servlet.ServletException; | |||||||
| import javax.servlet.ServletRegistration; | import javax.servlet.ServletRegistration; | ||||||
| 
 | 
 | ||||||
| import org.springframework.context.annotation.Configuration; | import org.springframework.context.annotation.Configuration; | ||||||
|  | import org.springframework.context.support.ClassPathXmlApplicationContext; | ||||||
| import org.springframework.web.WebApplicationInitializer; | import org.springframework.web.WebApplicationInitializer; | ||||||
| import org.springframework.web.context.ContextLoaderListener; | import org.springframework.web.context.ContextLoaderListener; | ||||||
| import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; | import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; | ||||||
|  | import org.springframework.web.context.support.XmlWebApplicationContext; | ||||||
| import org.springframework.web.servlet.DispatcherServlet; | import org.springframework.web.servlet.DispatcherServlet; | ||||||
| 
 | 
 | ||||||
| @Configuration |  | ||||||
| public class ApplicationInitializer implements WebApplicationInitializer { | public class ApplicationInitializer implements WebApplicationInitializer { | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     public void onStartup(ServletContext servletContext) throws ServletException { |     public void onStartup(ServletContext servletContext) throws ServletException { | ||||||
|         AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); |         //XML Context | ||||||
|         rootContext.register(RootApplicationConfig.class); |         //XmlWebApplicationContext rootContext = new XmlWebApplicationContext(); | ||||||
|         servletContext.addListener(new ContextLoaderListener(rootContext)); |         //rootContext.setConfigLocations("/WEB-INF/rootApplicationContext.xml"); | ||||||
|  |         //Annotations Context | ||||||
|  |         //AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); | ||||||
|  |         //rootContext.register(RootApplicationConfig.class); | ||||||
|  |         //Registration | ||||||
|  |         //servletContext.addListener(new ContextLoaderListener(rootContext)); | ||||||
|          |          | ||||||
|         AnnotationConfigWebApplicationContext normalWebAppContext = new AnnotationConfigWebApplicationContext(); |         XmlWebApplicationContext normalWebAppContext = new XmlWebApplicationContext(); | ||||||
|         normalWebAppContext.register(NormalWebAppConfig.class); |         normalWebAppContext.setConfigLocation("/WEB-INF/normal-webapp-servlet.xml"); | ||||||
|         ServletRegistration.Dynamic normal = servletContext.addServlet("normal-webapp", new DispatcherServlet(normalWebAppContext)); |         ServletRegistration.Dynamic normal = servletContext.addServlet("normal-webapp", new DispatcherServlet(normalWebAppContext)); | ||||||
|         normal.setLoadOnStartup(1); |         normal.setLoadOnStartup(1); | ||||||
|         normal.addMapping("/api/*"); |         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/*"); |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  | |||||||
							
								
								
									
										11
									
								
								spring-all/src/main/webapp/WEB-INF/greeting.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								spring-all/src/main/webapp/WEB-INF/greeting.xml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,11 @@ | |||||||
|  | <?xml version = "1.0" encoding = "UTF-8"?> | ||||||
|  | 
 | ||||||
|  | <beans xmlns="http://www.springframework.org/schema/beans" | ||||||
|  | 	   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-3.0.xsd"> | ||||||
|  | 	 | ||||||
|  | 	<bean id="greeting" class="com.baeldung.contexts.Greeting"> | ||||||
|  | 		<property name="message" value="Hello World !!" /> | ||||||
|  | 	</bean> | ||||||
|  | </beans> | ||||||
| @ -1,6 +0,0 @@ | |||||||
| <?xml version="1.0" encoding="UTF-8"?> |  | ||||||
| <beans xmlns="http://www.springframework.org/schema/beans" |  | ||||||
| 	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-4.2.xsd" > |  | ||||||
| 
 |  | ||||||
| </beans> |  | ||||||
| @ -10,7 +10,5 @@ | |||||||
| 
 | 
 | ||||||
| 	<context:component-scan base-package="com.baeldung.contexts.services" /> | 	<context:component-scan base-package="com.baeldung.contexts.services" /> | ||||||
| 	 | 	 | ||||||
| 	<bean id="greeting" class="ccom.baeldung.contexts.Greeting"> | 	<import resource="greeting.xml" /> | ||||||
| 		<property name="message" value="Hello World !!" /> |  | ||||||
| 	</bean> |  | ||||||
| </beans> | </beans> | ||||||
| @ -4,36 +4,53 @@ | |||||||
|         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"> | ||||||
| 
 | 
 | ||||||
|  |     <!-- Uncommented, this disallows org.springframework.web.SpringServletContainerInitializer to run and execute | ||||||
|  |          application initializers. --> | ||||||
|  |     <!--<absolute-ordering> | ||||||
|  |     </absolute-ordering>--> | ||||||
|  | 	 | ||||||
| 	 <!-- load root application context --> | 	 <!-- load root application context --> | ||||||
|   	<context-param> |     <!--<listener> | ||||||
|     	<param-name>contextConfigLocation</param-name> |  | ||||||
|     	<param-value>/WEB-INF/rootApplicationContext.xml</param-value> |  | ||||||
| 	</context-param> |  | ||||||
| 	<listener> |  | ||||||
|         <listener-class> |         <listener-class> | ||||||
|             org.springframework.web.context.ContextLoaderListener |             org.springframework.web.context.ContextLoaderListener | ||||||
|         </listener-class> |         </listener-class> | ||||||
| 	</listener>  |     </listener>--> | ||||||
|  |   	<!--<context-param> | ||||||
|  |     	<param-name>contextConfigLocation</param-name> | ||||||
|  |     	<param-value>/WEB-INF/rootApplicationContext.xml</param-value> | ||||||
|  | 	</context-param>--> | ||||||
|  |     <!--<context-param> | ||||||
|  |         <param-name>contextClass</param-name> | ||||||
|  |         <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> | ||||||
|  |     </context-param> | ||||||
|  |     <context-param> | ||||||
|  |         <param-name>contextConfigLocation</param-name> | ||||||
|  |         <param-value>com.baeldung.contexts.config.RootApplicationConfig, com.baeldung.contexts.config.NormalWebAppConfig</param-value> | ||||||
|  |     </context-param>--> | ||||||
|  |     <!--<context-param> | ||||||
|  |         <param-name>contextConfigLocation</param-name> | ||||||
|  |         <param-value>org.baeldung.bean.config</param-value> | ||||||
|  |     </context-param>--> | ||||||
| 
 | 
 | ||||||
| 	<!-- secure web app context --> | 	<!-- secure web app context --> | ||||||
|     <servlet> |     <!--<servlet> | ||||||
|         <servlet-name>secure-webapp</servlet-name> |         <servlet-name>secure-webapp</servlet-name> | ||||||
|         <servlet-class> |         <servlet-class> | ||||||
|             org.springframework.web.servlet.DispatcherServlet |             org.springframework.web.servlet.DispatcherServlet | ||||||
|         </servlet-class> |         </servlet-class> | ||||||
| 		    <load-on-startup>1</load-on-startup> |  | ||||||
|         <init-param> |         <init-param> | ||||||
|             <param-name>contextConfigLocation</param-name> |             <param-name>contextConfigLocation</param-name> | ||||||
|             <param-value>/WEB-INF/secure-webapp-servlet.xml</param-value> |             <param-value>/WEB-INF/secure-webapp-servlet.xml</param-value> | ||||||
|         </init-param> |         </init-param> | ||||||
|  |         <load-on-startup>1</load-on-startup> | ||||||
|     </servlet> |     </servlet> | ||||||
|     <servlet-mapping> |     <servlet-mapping> | ||||||
|         <servlet-name>secure-webapp</servlet-name> |         <servlet-name>secure-webapp</servlet-name> | ||||||
|         <url-pattern>/s/api/*</url-pattern> |         <url-pattern>/s/api/*</url-pattern> | ||||||
|     </servlet-mapping> |     </servlet-mapping>--> | ||||||
|      |      | ||||||
|     <!-- normal web app context --> |     <!-- normal web app context --> | ||||||
|     <servlet> |     <!--<servlet> | ||||||
|         <servlet-name>normal-webapp</servlet-name> |         <servlet-name>normal-webapp</servlet-name> | ||||||
|         <servlet-class> |         <servlet-class> | ||||||
|             org.springframework.web.servlet.DispatcherServlet |             org.springframework.web.servlet.DispatcherServlet | ||||||
| @ -43,23 +60,26 @@ | |||||||
|     <servlet-mapping> |     <servlet-mapping> | ||||||
|         <servlet-name>normal-webapp</servlet-name> |         <servlet-name>normal-webapp</servlet-name> | ||||||
|         <url-pattern>/api/*</url-pattern> |         <url-pattern>/api/*</url-pattern> | ||||||
|     </servlet-mapping> |     </servlet-mapping>--> | ||||||
| 	 |     <!-- normal webapp with annotations-based context --> | ||||||
|     <servlet> |     <servlet> | ||||||
|         <servlet-name>test-mvc</servlet-name> |         <servlet-name>normal-webapp-annotations</servlet-name> | ||||||
|         <servlet-class> |         <servlet-class> | ||||||
|             org.springframework.web.servlet.DispatcherServlet |             org.springframework.web.servlet.DispatcherServlet | ||||||
|         </servlet-class> |         </servlet-class> | ||||||
|  |         <init-param> | ||||||
|  |             <param-name>contextClass</param-name> | ||||||
|  |             <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> | ||||||
|  |         </init-param> | ||||||
|         <init-param> |         <init-param> | ||||||
|             <param-name>contextConfigLocation</param-name> |             <param-name>contextConfigLocation</param-name> | ||||||
|             <param-value>/WEB-INF/test-mvc.xml</param-value> |             <param-value>com.baeldung.contexts.config.NormalWebAppConfig</param-value> | ||||||
|         </init-param> |         </init-param> | ||||||
|         <load-on-startup>1</load-on-startup> |         <load-on-startup>1</load-on-startup> | ||||||
|     </servlet> |     </servlet> | ||||||
| 
 |  | ||||||
|     <servlet-mapping> |     <servlet-mapping> | ||||||
|         <servlet-name>test-mvc</servlet-name> |         <servlet-name>normal-webapp-annotations</servlet-name> | ||||||
|         <url-pattern>/test/*</url-pattern> |         <url-pattern>/api-ann/*</url-pattern> | ||||||
|     </servlet-mapping> |     </servlet-mapping> | ||||||
| 
 | 
 | ||||||
|     <welcome-file-list> |     <welcome-file-list> | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user