Servlet3.0 javaConfig配置
中配置,从Servlet 3.0开始提供了ServletContainerInitializer接口,允许使用代码去配置servlets、filters、listeners。
接口即可。
  • publicclassWebInitializerimplementsWebApplicationInitializer {
  •  
  •     privatestaticfinal Logger logger = LoggerFactory.getLogger(WebInitializer.class);
  •  
  •     @Override
  •     publicvoidonStartup(javax.servlet.ServletContext servletContext) throws ServletException {
  •         logger.info("begin init web application.");
  •  
  •         //配置Spring
  •         AnnotationConfigWebApplicationContext springContext = new AnnotationConfigWebApplicationContext();
  •         springContext.register(SpringConfig.class);
  •         
  •         //添加linstener
  •         servletContext.addListener(new ContextLoaderListener(springContext));
  •  
  •         //添加servlet
  •         ServletRegistration.Dynamic dispatcher = servletContext.addServlet(
  •                 "dispatcher", new DispatcherServlet(springContext));
  •         dispatcher.setLoadOnStartup(1);
  •         dispatcher.addMapping("/");
  •  
  •         //添加filter
  •         LoggerFilter loggerFilter = new LoggerFilter();
  •         FilterRegistration.Dynamic logFilterRegistration=container.addFilter("requestResponseLogFilter", loggerFilter);
  •         logFilterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC), false, "/*");
  •  
  •         logger.info("init web application success.");
  •     }
  • }
  • Spring 配置
    Spring的配置主要就是配置各种Bean,主要是要了解几种注解的使用方法。
     
    Configuration 注解 
    Bean标注为 bean
     
  • @ConfigurationpublicclassSpringConfig {
  •     @Bean(name = "exampleBean")
  •     public ExampleBean getExampleBean() {
  •         returnnew ExampleBean();
  •     }
  • }
  • ComponentScan 注解
    Spring会自动扫描注册指定包中使用注解指定的Bean。
  • @ComponentScan(basePackages = {"com.example.service","com.example.dao"})
  • @PropertySource  注解
    对象可以很方便的拿到配置文件中的内容。
  • @Configuration@PropertySource("classpath:config.properties")@ComponentScan(basePackages = {"com.example.service","com.example.dao"})publicclassSpringConfig {
  •  
  •     @Autowired
  •     private Environment env;
  •  
  •     @Bean(name = "mysqlDataSource")
  •     public DataSource mysqlDataSource() {
  •         ProxoolDataSource dataSource = new ProxoolDataSource();
  •         dataSource.setDriver(env.getProperty("ds.driver.classname"));
  •         dataSource.setDriverUrl(env.getProperty("ds.url"));
  •         dataSource.setUser(env.getProperty("ds.username"));
  •         dataSource.setPassword(env.getProperty("ds.password"));
  •         dataSource.setPrototypeCount(env.getProperty("proxool.prototype", Integer.class));
  •         dataSource.setMinimumConnectionCount(env.getProperty("proxool.minimum", Integer.class));
  •         dataSource.setMaximumConnectionCount(env.getProperty("proxool.maximum", Integer.class));
  •         dataSource.setSimultaneousBuildThrottle(env.getProperty("proxool.simultaneous", Integer.class));
  •         dataSource.setTestBeforeUse(true);
  •         dataSource.setHouseKeepingTestSql(env.getProperty("proxool.testSql"));
  •         return dataSource;
  •     }
  •  
  • }
  •  
    文件上传功能简介
    基本概念
    要使用基于Servlet 3.0的多路传输转换功能:
    1、在web.xml中为DispatcherServlet添加一个multipart-config元素
    2、通过Servlet编程的方法使用MultipartConfigElement进行注册
    3、自己定制了自己的Servlet类,那你必须使用 @MultipartConfig对其进行注解。
    注意:诸如最大文件大小或存储位置等配置选项都必须在这个Servlet级别进行注册,因为Servlet 3.0不允许在解析器MultipartResolver的层级配置这些信息。
     
     
    使用方法
    在以前,处理文件上传是一个很痛苦的事情,大都借助于开源的上传组件,诸如commons fileupload等。现在好了,很方便,便捷到比那些组件都方便至极。
     
    让Servlet支持上传,需要做两件事情
  • 需要添加MultipartConfig注解
  • 从request对象中获取Part文件对象
  • MultipartConfig注解
    属性名 类型 是否可选 描述  
    fileSizeThreshold int 当数据量大于该值时,内容将被写入文件。  
    location String 存放生成的文件地址。  
    maxFileSize long 允许上传的文件最大值。默认值为 -1,表示没有限制。  
    maxRequestSize long 针对该 multipart/form-data 请求的最大数量,默认值为 -1,表示没有限制。  
             
    一些实践建议:
  • 若是上传一个文件,仅仅需要设置maxFileSize属性即可。
  • 上传多个文件,可能需要设置maxRequestSize属性,设定一次上传数据的最大量。
  • 上传过程中无论是单个文件超过maxFileSize值,或者上传总的数据量大于maxRequestSize值都会抛出IllegalStateException异常;
  • location属性,既是保存路径(在写入的时候,可以忽略路径设定),又是上传过程中临时文件的保存路径,一旦执行Part.write方法之后,临时文件将被自动清除。
  • 但Servlet 3.0规范同时也说明,不提供获取上传文件名的方法,尽管我们可以通过part.getHeader("content-disposition")方法间接获取得到。
  • 如何读取MultipartConfig注解属性值,API没有提供直接读取的方法,只能手动获取。
  •  
    参考文档:
    Servlet 3.0笔记之超方便的文件上传支持
    表单多文件上传样式美化 && 支持选中文件后删除相关项

    相关文章: