码迷,mamicode.com
首页 > Web开发 > 详细

WebConfig类

时间:2019-01-30 07:18:37      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:source   ini   Servle   ted   convert   thread   方法   前缀和   sage   

 1 package com.ssm.yjblogs.config;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 import java.util.concurrent.Executor;
 6 
 7 import org.springframework.context.annotation.Bean;
 8 import org.springframework.context.annotation.ComponentScan;
 9 import org.springframework.context.annotation.ComponentScan.Filter;
10 import org.springframework.context.annotation.Configuration;
11 import org.springframework.context.annotation.FilterType;
12 import org.springframework.http.MediaType;
13 import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
14 import org.springframework.scheduling.annotation.AsyncConfigurerSupport;
15 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
16 import org.springframework.stereotype.Controller;
17 import org.springframework.web.servlet.HandlerAdapter;
18 import org.springframework.web.servlet.ViewResolver;
19 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
20 import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
21 import org.springframework.web.servlet.view.InternalResourceViewResolver;
22 
23 @Configuration
24 //定义Spring MVC扫描的包
25 @ComponentScan(value="com.*", includeFilters= {@Filter(type = FilterType.ANNOTATION, value = Controller.class)})
26 //启动Spring MVC配置 P389
27 @EnableWebMvc
28 //使用@EnableAsync表明支持异步调用,而我们重写了抽象类AsyncConfigurerSupport的getAsyncExecutor方法
29 //它是获取一个任务池,当在Spring环境中遇到注解@Async就会启动这个任务池的一条线程去运行对应的方法,这样便能执行异步了。
30 //@EnableAsync
31 public class WebConfig extends AsyncConfigurerSupport { 
32 
33     /***
34      * 通过注解 @Bean 初始化视图解析器
35      * @return ViewResolver 视图解析器
36      */
37     @Bean(name="internalResourceViewResolver")
38     public ViewResolver initViewResolver() {
39         InternalResourceViewResolver viewResolver =new InternalResourceViewResolver();
40         //设置前缀和后缀,这样就能获取由控制器返回的视图逻辑名,进而找到对应的JSP文件  P436
41         viewResolver.setPrefix("/WEB-INF/jsp/");//前缀
42         viewResolver.setSuffix(".jsp");//后缀
43         return viewResolver;
44     }
45     
46     /**
47      * 初始化RequestMappingHandlerAdapter,并加载Http的Json转换器
48      * @return  RequestMappingHandlerAdapter 对象
49      */
50     //注解@ResponseBody将标记SpringMVC,将响应结果转变为JSON,这样在控制器返回结果后,在处理器内转变为JSON  P453
51     @Bean(name="requestMappingHandlerAdapter") 
52     public HandlerAdapter initRequestMappingHandlerAdapter() {
53         //创建RequestMappingHandlerAdapter适配器
54         RequestMappingHandlerAdapter rmhd = new RequestMappingHandlerAdapter();
55         //HTTP JSON转换器
56         MappingJackson2HttpMessageConverter  jsonConverter 
57             = new MappingJackson2HttpMessageConverter();
58         //MappingJackson2HttpMessageConverter接收JSON类型消息的转换
59         MediaType mediaType = MediaType.APPLICATION_JSON_UTF8;
60         List<MediaType> mediaTypes = new ArrayList<MediaType>();
61         mediaTypes.add(mediaType);
62         //加入转换器的支持类型
63         jsonConverter.setSupportedMediaTypes(mediaTypes);
64         //往适配器加入json转换器
65         rmhd.getMessageConverters().add(jsonConverter);
66         return rmhd;
67     }
68     
69     
70 //    @Override
71 //    public Executor getAsyncExecutor() {
72 //        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
73 //        taskExecutor.setCorePoolSize(5);
74 //        taskExecutor.setMaxPoolSize(10);
75 //        taskExecutor.setQueueCapacity(200);
76 //        taskExecutor.initialize();
77 //        return taskExecutor;
78 //    }
79 }

无行号版本:

package com.ssm.yjblogs.config;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;

import org.springframework.context.annotation.Bean;
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.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.scheduling.annotation.AsyncConfigurerSupport;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
//定义Spring MVC扫描的包
@ComponentScan(value="com.*", includeFilters= {@Filter(type = FilterType.ANNOTATION, value = Controller.class)})
//启动Spring MVC配置 P389
@EnableWebMvc
//使用@EnableAsync表明支持异步调用,而我们重写了抽象类AsyncConfigurerSupport的getAsyncExecutor方法
//它是获取一个任务池,当在Spring环境中遇到注解@Async就会启动这个任务池的一条线程去运行对应的方法,这样便能执行异步了。
//@EnableAsync
public class WebConfig extends AsyncConfigurerSupport { 

    /***
     * 通过注解 @Bean 初始化视图解析器
     * @return ViewResolver 视图解析器
     */
    @Bean(name="internalResourceViewResolver")
    public ViewResolver initViewResolver() {
        InternalResourceViewResolver viewResolver =new InternalResourceViewResolver();
        //设置前缀和后缀,这样就能获取由控制器返回的视图逻辑名,进而找到对应的JSP文件  P436
        viewResolver.setPrefix("/WEB-INF/jsp/");//前缀
        viewResolver.setSuffix(".jsp");//后缀
        return viewResolver;
    }
    
    /**
     * 初始化RequestMappingHandlerAdapter,并加载Http的Json转换器
     * @return  RequestMappingHandlerAdapter 对象
     */
    //注解@ResponseBody将标记SpringMVC,将响应结果转变为JSON,这样在控制器返回结果后,在处理器内转变为JSON  P453
    @Bean(name="requestMappingHandlerAdapter") 
    public HandlerAdapter initRequestMappingHandlerAdapter() {
        //创建RequestMappingHandlerAdapter适配器
        RequestMappingHandlerAdapter rmhd = new RequestMappingHandlerAdapter();
        //HTTP JSON转换器
        MappingJackson2HttpMessageConverter  jsonConverter 
            = new MappingJackson2HttpMessageConverter();
        //MappingJackson2HttpMessageConverter接收JSON类型消息的转换
        MediaType mediaType = MediaType.APPLICATION_JSON_UTF8;
        List<MediaType> mediaTypes = new ArrayList<MediaType>();
        mediaTypes.add(mediaType);
        //加入转换器的支持类型
        jsonConverter.setSupportedMediaTypes(mediaTypes);
        //往适配器加入json转换器
        rmhd.getMessageConverters().add(jsonConverter);
        return rmhd;
    }
    
    
//    @Override
//    public Executor getAsyncExecutor() {
//        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
//        taskExecutor.setCorePoolSize(5);
//        taskExecutor.setMaxPoolSize(10);
//        taskExecutor.setQueueCapacity(200);
//        taskExecutor.initialize();
//        return taskExecutor;
//    }
}

 

WebConfig类

标签:source   ini   Servle   ted   convert   thread   方法   前缀和   sage   

原文地址:https://www.cnblogs.com/YUJIE666/p/10336549.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!