1. 程式人生 > >Spring使用程式碼配置Web.xml

Spring使用程式碼配置Web.xml

方式一:繼承WebMvcConfigurerAdapter類

1、使用一個類來繼承

package com.wbg.springJavaConfig.spring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; 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; import javax.servlet.ServletContext; import javax.servlet.ServletRegistration; @Configuration//聲明當前類是一個配置類(相當於一個Spring的xml檔案) @EnableWebMvc//若無此註解,WebMvcConfigurerAdapter無效 /** * * @ComponentScan 掃描("xxx")包下的@Service、@Controller、@Component、@Repository的類,並註冊為Bean *相當於:<context:component-scan base-package="xxx" />
*/ @ComponentScan("com.wbg.springJavaConfig.controller") public class SpringConfig extends WebMvcConfigurerAdapter { /** * @Bean 相當Spring配置檔案bean節點 * 新增一個ViewResolver解析view 配置jsp * @return InternalResourceViewResolver * 這裡的配置相當於: * <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> * <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> * <property name="prefix" value="/WEB-INF/jsp/" /> * <property name="suffix" value=".jsp" /> * </bean> */ @Bean public InternalResourceViewResolver viewResolver(){ InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/views/"); viewResolver.setSuffix(".jsp"); viewResolver.setViewClass(JstlView.class); return viewResolver; } } class WebInitializer implements WebApplicationInitializer { /** * <context-param> * <param-name>contextConfigLocation</param-name> * <param-value>classpath:spring/spring-*.xml</param-value> * </context-param> * <listener> * <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> * </listener> * * * <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> * <init-param> * <param-name>contextConfigLocation</param-name> * <param-value>classpath:spring-web.xml</param-value> * </init-param> * * @param servletContext */ public void onStartup(ServletContext servletContext){ AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(SpringConfig.class); ctx.setServletContext(servletContext); ServletRegistration.Dynamic dynamic = servletContext.addServlet("dispatcher",new DispatcherServlet(ctx)); dynamic.addMapping("/"); dynamic.setLoadOnStartup(1); } }
View Code

2、建立jsp

3、使用

方式二:繼承AbstractAnnotationConfigDispatcherServletInitializer類

1、建立WebInit繼承 :

package com.wbg.springJavaConfig.spring2;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{ContextConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringWebConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}
View Code

2、建立:SpringWebConfig類

package com.wbg.springJavaConfig.spring2;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;


@Configuration
public class SpringWebConfig {
    @Bean
    public InternalResourceViewResolver internalResourceViewResolverConfig(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setViewClass(JstlView.class);
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

    @Bean
    public StandardServletMultipartResolver multipartResolverConfig(){
        return new StandardServletMultipartResolver();
    }

    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();//對靜態資源的請求轉發到容器預設的servlet,而不使用 DispatcherServlet
    }
}
View Code

3、建立ContextConfig類

package com.wbg.springJavaConfig.spring2;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.wbg.springJavaConfig.controller"},excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = EnableWebMvc.class)})
public class ContextConfig {
}
View Code

3、使用

 

 demo:https://github.com/weibanggang/SpirngwebConfig.git