重写全局配置

如果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 3713 Transferring Sylla (三联通分量)

    Transferring Sylla Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3533   Accepted: 988 ...

  2. 字符缓冲流BufferedWriter BufferedReader

    //字符缓冲流主要用于文本数据的高速写入 package cn.lijun.demo1; import java.io.BufferedReader; import java.io.FileNotFo ...

  3. 《Java 程序设计》第一周学习总结

    本周因为刚刚接触Linux和码云等等,所以在完成作业的时候遇到很多问题. 首先,在安装Linux没有安装盘片,在盘片安装之后成功建立虚拟机,建立虚拟机后首先要下载jdk,第一次下载时没有选对格式,Li ...

  4. 9.Django Admin编写

    ##Admin功能添加 ##效果图 ##添加时间日期 添加新的字段后需要重新数据移值操作 修改models.py auto_now是自定设置日期为当前日期 修改日期:null=True

  5. jmeter中判断数据库是否存在相应的记录

    jmeter判断数据库中是否存在相应记录可以使用count 配合case,然后再加个断言,后面用 变量JMeterThread.last_sample_ok来判断是否存在相应记录 select cas ...

  6. 持续集成CI

    一.CI 和 CD 持续集成是什么? 持续集成(Continuous integration,简称CI)指的是,频繁地(一天多次)将代码集成到主干.让产品可以快速迭代,同时还能保持高质量. 持续交付( ...

  7. SQL语法基础之UPDATE语句

    SQL语法基础之UPDATE语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看UPDATE语句的帮助信息 1>.查看UPDATE的帮助信息 mysql> ? ...

  8. js中得计算问题算式结果拼接成字符串怎么解决

    如题:经常遇到类似问题 一种:自定义的弱类型 var savNum=0; var num=$("#numU").val();//jsp页面获得得值 savNum=parseInt( ...

  9. xml实体注入学习

    好久没学习技术了  很多东西都忘了  复习一下 测试代码 <?php $xml = file_get_contents("php://input"); $data = sim ...

  10. JavaScript中的this指向规则

    首先,JavaScript的this指向问题并非传说中的那么难,不难的是机制并不复杂,而被认为不好理解的是逻辑关系和容易混淆的执行上下文.这篇博客也就会基于这两个不好理解的角度来展开,如要要严格的来对 ...