重写全局配置

如果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. 4.1、实现4个LED灯同时闪烁

    图中可以看出,P1的0.1.3.4引脚分别连接着4个LED.控制引脚状态,来控制LED. #include "ioCC2530.h" //引用CC2530头文件 /******** ...

  2. (数学) PTA 1005 继续(3n+1)猜想 (25 分)

    1005 继续(3n+1)猜想 (25 分) 卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程 ...

  3. (find) nyoj5-Binary String Matching

    5-Binary String Matching 内存限制:64MB 时间限制:3000ms 特判: No通过数:232 提交数:458 难度:3 题目描述: Given two strings A ...

  4. php 4种传值方式

    我们定义page01.php和page02.php两个php文件,将page01中的内容想办法传递到page02,然后供我们继续使用. 第一种:     使用客户端浏览器的cookie.cookie很 ...

  5. jmeter计算身份证校验位

    idcard_no='111111198101017000' ; idcard_no_array = idcard_no.substring(0,17).toCharArray(); int[] c ...

  6. passat QA / error code 20190210

    s 帕萨特B5 技术资料下载 https://pan.baidu.com/s/1KXYly7eGDUSI5QiLcz8fiQ 提取码: 1i7u 星期日,10,二月,2019,17:16:16:317 ...

  7. twitter分布式主键id生成器

    pom <!--生成id--> <dependency> <groupId>com.github.bingoohuang</groupId> <a ...

  8. cors解决跨域

    什么是cors CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing). 它允许浏览器向跨源服务器,发出XMLHttpReq ...

  9. windows环境配置计划任务让weblogic的servers开机启动【原】

    准备脚本 注意如果weblogic在D盘,那么以下cmd中的所有C:都要替换成D: , 因为windows需要切盘符. 启动weblogic管理服务adminServer 的文件: startAdmi ...

  10. 关于js事件执行顺序

    关于js事件执行顺序小技巧 js事件执行顺序是js中一个老生常谈的一个话题, 聊这个话题之前我们先谈谈怎么给页面元素绑定我们需要的事件 1.给页面元素绑定事件 a)直接在元素上面加上需要绑定的事件,如 ...