Spring Boot 学习笔记(三)——特性
2017-04-09 11:13
435 查看
启动失败
如果你的应用启动失败,注册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.com my.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)自定义日志格式
根据不同的日志框架,编写不同的配置文件Logging System | Customization |
---|---|
Customization | logback-spring.xml, logback-spring.groovy, logback.xml or logback.groovy |
Log4j2 | log4j2-spring.xml or log4j2.xml |
JDK (Java Util Logging) | logging.properties |
开发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.*; @JsonComponent public 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、ListenerJSP支持
可参考代码:https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-samples/spring-boot-sample-web-jsp1、打包方式需要改成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
相关文章推荐
- Springboot学习笔记之Springboot 2.0特性
- Spring学习笔记2——高级特性
- Spring boot 学习笔记—带来的好处(二)
- spring boot 学习笔记(001) 新建Eclipse工程
- spring boot 学习笔记(009)application.yml
- spring-boot学习笔记
- Uboot学习笔记②---(bootloader的一些共同特性、uboot启动过程、编译环境、地址规划设计)
- spring学习笔记:ioc容器高级特性
- Spring学习笔记2——高级特性
- spring boot 学习笔记(007)pom.xml修改
- spring boot 学习笔记(1)
- SpringBoot 学习笔记 2
- Spring学习笔记2——高级特性
- Spring Boot学习笔记-快速示例
- spring boot 学习笔记(003) 静态资源(html/js等文件)
- spring学习笔记(一)事务的管理和传播特性
- Spring boot 学习笔记 ---分分钟构建一个web程序(一)
- Spring Boot学习笔记-SQL数据库使用
- [Spring入门学习笔记][Spring Boot]
- spring boot 学习笔记(008)Apache camel