To add to Kevin’s answer, I find that almost all of your non-trivial Spring MVC applications will require an application context (as opposed to only the Spring MVC dispatcher servlet context). In the context of the application, you must configure all non-website related issues, such as:
- Security
- Constancy
- Scheduled Tasks
- Others?
To make it more specific, here is an example Spring configuration that I used when setting up a modern Spring application version 4.1.2) Spring MVC. Personally, I prefer to use the WEB-INF/web.xml , but this is really the only xml configuration in sight.
WEB-INF / web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <filter> <filter-name>openEntityManagerInViewFilter</filter-name> <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class> </filter> <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy </filter-class> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>openEntityManagerInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>springMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> <init-param> <param-name>contextClass</param-name> <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> </init-param> <init-param> <param-name>contextConfigLocation</param-name> <param-value>com.company.config.WebConfig</param-value> </init-param> </servlet> <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.company.config.AppConfig</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet-mapping> <servlet-name>springMvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <session-config> <session-timeout>30</session-timeout> </session-config> <jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <scripting-invalid>true</scripting-invalid> </jsp-property-group> </jsp-config> </web-app>
Webconfig.java
@Configuration @EnableWebMvc @ComponentScan(basePackages = "com.company.controller") public class WebConfig { @Bean public InternalResourceViewResolver getInternalResourceViewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } }
Appconfig.java
@Configuration @ComponentScan(basePackages = "com.company") @Import(value = {SecurityConfig.class, PersistenceConfig.class, ScheduleConfig.class}) public class AppConfig { // application domain @Beans here... }
Security.java
@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private LdapUserDetailsMapper ldapUserDetailsMapper; @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/").permitAll() .antMatchers("/**/js/**").permitAll() .antMatchers("/**/images/**").permitAll() .antMatchers("/**").access("hasRole('ROLE_ADMIN')") .and().formLogin(); http.logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.ldapAuthentication() .userSearchBase("OU=App Users") .userSearchFilter("sAMAccountName={0}") .groupSearchBase("OU=Development") .groupSearchFilter("member={0}") .userDetailsContextMapper(ldapUserDetailsMapper) .contextSource(getLdapContextSource()); } private LdapContextSource getLdapContextSource() { LdapContextSource cs = new LdapContextSource(); cs.setUrl("ldaps://ldapServer:636"); cs.setBase("DC=COMPANY,DC=COM"); cs.setUserDn("CN=administrator,CN=Users,DC=COMPANY,DC=COM"); cs.setPassword("password"); cs.afterPropertiesSet(); return cs; } }
PersistenceConfig.java
@Configuration @EnableTransactionManagement @EnableJpaRepositories(transactionManagerRef = "getTransactionManager", entityManagerFactoryRef = "getEntityManagerFactory", basePackages = "com.company") public class PersistenceConfig { @Bean public LocalContainerEntityManagerFactoryBean getEntityManagerFactory(DataSource dataSource) { LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean(); lef.setDataSource(dataSource); lef.setJpaVendorAdapter(getHibernateJpaVendorAdapter()); lef.setPackagesToScan("com.company"); return lef; } private HibernateJpaVendorAdapter getHibernateJpaVendorAdapter() { HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter(); hibernateJpaVendorAdapter.setDatabase(Database.ORACLE); hibernateJpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.Oracle10gDialect"); hibernateJpaVendorAdapter.setShowSql(false); hibernateJpaVendorAdapter.setGenerateDdl(false); return hibernateJpaVendorAdapter; } @Bean public JndiObjectFactoryBean getDataSource() { JndiObjectFactoryBean jndiFactoryBean = new JndiObjectFactoryBean(); jndiFactoryBean.setJndiName("java:comp/env/jdbc/AppDS"); return jndiFactoryBean; } @Bean public JpaTransactionManager getTransactionManager(DataSource dataSource) { JpaTransactionManager jpaTransactionManager = new JpaTransactionManager(); jpaTransactionManager.setEntityManagerFactory(getEntityManagerFactory(dataSource).getObject()); jpaTransactionManager.setDataSource(dataSource); return jpaTransactionManager; } }
ScheduleConfig.java
@Configuration @EnableScheduling public class ScheduleConfig { @Autowired private EmployeeSynchronizer employeeSynchronizer;
As you can see, the web configuration is only a small part of the overall configuration of the Spring web application. Most of the web applications I have worked with have many problems that lie outside the dispatcher servlet configuration, which require a full-blown application context loaded through org.springframework.web.context.ContextLoaderListener in web.xml .