Spring MVC无xml配置

如何配置与说明

前置知识

前置知识:

  1. 在servlet3.0后servlet容器会搜索classPath下的META-INF下javax.servlet.ServletContainerInitializer文件中配置的类,这个类需要实现javax.servlet.ServletContainerInitializer 接口并实现onStartup方法。
@HandlesTypes({WebApplicationInitializer.class})
public class SpringServletContainerInitializer implements ServletContainerInitializer {
    public SpringServletContainerInitializer() {
    }

    public void onStartup(Set<Class<?>> webAppInitializerClasses, ServletContext servletContext) throws ServletException {
    }
}

WebApplicationInitializer 是一个接口

public interface WebApplicationInitializer {
    void onStartup(ServletContext var1) throws ServletException;
}
  1. 上述onStartup方法中会有两个参数
@Override
    public void onStartup(Set<Class<?>> webAppInitializerClasses, ServletContext servletContext) throws ServletException {

参数说明:
webAppInitializerClasses 是对应ServletContainerInitializer 上@HandlesTypes 中对应接口的所有实现类的全限定类名

ServletContext 就是servlet上下文

  1. Spring 3.x 开始引入一个简易的 WebApplicationInitializer 实现类,这就是 AbstractAnnotationConfigDispatcherServletInitializer。所以 目标类继承 AbstractAnnotationConfigDispatcherServletInitializer之后,也就是间接实现了 WebApplicationInitializer,在 Servlet 3.0 容器中,它会被自动搜索到,被用来配置 servlet 上下文。

具体配置与说明

入口类Initializer

public class Initializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Filter[] getServletFilters() {
        // 这里配置servlet filter
        DelegatingFilterProxy a = new DelegatingFilterProxy();
        a.setTargetBeanName("***");
        return new Filter[] {a};
    }

    @Override
    protected Class<?>[] getRootConfigClasses() {
        // 这里配置spring基本配置(配置文件等)
        return new Class[] {RootConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        // 这里可以配置Servlet spring mvc 基本设置
        return new Class[] {ServletConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        /**
         * 这里设置为DispatcherServlet提供一个或更多的Servlet 映射;这里是被映射到 /ab/*  /bc/*,用来操作所有来到程序的 Request.
         * 这样就能使用 ip:端口/ab/* 或者 ip:端口/bc/* 请求到我们的服务
         */
        return new String[] {"/ab/*", "/bc/*"};
    }
}

基本配置类RootConfig

@Configuration
@PropertySource("classpath:application.properties")
@ImportResource({"classpath:dubbo-consumer.xml"})
// 自动扫描路径
@ComponentScan(basePackages = "*****")
public class RootConfig {
    private static final PathMatchingResourcePatternResolver RESOURCE_RESOLVER = new PathMatchingResourcePatternResolver();

    /**
     * 配置基础配置文件
     * @return
     */
    @Bean
    public static ReloadablePropertySourcesPlaceholderConfigurer reloadablePropertySourcesPlaceholderConfigurer() {
        ReloadablePropertySourcesPlaceholderConfigurer configurer = new ReloadablePropertySourcesPlaceholderConfigurer();
        configurer.setConfigName("***");
        configurer.setIgnoreUnresolvablePlaceholders(false);
        configurer.setFileEncoding("UTF-8");
        configurer.setLocation(RESOURCE_RESOLVER.getResource("classpath:application.properties"));
        return configurer;
    }

    /**
     * 后置处理器配置
     * @param configurer
     * @return
     */
    @Bean
    public ReloadablePropertyPostProcessor reloadablePropertyPostProcessor(ReloadablePropertySourcesPlaceholderConfigurer configurer) {
        return new ReloadablePropertyPostProcessor(configurer);
    }

    /**
     * 指标配置
     * @return
     */
    @Bean
    public MetricsConfiguration metricsConfiguration() {
        return new MetricsConfiguration();
    }
}

Spring MVC 基本设置类ServletConfig

// 开启mvc
@EnableWebMvc
@Configuration
// 开启aop Aspect 配置
@EnableAspectJAutoProxy
// 自动扫描
@ComponentScan(basePackages = "com.facishare.marketing.web")
public class ServletConfig extends WebMvcConfigurerAdapter {

    /**
     * 开启默认ServletHandling
     * @param configurer
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    /**
     * 设置多部件解析器(文件上传等)
     * @return
     */
    @Bean
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
        resolver.setDefaultEncoding("UTF-8");
        resolver.setMaxUploadSize(10485760);
        return resolver;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值