1. 什么是Spring Boot
Spring Boot是由Pivotal团队提供的一套快速开发框架,旨在简化Spring应用的初始搭建以及开发过程。Spring Boot通过提供一系列预配置的组件,简化了Spring应用的配置和部署。
2. Spring Boot的核心功能
-
自动配置:Spring Boot通过
@EnableAutoConfiguration
和@ComponentScan
注解实现自动配置,自动扫描并注册Bean。 -
独立运行:Spring Boot应用可以打包成JAR文件,用Java -jar命令运行。
-
生产就绪:Spring Boot应用天生就是为生产环境准备的,提供了很多生产就绪的特性,如健康检查、指标监控等。
-
无代码生成:Spring Boot提倡零XML配置,通过注解和Java配置类来配置Bean。
-
依赖管理:Spring Boot通过Maven或Gradle管理项目依赖,简化了依赖管理。
Spring Boot项目结构
一个典型的Spring Boot项目结构如下:
/your-project
/src
/main
/java
/com/example/demo
DemoApplication.java
/resources
application.properties
test
/java
/com/example/demo
DemoApplicationTests.java
-
src/main/java
:包含Java源代码。 -
src/main/resources
:包含配置文件和静态资源。 -
src/test/java
:包含测试代码。
Spring Boot配置
Spring Boot使用application.properties
或application.yml
文件进行配置。
application.properties
spring.application.name=demo
server.port=8080
-
spring.application.name
:设置应用名称。 -
server.port
:设置应用端口。
Spring Boot Bean
在Spring Boot中,通过注解可以将类注册为Bean。
DemoService.java
package com.example.service;
import org.springframework.stereotype.Service;
@Service
public class DemoService {
public String sayHello() {
return "Hello, World!";
}
}
-
@Service
注解将DemoService
类注册为Bean。
DemoController.java
package com.example.controller;
import com.example.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
@Autowired
private DemoService demoService;
@GetMapping("/")
public String home() {
return demoService.sayHello();
}
}
-
@Autowired
注解自动注入DemoService
Bean。
Spring Data JPA
Spring Data JPA提供对JPA的支持,简化了数据库操作。
DemoRepository.java
package com.example.repository;
import org.springframework.data.jpa.repository.JpaRepository;
public interface DemoRepository extends JpaRepository<String, String> {
}
-
JpaRepository
提供了基本的数据库操作方法。
DemoService.java
package com.example.service;
import com.example.repository.DemoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class DemoService {
@Autowired
private DemoRepository demoRepository;
public String sayHello() {
return demoRepository.findAll().toString();
}
}
-
@Autowired
注解自动注入DemoRepository
Bean。
Spring MVC
Spring MVC是Spring框架的一部分,用于构建Web应用程序。
DemoController.java
package com.example.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
@GetMapping("/")
public String home() {
return "Hello, World!";
}
}
-
@RestController
注解定义一个REST控制器。 -
@GetMapping("/")
方法定义一个GET请求处理方法。
Spring Boot高级特性
1. Spring Profiles
Spring Profiles允许您为不同的环境定义不同的配置。
application-dev.properties
server.port=8081
application-prod.properties
server.port=8080
-
使用
--profile
参数指定激活的Profile。
java -jar -Dspring.profiles.active=dev your-application.jar
2. Spring Boot Actuator
Spring Boot Actuator提供了监控和管理Spring Boot应用的功能。
application.properties
management.endpoints.web.exposure=*.*
management.endpoints.health.show-details=always
-
配置Actuator端点暴露和健康检查详细信息。
3. Spring Boot DevTools
Spring Boot DevTools提供了热部署、实时加载等功能。
pompatibility
-
Spring Boot 2.x与Spring 5.x兼容。
-
Spring Boot 3.x将与Spring 6.x兼容。
Spring Boot测试
Spring Boot提供了强大的测试支持。
DemoApplicationTests.java
package com.example;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest
public class DemoApplicationTests {
@Test
public void contextLoads() {
assertThat(true).isTrue();
}
}
-
@SpringBootTest
注解提供Spring Boot测试环境。 -
@Test
注解定义一个测试方法。
Spring Boot部署
1. 打包应用
使用Maven或Gradle打包Spring Boot应用。
mvn clean package
3. 部署到服务器
将打包后的JAR文件上传到服务器,并使用Java命令运行。
扩展功能
1. Spring Security
Spring Security提供了强大的安全支持。
DemoSecurityConfig.java
package com.example.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
@Configuration
@EnableWebSecurity
public class DemoSecurityConfig {
http(http)
.authorizeRequests()
.antMatchers("/home").permitAll()
.anyRequest().authenticated()
.and()
.formLogin();
}
-
@EnableWebSecurity
注解启用Spring Security。 -
HttpSecurity
配置安全规则。
2. Spring Data MongoDB
Spring Data MongoDB提供了对MongoDB的支持。
DemoRepository.java
package com.example.repository;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface DemoRepository extends MongoRepository<String, String> {
}
-
MongoRepository
提供了基本的MongoDB操作方法。
DemoService.java
package com.example.service;
import com.example.repository.DemoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class DemoService {
@Autowired
private DemoRepository demoRepository;
public String sayHello() {
return demoRepository.findAll().toString();
}
}
-
@Autowired
注解自动注入DemoRepository
Bean。
3. Spring Boot和Vue.js集成
Spring Boot可以与Vue.js集成,提供前后端分离的开发模式。
index.html
<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
</head>
<body>
<div id="app"></div>
<script src="/static/js/chunk-vendors.js"></script>
<script src="/static/js/chunk-app.js"></script>
</body>
</html>
-
在HTML中引用Vue构建的JavaScript文件。
DemoController.java
package com.example.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
@GetMapping("/vue")
public String vue() {
return "Welcome to Vue.js!";
}
}
-
@GetMapping("/vue")
方法提供Vue页面。
通过这些扩展功能,您可以构建更强大的Spring Boot应用。希望这些详细信息和代码示例能帮助您更好地理解和使用Spring Boot。