重写全局配置

如果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. http协议中的请求方式

    get:获取url传的查询字符串(action=show)表单和连接的url中传的值.容量2K左右. post:以post方式提交,获取表单和连接的url中传的值.容量8M左右. delete:删除某 ...

  2. Go-day05

    今日概要: 1. 结构体和方法 2. 接口 一.go中的struct 1. 用来自定义复杂数据结构 2. struct里面可以包含多个字段(属性) 3. struct类型可以定义方法,注意和函数的区分 ...

  3. hadoop 伪分布式搭建

    下载hadoop1.0.4版本,和jdk1.6版本或更高版本:1. 安装JDK,安装目录大家可以自定义,下面是我的安装目录: /usr/jdk1.6.0_22 配置环境变量: [root@hadoop ...

  4. puppeteer,新款headless chrome

    puppeteer puppeteer是一种谷歌开发的Headless Chrome,因为puppeteer的出现,业内许多自动化测试库停止维护,比如PhantomJS,Selenium IDE fo ...

  5. python 第一类对象 闭包 迭代器

    ########################总结########################### 1. 函数名 -> 第一类对象 函数名就是变量名. 函数可以赋值 函数可以作为集合类的 ...

  6. Dubbo优雅关机原理

    Dubbo是通过JDK的ShutdownHook来完成优雅停机的 所以如果用户使用 kill -9 PID 等强制关闭命令,是不会执行优雅停机的 只有通过 kill PID时,才会执行 原理: · 服 ...

  7. Hadoop记录-Ganglia监控HDFS和HBase指标说明

    HDFS相关 datanode相关 参数 解释 dfs.datanode.blockChecksumOp_avg_time 块校验平均时间 dfs.datanode.blockChecksumOp_n ...

  8. SSRF漏洞分析与利用

    转自:http://www.4o4notfound.org/index.php/archives/33/ 前言:总结了一些常见的姿势,以PHP为例,先上一张脑图,划√的是本文接下来实际操作的 0x01 ...

  9. 【不懂】spring bean生命周期

    完整的生命周期(牢记): 1.spring容器准备 2.实例化bean 3.注入依赖关系 4.初始化bean 5.使用bean 6.销毁bean Bean的完整生命週期可以認為是從容器建立初始化Bea ...

  10. HDU - 6357 Hills And Valleys(DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=6357 题意 给一个数值范围为0-9的a数组,可以选择翻转一个区间,问非严格最长上升子序列,以及翻转的区间. 分析 ...