SpringBoot的Web配置
重写全局配置
如果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配置的更多相关文章
- springboot情操陶冶-web配置(九)
承接前文springboot情操陶冶-web配置(八),本文在前文的基础上深入了解下WebSecurity类的运作逻辑 WebSecurityConfigurerAdapter 在剖析WebSecur ...
- springboot情操陶冶-web配置(七)
参数校验通常是OpenApi必做的操作,其会对不合法的输入做统一的校验以防止恶意的请求.本文则对参数校验这方面作下简单的分析 spring.factories 读者应该对此文件加以深刻的印象,很多sp ...
- springboot情操陶冶-web配置(四)
承接前文springboot情操陶冶-web配置(三),本文将在DispatcherServlet应用的基础上谈下websocket的使用 websocket websocket的简单了解可见维基百科 ...
- springboot情操陶冶-web配置(二)
承接前文springboot情操陶冶-web配置(一),在分析mvc的配置之前先了解下其默认的错误界面是如何显示的 404界面 springboot有个比较有趣的配置server.error.whit ...
- springboot情操陶冶-web配置(三)
承接前文springboot情操陶冶-web配置(二),本文将在前文的基础上分析下mvc的相关应用 MVC简单例子 直接编写一个Controller层的代码,返回格式为json package com ...
- SpringBoot学习(七)-->SpringBoot在web开发中的配置
SpringBoot在web开发中的配置 Web开发的自动配置类:在Maven Dependencies-->spring-boot-1.5.2.RELEASE.jar-->org.spr ...
- 从Spring到SpringBoot构建WEB MVC核心配置详解
目录 理解Spring WEB MVC架构的演变 认识Spring WEB MVC 传统时代的Spring WEB MVC 新时代Spring WEB MVC SpringBoot简化WEB MVC开 ...
- springboot的Web开发-Web相关配置
一:Spring Boot提供自动配置 通过查看WebMvcAutoConfiguration及WebMvcProperties的源码,可以发现Spring Boot为我们提供了如下的自动配置. 1, ...
- SpringBoot的Web开发
一.创建Web项目 创建的时候勾选对应web选项即可,会自动引入相应的starter,pom如下: <dependency> <groupId>org.springframew ...
随机推荐
- 【内核】Linux内核Initrd机制解析,内核更新步骤,grub配置说明
什么是Initrd initrd的英文含义是 boot loader initialized RAM disk,就是由boot loader初始化的内存盘.在 linux内核启动前, boot loa ...
- Luogu P2770 航空路线问题
题目链接 \(Click\) \(Here\) 本来想调剂心情没想到写了那么久,还被\(dreagonm\)神仙嘲讽不会传纸条,我真是太弱了\(QAQ\)(原因:最开始写最大费用最大流一直想消圈,最后 ...
- linux 优化(一)
uptime up 表示系统正在运行 10:54:19 表示当前时间 8 min 表示系统启动的总时间 1 user 表示正在登陆用户数 load average 表示系统平均负载,最后三个数字呢,依 ...
- golang 热升级
需求场景 干净利落地升级正在运行的agent程序.适用于Devops团队. 目标: 不关闭现有连接:例如我们不希望关掉已部署的运行中的程序.但又想不受限制地随时升级服务. 新的进程要能够启动并替换掉旧 ...
- 2017-12-18python全栈9期第三天第二节之str常用操作方法及for循环之判断字母数字组成
#!/user/bin/python# -*- coding:utf-8 -*-name = 'zd123'print(name.isalnum()) #由数字或字母组成print(name.isal ...
- log4j 基础教程【转】
参考引用自: http://javacrazyer.iteye.com/blog/1135493 我的git地址: https://git.oschina.net/KingBoBo/Log4JDemo ...
- [Android] Android 使用Greendao gradle 出现 Error:Unable to find method 'org.gradle.api.tasks.TaskInputs.file(Ljava/lang/Object;)
Android 使用Greendao gradle 出现 Error:Unable to find method 'org.gradle.api.tasks.TaskInputs.file(Ljava ...
- MyEclipse创建SWT程序
创建项目:NewProject->WindowsBuilder->SWT Designer->SWT/JFACE Java Project添加SWT窗口:New->Window ...
- UDP客户/服务器程序所用的套接字函数
- oracle 存储过程(包)的写法和执行
--in 代表输入参数,out 代表输出参数create or replace procedure myproc(id in int, v_message out varchar2) is--定义临时 ...