SPRING IN ACTION 第4版笔记-第五章Building Spring web applications-001-SpringMVC介绍
一、
二、用Java文件配置web application
1.
1 package spittr.config; 2 3 import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; 4 5 import spittr.web.WebConfig; 6 7 public class SpitterWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 8 9 @Override 10 protected Class<?>[] getRootConfigClasses() { 11 return new Class<?>[] { RootConfig.class }; 12 } 13 14 @Override 15 protected Class<?>[] getServletConfigClasses() { 16 //Specify configuration class 17 return new Class<?>[] { WebConfig.class }; 18 } 19 20 @Override 21 protected String[] getServletMappings() { 22 //Map DispatcherServlet to "/" 23 return new String[] { "/" }; 24 } 25 26 }
(1)值得注意的是SpitterWebInitializer继承了AbstractAnnotationConfigDispatcherServletInitializer,所以在Servlet3.1以上的容器中,会自动将此类作为DispatcherServlet和Spring Application Context的配置文件
从Servlect3.0开始,窗器会在classpath中寻找实现javax.servlet.ServletContainerInitializer的类作为配置文件,而Spring提供了实现SpringServletContainerInitializer、WebApplicationInitializer、AbstractAnnotationConfigDispatcherServletInitializer
(2)在Spring web application通常有两种context:Dispactcherservlet、ContextLoaderListener
Dispactcherservlet用来装载springmvc相关组件,如controller、view resolvers、handler mappings,ContextLoaderListener是装载应用的其他组件,通常是中间层和后台打交道的组件,如service、dao。 上述配置文件通过getServletConfigClasses()以返回的java配置文件来定义Dispatcherservlet,用getRootConfigClasses()以返回的配置文件定义ContextLoaderListener,其实就是相当于以前的web.xml配置
2.
package spittr.web; 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.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.InternalResourceViewResolver; @Configuration @EnableWebMvc @ComponentScan("spittr.web") public class WebConfig extends WebMvcConfigurerAdapter { @Bean public ViewResolver viewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { //Configure static content handling configurer.enable(); //configurer.enable();是you’re asking DispatcherServlet to forward //requests for static resources to the servlet container’s default servlet and not to try to //handle them itself. } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { // TODO Auto-generated method stub super.addResourceHandlers(registry); } }
在xml中是用 <mvc:annotation-driven>
3.
package spittr.config; import java.util.regex.Pattern; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.context.annotation.Import; import org.springframework.core.type.filter.RegexPatternTypeFilter; import spittr.config.RootConfig.WebPackage; @Configuration @Import(DataConfig.class) @ComponentScan(basePackages={"spittr"}, excludeFilters={ @Filter(type=FilterType.CUSTOM, value=WebPackage.class) }) public class RootConfig { public static class WebPackage extends RegexPatternTypeFilter { public WebPackage() { super(Pattern.compile("spittr\\.web")); } } }
4.
1 package spittr.config; 2 3 import javax.sql.DataSource; 4 5 import org.springframework.context.annotation.Bean; 6 import org.springframework.context.annotation.Configuration; 7 import org.springframework.jdbc.core.JdbcOperations; 8 import org.springframework.jdbc.core.JdbcTemplate; 9 import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; 10 import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; 11 12 @Configuration 13 public class DataConfig { 14 15 @Bean 16 public DataSource dataSource() { 17 return new EmbeddedDatabaseBuilder() 18 .setType(EmbeddedDatabaseType.H2) 19 .addScript("schema.sql") 20 .build(); 21 } 22 23 @Bean 24 public JdbcOperations jdbcTemplate(DataSource dataSource) { 25 return new JdbcTemplate(dataSource); 26 } 27 28 }
5.