一、SpringBoot入门
1、SpringBoot简介
Spring Boot是一个开源的Java框架,它基于Spring框架并设计用于简化和加速Java应用程序的构建过程。Spring Boot通过提供一个默认的配置和一套约定大于配置的原则,极大地简化了Spring应用程序的开发、部署和管理
(一)Spring Boot有哪些特点和优势?
- 简化配置:Spring Boot采用约定大于配置的原则,持外部化配置,灵活性很高。
- 内嵌容器:Spring Boot内置了多个常用的Servlet容器,如Tomcat、Jetty等,直接运行,无需额外安装和配置容器。
- 起步依赖:提供了一系列预定义的起步依赖,用于快速集成常用的功能和组件。
- 自动配置:根据依赖的起步依赖和配置进行自动配置,根据应用程序的需求自动装配所需的Bean、组件和功能。
- 生产就绪:Spring Boot提供了大量的生产级别特性和监控工具,如健康检查、性能指标、外部化配置等。
- 简化测试:Spring Boot提供了丰富的测试支持,包括单元测试、集成测试、端到端测试等。
2、快速搭建第一个SpringBoot项目
-
打开Spring Initializr网站(https://start.spring.io/)。
-
在页面上选择项目的基本信息,包括构建工具(Maven或Gradle)、Spring Boot版本等。
-
在“Dependencies”(依赖)部分,选择你需要的起步依赖。例如,如果你要创建一个简单的Web应用程序,可以选择“Spring Web”起步依赖。
-
点击“Generate”按钮,下载生成的项目压缩文件。
-
解压缩下载的项目压缩文件,并使用IDE(如IntelliJ IDEA、Eclipse等)导入解压后的项目。
-
在IDE中,等待依赖的下载和构建完成。
-
打开项目中的主应用程序类(通常是名为
Application.java
或Application.kt
的类),这是Spring Boot项目的入口点。 -
在主应用程序类中,添加
@SpringBootApplication
注解,这是一个组合注解,包含了@Configuration
、@EnableAutoConfiguration
、@ComponentScan
等注解。
import org.springframework.boot.SpringApplication;
import ofg.springframework.boot,autoconfigure.SpringBootApplication;
@SpringBootApplion
public class Application{
public static void main(String[] args){
SpringApplication.run(Application.class,args);
}
}
-
创建其他你需要的类、方法和资源文件,根据你的具体需求进行编码。
-
运行应用程序,可以使用IDE提供的运行工具,或者在命令行中执行相应的命令。
-
访问应用程序的URL,查看应用程序是否正常运行。对于一个简单的Web应用程序,你可以在浏览器中访问
http://localhost:8080/
。
3、SpringBoot入门HelloWorld
案例:
import org.springframework.boot.SpringApplication;
omport ofg.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootAppltion
public class HelloworldAppation{
public static void main(String[] args){
SpringApplication.run(HelloWorldApplication.class,args);
}
}
@RestController
class HelloworldController{
@GetMapping("/hello")
public String hello(){
return "Hello,World!";
}
}
示例包含了一个主应用程序类HelloWorldApplication
和一个控制器类HelloWorldController。
在HelloWorldController
中,我们定义了一个hello()
方法,使用@GetMapping
注解将该方法映射到URL路径/hello
上,并返回字符串"Hello, World!"。
二、SpringBoot整合web开发
总结:
|
1、整合Servlet
- 创建Servlet类:首先,创建一个继承自
javax.servlet.http.HttpServlet
的Servlet类,实现相应的doGet()、doPost()等方法来处理请求。
@WebServlet("/example")
public class ExapleServet extends HttpServlet{
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletExecetion,IOException{
}
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServleltException,IOException{
}
}
2.配置Servlet:在Spring Boot项目中,使用@ServletComponentScan
注解启用Servlet组件的自动扫描。然后,在Servlet类上加上@WebServlet
注解,并指定对应的URL映射。
@ServletComponentScan
@SpringBootApplication
public class Application{
public static void main(String[] args){
SpringApplication.run(Application.class,args);
}
}
3.运行应用程序:运行Spring Boot应用程序,Servlet将被自动注册并生效。
4.配置Servlet的初始化参数:如果需要为Servlet设置初始化参数,可以在Servlet类上使用@WebServlet
注解的initParams属性进行配置。
@WebServlet(urlPatterns="/example",initParams={
@WebInitParam(name = "paramName", value = "paramValue")
})
public class ExampleServlet extends HttpServlet{
//Servlet的具体实现
}
这样,你就成功地将Servlet整合到了Spring Boot项目中。当访问配置的URL时,请求将会被映射到对应的Servlet类进行处理。需要注意的是,Spring Boot会使用内嵌的Servlet容器(默认是Tomcat)来运行应用程序,因此无需手动配置web.xml文件。
2、整合Filter
- 创建Filter类:首先,创建一个实现
javax.servlet.Filter
接口的Filter类,并实现相应的doFilter()
方法来处理请求。
public class ExampleFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
// 初始化逻辑
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
// 过滤逻辑
filterChain.doFilter(servletRequest, servletResponse);
}
public void destroy() {
// 销毁逻辑
}
}
2.注册Filter:在Spring Boot项目中,可以通过创建一个FilterRegistrationBean
的Bean来注册Filter,并设置其URL映射和顺序。
@Configuration
public class FilterConfig{
@Bean
public FilterRegistrationBean<ExampleFilter> exampleFilterRtration(){
FliterRegisonBean<Exampler> registrationBean = new FiltertrationBean<>();
regiatrationBean.setFilter(new ExampleFilter());
registrationBean.addUrlPatterns("/example");
registrationBean.setOrder(1);
return registrationBean;
}
}
3.配置Filter的顺序和映射路径:在上述代码中,使用registrationBean.setOrder()
来设置Filter的执行顺序,数值越低优先级越高。使用registrationBean.addUrlPatterns()
来设置Filter的URL映射路径。
4.运行应用程序:运行Spring Boot应用程序,Filter将会自动注册并生效。
在整合Filter时,也可以使用@ServletComponentScan
注解来启用Filter组件的自动扫描。如果使用此注解,则无需在配置类中手动注册Filter,只需要在Filter类上使用@WebFilter
注解,并指定对应的URL映射和顺序。
@ServletComponentScan
@SpringBootApplication
public class Application{
public static void main(String[] args){
SpringApplication.run(Application.class,args);
}
}
@WebFilter(urlPatterns = "/example",filterName = "exampleFilter")
public class ExampleFilter implements Filter{
//Filter的实现
}
这样,你就成功地将Filter整合到了Spring Boot项目中。当访问配置的URL时,请求将会经过Filter进行处理。与Servlet类似,Spring Boot会使用内嵌的Servlet容器(默认是Tomcat)来运行应用程序,无需手动配置web.xml文件。
3、整合Listener
1.创建Listener类:首先,创建一个实现javax.servlet.ServletContextListener
接口的Listener类,并实现相应的contextInitialized()
和contextDestroyed()
方法来处理初始化和销毁逻辑。
public class ExampleList implements ServletContextListner{
public void contextInitialized(ServletContextEvent servletContextEvent){
//初始化逻辑
}
public void contextInitialized(ServletContextEvent servletContextEvent){
//销毁逻辑
}
}
2.注册Listener:在Spring Boot项目中,可以通过创建一个ServletListenerRegistrationBean
的Bean来注册Listener。
@Configuration
public class ListenerConfig {
@Bean
public ServletListenerRegistrationBean<ExampleListener> exampleListenerRegistrationBean() {
ServletListenerRegistrationBean<ExampleListener> registrationBean = new ServletListenerRegistrationBean<>();
registrationBean.setListener(new ExampleListener());
return registrationBean;
}
}
3.运行应用程序:运行Spring Boot应用程序,Listener将会自动注册并生效。
与注册Filter类似,你也可以使用@ServletComponentScan
注解启用Listener组件的自动扫描。如果使用此注解,则无需在配置类中手动注册Listener,只需要在Listener类上使用@WebListener
注解即可。
@ServletComponentScan
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@WebListener
public class ExampleListener implements ServletContextListener {
// Listener的实现
}
这样,你就成功地将Listener整合到了Spring Boot项目中。当应用程序启动或关闭时,Listener将会触发相应的事件处理逻辑。
三、SpringBoot视图层技术
1、整合jsp技术
- 将JSP相关的依赖添加到项目的构建文件中。对于Maven项目,需要在
pom.xml
文件中添加以下依赖:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
2.在application.properties
(或application.yml
)中配置JSP视图解析器和JSP文件的位置。将以下配置添加到application.properties
文件中:
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
或者,在application.yml
文件中添加以下配置:
spring:
mvc:
view:
prefix: /WEB-INF/views/
suffix: .jsp
3.创建JSP文件:在项目的src/main/webapp/WEB-INF/views/
目录下创建JSP文件,例如hello.jsp
。
<html>
<body>
<h1>Hello, JSP!</h1>
</body>
</html>
4.创建Controller类:创建一个用于处理请求的Controller类,并添加一个方法来映射到JSP页面。
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello(Model model) {
model.addAttribute("message", "Hello, JSP!");
return "hello";
}
}
5.运行应用程序:运行Spring Boot应用程序,访问http://localhost:8080/hello
,将会显示JSP页面的内容。
2、整合freemarker
- 添加FreeMarker依赖:在项目的构建文件中(如Maven的
pom.xml
)添加FreeMarker的依赖项。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2.创建FreeMarker模板文件:在src/main/resources/templates/
目录下创建FreeMarker模板文件,例如hello.ftl
。
<html>
<body>
<h1>Hello, FreeMarker!</h1>
</body>
</html>
3.创建Controller类:创建一个用于处理请求的Controller类,并添加一个方法来映射到FreeMarker模板。
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello(Model model) {
model.addAttribute("message", "Hello, FreeMarker!");
return "hello";
}
}
4.运行应用程序:运行Spring Boot应用程序,访问http://localhost:8080/hello
,将会显示FreeMarker模板的内容。
3、整合Thymeleaf
- 添加Thymeleaf依赖:在项目的构建文件中(如Maven的
pom.xml
)添加Thymeleaf的依赖项。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.创建Thymeleaf模板文件:在src/main/resources/templates/
目录下创建Thymeleaf模板文件,例如hello.html
。
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<h1 th:text="${message}"></h1>
</body>
</html>
3.创建Controller类:创建一个用于处理请求的Controller类,并添加一个方法来映射到Thymeleaf模板。
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello(Model model) {
model.addAttribute("message", "Hello, Thymeleaf!");
return "hello";
}
}
4.运行应用程序:运行Spring Boot应用程序,访问http://localhost:8080/hello
,将会显示Thymeleaf模板的内容。