Spring使用代码配置Web.xml

Posted 邦☆杠

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

4、使用

 

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

以上是关于Spring使用代码配置Web.xml的主要内容,如果未能解决你的问题,请参考以下文章

Choose unique values for the 'webAppRootKey' context-param in your web.xml files! 错误的解决(代码片段

在没有插件 web.xml 问题的情况下配置 Spring Security

spring的配置文件在web.xml中加载的方式

在web.xml正确加载spring配置文件的方式

web.xml中的contextConfigLocation在spring中的作用

Spring Struts2 整合