Spring Boot 学习笔记(三)——特性
来源:互联网 发布:淘宝店家论坛 编辑:程序博客网 时间:2024/05/17 02:50
启动失败
如果你的应用启动失败,注册FailureAnalyzers有可能会提供专门的错误信息和解决这个问题的具体行动。例如,如果你启动一个8080端口的web应用并且这个端口已经被占用,你应该会看到类似于下面的内容:
***************************APPLICATION FAILED TO START***************************Description:Embedded servlet container failed to start. Port 8080 was already in use.Action:Identify and stop the process that's listening on port 8080 or configure this application to listen on another port.
如果没有任何失败分析器的话,可以开启debug(默认的
日志级别是INFO
),属性名为“org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer”,如果是通过java -jar
运行的,还可以使用--debug
进行开启,如java -jar myproject-0.0.1-SNAPSHOT.jar --debug
自定义Banner
默认情况下,Spring Boot的打印内容如下
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.2.RELEASE)
如果要修改,可在classpath下创建一个文件,名称为“banner.txt”,编写自己要的内容即可。
关闭banner
public static void main(String[] args) { SpringApplication app = new SpringApplication(MySpringConfiguration.class); app.setBannerMode(Banner.Mode.OFF); app.run(args);}
应用事件与监听器
在ApplicationContext创建之前实际上会触发一些事件,因此你不能使用@Bean来注册这些监听器。你可以通过SpringApplication.addListeners()
或SpringApplicationBuilder.listeners()
来注册这些监听器。
如果你想自动注册这些监听器,不管上下文的创建方式,你可以在你的工程中添加META-INF/spring.factories文件,并通过org.springframework.context.ApplicationListener作为key来引用你的监听器。
当应用运行时,运用的发送顺序
1. ApplicationStartingEvent
2. ApplicationEnvironmentPreparedEvent
3. ApplicationPreparedEvent
4. ApplicationReadyEvent
5. ApplicationFailedEvent
web环境
SpringApplication
将试图创建一个正确类型的ApplicationContext
。如果是web应用,创建一个AnnotationConfigEmbeddedWebApplicationContext
,否则创建一个AnnotationConfigApplicationContext
。
访问应用参数
启动应用时,我们会通过SpringApplication.run(args)
添加启动参数args,我们可通过注入org.springframework.boot.ApplicationArguments
来获取启动参数。
在应用启动过程中做一些事
实现ApplicationRunner
或CommandLineRunner
接口。
应用退出拦截
使用@PreDestroy注解或实现org.springframework.boot.ExitCodeGenerator
接口
可变配置
可通过“application.properties”(或YAML 变量)对默认配置进行修改。
通过命令行对配置进行修改,即SpringApplication.run(MainApplicion.class, args)
中的args
参数,如--server.port=9000
则启动端口变成了9000。
配置随机数
my.secret=${random.value}my.number=${random.int}my.bignumber=${random.long}my.uuid=${random.uuid}my.number.less.than.ten=${random.int(10)}my.number.in.range=${random.int[1024,65536]}
把配置文件中的配置绑定到Bean中,可使用@value
,还可以使用@ConfigurationProperties
,如下
my.servers[0]=dev.bar.commy.servers[1]=foo.bar.com
@ConfigurationProperties(prefix="my")public class Config { private List<String> servers = new ArrayList<String>(); public List<String> getServers() { return this.servers; }}
profile(不同环境读取不同配置)
不同环境的配置设置一个配置文件,例如:dev环境下的配置配置在application-dev.properties中;pro环境下的配置配置在application-pro.properties中。
各个环境公共的配置写在application.properties中。
各个模块独有的配置配置在自己的application-{xxx}.properties文件中。
指定某一环境运行,使用spring.profiles.active
进行配置
SpringApplication.run(MainApplicion.class, "--spring.profiles.active=dev");
日志
说明
默认日志框架是“Commons Logging”,但如果是用“Starters”,那么默认日志框架是“Logback”
默认打印级别是INFO
,若要开启,有两种方式
java -jar myapp.jar --debug
在application.properties中添加debug=true
文件输出
默认是只打打印在控制台上,不输出到文件,可配置。
可在application.properties中配置logging.file
或 logging.path
日志级别
格式:logging.level.*=LEVEL,LEVEL的取值有(TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF)
自定义日志格式
根据不同的日志框架,编写不同的配置文件
开发web应用
Spring MVC框架
使用HttpMessageConverter
转换HTTP中的requests 和 responses
使用@JsonComponent注解来实现JSON的序列号跟反序列化
import java.io.*;import com.fasterxml.jackson.core.*;import com.fasterxml.jackson.databind.*;import org.springframework.boot.jackson.*;@JsonComponentpublic class Example { public static class Serializer extends JsonSerializer<SomeObject> { // ... } public static class Deserializer extends JsonDeserializer<SomeObject> { // ... }}
静态资源
默认配置的 /** 映射到 /static (或/public、/resources、/META-INF/resources),上面的 static、public、resources 等目录都在 classpath: 下面(如 src/main/resources/static)。
优先级顺序为:META/resources > resources > static > public
如:src/main/resources/static/js/test.js的访问路径为http://localhost:8080/js/test.js
注意:不要使用src/main/webapp
文件夹,因为打成jar包时,这个文件夹不会被打包进去。
当我们的资源内容发生改变时,由于浏览器缓存,用户本地的资源还是旧资源,为了防止这种情况发生导致的问题。Spring在解决这种问题方面,提供了2种解决方式:资源名称md5方式、资源名称md5方式。
错误处理
自定义错误页面
src/ +- main/ +- java/ | + <source code> +- resources/ +- static/ +- error/ | +- 404.html | +- 5xx.ftl +- <other public assets>
嵌入的Servlet容器支持
可使用@WebServlet, @WebFilter, @WebListener注解,还有@ServletComponentScan注解扫描来支持Servlet、Filter、Listener
JSP支持
可参考代码:https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-samples/spring-boot-sample-web-jsp
1、打包方式需要改成war。
2、添加必要的依赖
<dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId></dependency><!-- Provided --><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope></dependency><dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope></dependency>
3、新建文件夹src/main/webapp/WEB-INF/jsp/
。
4、application.properties文件添加配置
spring.mvc.view.prefix=/WEB-INF/jsp/spring.mvc.view.suffix=.jsp
5、创建Controller跟jsp文件
其他特性
其他特性是一些比较大的话题,将以独立文章的方式呈现
- Security
- SQL databases
- NoSQL technologies
- Caching
- Messaging
- Validation
- Sending email
- JTA
- JMX
- Testing
- Spring Boot 学习笔记(三)——特性
- Spring boot 学习笔记(三)
- Spring Boot 学习笔记(三)
- Spring读书学习笔记(二)——Spring boot
- Spring boot学习笔记 001——初识Spring boot
- (三) Spring Boot学习笔记之Spring Boot整合Logback
- Spring Boot学习笔记-整合Mybatis (三)
- spring boot学习笔记——踩坑
- spring boot 学习笔记(05)——热部署 之spring-boot-devtools
- Spring Boot 学习笔记(一)——入门
- Spring Boot 学习笔记(二)——使用
- Spring boot 学习笔记—带来的好处(二)
- Spring学习笔记—高级特性
- Spring Boot学习(三)
- spring boot 自学笔记(三) Redis集成—RedisTemplate
- spring boot 自学笔记(三) Redis集成—RedisTemplate
- spring boot 自学笔记(三) Redis集成—RedisTemplate
- spring boot 笔记(三):配置redis
- ARM汇编指令特点
- Java动态录入弹球
- java数据类型转换(强制转换)
- Python与机器学习3
- 谈谈学习element UI + vue.js的感受
- Spring Boot 学习笔记(三)——特性
- hibernate懒加载
- 邻接矩阵的幂
- js
- 随机失活 dropout直观理解
- PHP Class中public,private,protected,static的区别
- DFS
- 数据库练习题笔记
- 程序员面试经典--链表分割