Java:SpringBoot--内容概述
1. Spring Boot 中的重要概念
1.1 @SpringBootApplication
@SpringBootApplication
是 Spring Boot 核心注解之一,用于标识 SpringBoot 主类。
@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}
1.2 application.properties
application.properties
是 Spring Boot 应用程序的配置文件。
# Server
server.port=8080
server.servlet.context-path=/api# Database
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=root# Logging
logging.level.com.example=DEBUG
1.3 Spring Boot starter
Spring Boot starter 是一组相关依赖的集合,可以简化以来管理,只需添加相应的 starter,自动引入它们所需要的全部依赖。
<!-- Web -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency><!-- JPA -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency><!-- Security -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId>
</dependency>
2. Spring Boot 常用组件
2.1 Spring MVC
Spring MVC 是一种基于 Java 的 Web 应用程序框架,它支持模型-视图-控制器(MVC)模式。
2.2 Spring Data
Spring Data 是 Spring Framework 的一部分,它提供了简化数据访问和持久化的方法。
2.3 Spring Security
Spring Security 是 Spring Framework 的一个模块,它提供了认证和授权的支持。
2.4 Spring Boot Actuator
Spring Boot Actuator 可以让你轻松地监视和管理应用程序,例如查看健康状态、监视 HTTP 请求、查看日志等。
3. Spring Boot 中的 AOP
AOP(面向切面编程)是一种编程范式,它允许将横跨多个对象的通用行为单独封装为可重用的模块,从而实现可重用性和避免代码复制。
Spring Boot 中的 AOP 采用注解驱动的方式。
@Aspect
@Component
public class LoggingAspect {@Before("execution(* com.example.demo.service.*.*(..))")public void logBefore(JoinPoint joinPoint) {System.out.println("Logging before " + joinPoint.getSignature().getName() + " method");}@After("execution(* com.example.demo.service.*.*(..))")public void logAfter(JoinPoint joinPoint) {System.out.println("Logging after " + joinPoint.getSignature().getName() + " method");}}
4. Spring Boot 中的异常处理
Spring Boot 提供了一种灵活的异常处理机制,使我们可以捕获和处理不同类型的异常。
@ControllerAdvice
public class GlobalExceptionHandler {@ExceptionHandler(Exception.class)public ResponseEntity<ErrorResponse> handleException(Exception ex) {ErrorResponse errorResponse = new ErrorResponse("500", ex.getMessage());return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);}}
5. Spring Boot 中的日志记录
在 Spring Boot 中,使用日志记录框架是非常简单的,只需要添加相应的依赖以及配置即可。
<!-- Logback -->
<dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId>
</dependency>
# Logging
logging.level.com.example=DEBUG
logging.file=logs/application.log
6. Spring Boot 中的测试
Spring Boot 提供了多种测试工具,只需要在 pom.xml 中的 test 作用域中添加相应的依赖即可。
<!-- JUnit 5 -->
<dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><scope>test</scope>
</dependency><!-- Mockito -->
<dependency><groupId>org.mockito</groupId><artifactId>mockito-core</artifactId><scope>test</scope>
</dependency>
7. Spring Boot 中的部署
Spring Boot 一般使用内嵌的 Servlet 容器运行应用程序,例如 Tomcat、Jetty 或 Undertow。如果需要将应用程序部署到外部 Servlet 容器,则需要将打包方式改为 WAR 包,并在其它配置上做相应调整。
<packaging>war</packaging><!-- Tomcat -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><scope>provided</scope>
</dependency>
然后,在 Servlet 容器中部署 WAR 包即可。