Skip to content

Spring Boot

luoml edited this page Jul 22, 2017 · 7 revisions

1. Logging

Spring Boot 支持日志框架:Java Util LoggingLog4J2Logback,默认使用 Logback 。

1.1. Logback

  • 默认配置文件方式
// application.properties
logging.file=spring-boot-logback.log
logging.level.root=WARN
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR
  • 引用外部配置文件

Spring Boot 默认会加载 classpath:logback-spring.xmlclasspath:logback-spring.groovy

// application.properties
// 自定义文件名
logging.config=classpath:my-logback.xml

// my-logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <property name="PATTERN" value="%d [%t] %p %c - %m%n" />

  <springProfile name="default">
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
      <file>spring-boot-log.log</file>
      <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>spring-boot-logback.%d{yyyyMMdd}.log</fileNamePattern>
        <maxHistory>30</maxHistory>
      </rollingPolicy>
      <encoder>
        <pattern>${PATTERN}</pattern>
      </encoder>
    </appender>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
      <encoder>
        <pattern>${PATTERN}</pattern>
      </encoder>
    </appender>
	
    <root level="debug">
      <appender-ref ref="FILE" />
      <appender-ref ref="STDOUT" />
    </root>
  </springProfile>

</configuration>

1.2. Log4j2

  1. 配置 POM 文件
  • 排除 spring-boot-starter-logging
<exclusions>
  <exclusion>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-logging</artifactId>
  </exclusion>
</exclusions>
  • 引入 spring-boot-starter-log4j2
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
  1. 配置 log4j2.xml
    Spring Boot 默认会加载 logging.config=classpath:log4j2-spring.xmllogging.config=classpath:log4j2.xml
// log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
  </Appenders>
  <Loggers>
    <Logger name="org.springframework.web.servlet" level="info">
      <AppenderRef ref="Console"/>
    </Logger>
    <Root level="error">
      <AppenderRef ref="Console"/>
    </Root>
  </Loggers>
</Configuration>

2. JSON

Spring Boot 默认使用 Jackson 。

2.1. 日期格式化

参考 http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#appendix

spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8

3. JSP

3.1. 添加 POM 依赖

<!-- web mvc、restful、jackson、aop、...... -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- servlet依赖 -->
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <scope>provided</scope>
</dependency>

<!-- JSP标准标签库 -->
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>jstl</artifactId>
</dependency>

<!-- tomcat支持 -->
<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.2. 配置 application.properties

spring.mvc.view.prefix=/WEB-INF/jsp
spring.mvc.view.suffix=.jsp

4. Servlet

在 Spring Boot 中,自定义 Servlet 有两种方式。

4.1. 代码注册方式

  1. 配置 POM 依赖
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  1. 自定义 Servlet ,继承 HttpServlet
public class MyServlet1 extends HttpServlet {
  private static final long serialVersionUID = -7344886891091037086L;

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    PrintWriter out = resp.getWriter();
    out.print("MyServlet1");
    out.flush();
    out.close();
  }
}
  1. 注册 Servlet
@Bean
public ServletRegistrationBean registerServlet() {
  return new ServletRegistrationBean(new MyServlet1(), "/servlet/myservlet1");
}

4.2. 注解注册方式

  1. 配置 POM 依赖
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  1. 自定义 Servlet ,继承 HttpServlet;注意增加 @WebServlet 注解
@WebServlet("/servlet/myservlet2")
public class MyServlet2 extends HttpServlet {
  private static final long serialVersionUID = -7344886891091037086L;

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    PrintWriter out = resp.getWriter();
    out.print("MyServlet2");
    out.flush();
    out.close();
  }
}
  1. 在 Spring Boot 启动类,启用 @ServletComponentScan,扫描 Servlet
@ServletComponentScan
@SpringBootApplication
public class SpringBootServletApplication {
  ......
}