重写全局配置

如果springboot提供的springmvc配置不符合要求,则可以通过一个配置类(标有@Configuration注解的类)加上@EnableWebMvc注解来实现完全自己控制的mvc配置

当你既想保留springboot提供的配置,又想增加自己额外的配置时,可以定义一个配置类并继承WebMvcConfigurationAdapter,无须使用@EnableWebMvc注解

web容器配置(servlet容器配置)

如果需要使用代码的方式配置servlet容器,则可以注册一个实现EmbeddedServletContainerCustomizer接口的bean,若想直接配置Tomcat、Jetty、Undertow则可以直接定义TomcatEmbededServletContainerFactory、Jetty....、Undertow...

springboot默认是以tomcat作为servlet容器,修改为其他web容器

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
<version>1.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

Servlet配置

@Bean
public ServletRegistrationBean camelServletRegistrationBean() {
ServletRegistrationBean registration = new ServletRegistrationBean(new CamelHttpTransportServlet(), "/hsm/*");
registration.setName("CamelServlet");
return registration;
}

Filter配置

@Bean
public FilterRegistrationBean requestParamFilter() {
FilterRegistrationBean filterReg = new FilterRegistrationBean();
filterReg.setFilter(new RequestParamFilter());
filterReg.setUrlPatterns(Collections.singletonList("/service/*"));
filterReg.setName("requestParamFilter");
return filterReg;
}

Listener配置

listener类需要实现ServletContextListener接口,并标注@Component注解。想增加一些自定义的配置,可以如下

@Bean
public ServletListenerRegistrationBean socketListener(){
ServletListenerRegistrationBean<EventListener> listenerRegistrationBean = new ServletListenerRegistrationBean();
listenerRegistrationBean.setListener(new VideoUploadLinstener());
return listenerRegistrationBean;
}

初始化参数配置

@Bean
public InitParameterConfiguringServletContextInitializer initParamsInitializer(Environment env) {
Map<String, String> contextParams = new HashMap<>();
contextParams.put("videoUploadServerPort", env.getProperty("videoUploadServerPort"));
return new InitParameterConfiguringServletContextInitializer(contextParams);
}

初始化参数就相当于之前我们配置spring listener、servlet的initParam。可以通过  javax.servlet.ServletContext.getInitParameter(key) 来取值。这种方式是全局化的添加web初始化参数,通过ServletContext取值。

加载自定义yml文件

springboot在容器启动默认会去加载jar包同级config目录、jar包同级目录、classpath config目录,classpath目录的application.yml(或者.properties)文件,使用 spring.profiles.active=dev 可以激活application-dev.yml文件,支持多个。此外使用 YamlPropertiesFactoryBean 类可以自定义加载。

YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
yamlPropertiesFactoryBean.setResources(new ClassPathResource("ftp.yml"));
Properties properties = yamlPropertiesFactoryBean.getObject();
isSftp = Boolean.valueOf(properties.getProperty("ftp.sftp"));
url = properties.getProperty("ftp.url");
port = Integer.parseInt(properties.getProperty("ftp.port"));
username = properties.getProperty("ftp.username");
password = properties.getProperty("ftp.password");
localPath = properties.getProperty("ftp.localPath");
remotePath = properties.getProperty("ftp.remotePath");

SpringBoot的Web配置的更多相关文章

  1. springboot情操陶冶-web配置(九)

    承接前文springboot情操陶冶-web配置(八),本文在前文的基础上深入了解下WebSecurity类的运作逻辑 WebSecurityConfigurerAdapter 在剖析WebSecur ...

  2. springboot情操陶冶-web配置(七)

    参数校验通常是OpenApi必做的操作,其会对不合法的输入做统一的校验以防止恶意的请求.本文则对参数校验这方面作下简单的分析 spring.factories 读者应该对此文件加以深刻的印象,很多sp ...

  3. springboot情操陶冶-web配置(四)

    承接前文springboot情操陶冶-web配置(三),本文将在DispatcherServlet应用的基础上谈下websocket的使用 websocket websocket的简单了解可见维基百科 ...

  4. springboot情操陶冶-web配置(二)

    承接前文springboot情操陶冶-web配置(一),在分析mvc的配置之前先了解下其默认的错误界面是如何显示的 404界面 springboot有个比较有趣的配置server.error.whit ...

  5. springboot情操陶冶-web配置(三)

    承接前文springboot情操陶冶-web配置(二),本文将在前文的基础上分析下mvc的相关应用 MVC简单例子 直接编写一个Controller层的代码,返回格式为json package com ...

  6. SpringBoot学习(七)-->SpringBoot在web开发中的配置

    SpringBoot在web开发中的配置 Web开发的自动配置类:在Maven Dependencies-->spring-boot-1.5.2.RELEASE.jar-->org.spr ...

  7. 从Spring到SpringBoot构建WEB MVC核心配置详解

    目录 理解Spring WEB MVC架构的演变 认识Spring WEB MVC 传统时代的Spring WEB MVC 新时代Spring WEB MVC SpringBoot简化WEB MVC开 ...

  8. springboot的Web开发-Web相关配置

    一:Spring Boot提供自动配置 通过查看WebMvcAutoConfiguration及WebMvcProperties的源码,可以发现Spring Boot为我们提供了如下的自动配置. 1, ...

  9. SpringBoot的Web开发

    一.创建Web项目 创建的时候勾选对应web选项即可,会自动引入相应的starter,pom如下: <dependency> <groupId>org.springframew ...

随机推荐

  1. POJ 3687 Labeling Balls (top 排序)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15792   Accepted: 4630 D ...

  2. 【BZOJ3894】【Luogu3358】文理分科 - 最小割多选一模型

    链接Click Here 这个题就是个板子的最小割多选一模型啦\(QwQ\),这里介绍一种通用的解法. 抛开组合收益不谈,这个题就是一个简单的最小割模型.我们只需要建出来这样一张图,在上面跑最小割,割 ...

  3. 新建体(1):新建type

    类似数组的类型: TYPE TAB_TYPE_MCHNO IS TABLE OF t_r_terminal.rt_merchno%type; tMchNo TAB_TYPE_MCHNO; )集合赋值: ...

  4. HTML常用提交按钮

    1. 标签=元素 disabled(不可操作)  readonly(只读)  placeholder(提示文本) autofocus(自动获焦)  autocomplete=”on(默认.规定启用自动 ...

  5. pandas知识点

    1.选择对象 1.选择特定列和行的数据 a['x'] 那么将会返回columns为x的列,注意这种方式一次只能返回一个列.a.x与a['x']意思一样. 取行数据,通过切片[]来选择 如:a[0:3] ...

  6. 剑指Offer_编程题_1

    题目描述 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数.   class Sol ...

  7. Linux记录-I/O系统监控

    几个基本的概念 在研究磁盘性能之前我们必须先了解磁盘的结构,以及工作原理.不过在这里就不再重复说明了,关系硬盘结构和工作原理的信息可以参考维基百科上面的相关词条——Hard disk drive(英文 ...

  8. [开源 .NET 跨平台 Crawler 数据采集 爬虫框架: DotnetSpider] 学习

    http://www.cnblogs.com/jjg0519/p/6707513.html

  9. VirtualBox下安装Ubuntu Server 16.04

    安装环境: Windows:确保磁盘空间足够,一般需要8个G左右. 所需文件: 首先在Ubuntu的官网上下载.iso的镜像文件,链接是:http://www.ubuntu.org.cn/server ...

  10. Maven入门:使用Nexus搭建Maven私服及上传下载jar包

    一. 私服搭建及配置 1 . 私服简介 私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件.有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在则下载到本地仓 ...