View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.samples.spring.config;
20  
21  import org.springframework.web.WebApplicationInitializer;
22  import org.springframework.web.context.ContextLoaderListener;
23  import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
24  import org.springframework.web.filter.DelegatingFilterProxy;
25  import org.springframework.web.servlet.DispatcherServlet;
26  import org.springframework.web.util.Log4jConfigListener;
27  
28  import javax.servlet.DispatcherType;
29  import javax.servlet.FilterRegistration;
30  import javax.servlet.ServletContext;
31  import javax.servlet.ServletRegistration;
32  import java.util.EnumSet;
33  
34  /**
35   * Initializes Spring Environment without the need for a web.xml
36   */
37  public class ServletApplicationInitializer implements WebApplicationInitializer {
38  
39      @Override
40      public void onStartup(ServletContext container) {
41  
42          //now add the annotations
43          AnnotationConfigWebApplicationContext appContext = getContext();
44  
45          // Manage the lifecycle of the root application context
46          container.addListener(new ContextLoaderListener(appContext));
47  
48          container.addListener(new Log4jConfigListener());
49  
50          FilterRegistration.Dynamic shiroFilter = container.addFilter("shiroFilterFactoryBean", DelegatingFilterProxy.class);
51          shiroFilter.setInitParameter("targetFilterLifecycle", "true");
52          shiroFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*");
53  
54  
55          ServletRegistration.Dynamic remotingDispatcher = container.addServlet("remoting", new DispatcherServlet(appContext));
56          remotingDispatcher.setLoadOnStartup(1);
57          remotingDispatcher.addMapping("/remoting/*");
58  
59  
60          ServletRegistration.Dynamic dispatcher = container.addServlet("DispatcherServlet", new DispatcherServlet(appContext));
61          dispatcher.setLoadOnStartup(1);
62          dispatcher.addMapping("/");
63  
64      }
65  
66      private AnnotationConfigWebApplicationContext getContext() {
67          AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
68          context.setConfigLocation(getClass().getPackage().getName());
69          return context;
70      }
71  
72  }