Spring Boot 是一个用于简化 Spring 应用初始搭建以及开发过程的框架。它通过约定大于配置的方式,大大减少了开发者需要编写的配置代码。本文将详细介绍 Spring Boot 的启动方式、核心注解的用法及含义、配置文件的书写格式以及模板引擎的使用方法。
Spring Boot 应用有多种启动方式,以下是最常见的三种方式:
这是最常见的启动方式。只需在主类中添加 @SpringBootApplication
注解,并提供 main
方法即可。
package com.ffyc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
@ComponentScan
注解显式指定扫描包 如果你有多个程序需要启动,那么给每个添加单独的注解就显得很麻烦,可以使用 @ComponentScan
注解显式指定需要扫描的包。
package com.ffyc.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("com.ffyc.service")
public class HelloService {
public static void main(String[] args) {
SpringApplication.run(HelloService.class, args);
}
}
@SpringBootApplication
@SpringBootApplication
是Spring Boot的核心注解,它集成了@SpringBootConfiguration
、@EnableAutoConfiguration
和@ComponentScan
,简化了配置过程。
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
@SpringBootApplication
结合
@SpringBootConfiguration
、@EnableAutoConfiguration
和@ComponentScan
,简化配置。
@ComponentScan
指定扫描组件的包路径,自动识别
@Component
、@Service
、@Repository
等注解的类。
@RestController
和@RequestMapping
用于创建RESTful控制器,处理HTTP请求。
@RestController
@RequestMapping("/hello")
public class HelloController {
@RequestMapping
public String sayHello() {
return "Hello, Spring Boot!";
}
}
application.properties
和 application.yml
Spring Boot 支持两种配置文件格式:application.properties
和 application.yml
。
application.properties
application.properties
是传统的 Properties 文件格式,每行配置一个键值对。
# application.properties
spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.suffix=.ftl
application.yml
application.yml
是 YAML 格式的配置文件,支持层次化配置,也是大多数常用的。
# application.yml
management:
name: zhangsan
age: 25
Spring Boot 会按照以下顺序加载配置文件:
application.properties
application.yml
命令行参数
环境变量
外部配置文件(如
application-xxx.properties
或application-xxx.yml
)
在 Spring Boot 中,静态资源默认放在 src/main/resources/static
目录下。Spring Boot 会自动将该目录下的资源映射到根路径 /
。
例如,如果你在 static
目录下有一个 index.html
文件,可以通过 http://localhost:8080/index.html
访问。
模板引擎是一种用于生成文本输出的工具,通常用于生成 HTML 页面。它允许你将代码和内容分离,从而使代码更清晰、更易于维护。
Thymeleaf: 一个现代化的模板引擎,支持 HTML5。
Freemarker: 一个强大的模板引擎,支持复杂的逻辑。
Velocity: 一个简单易用的模板引擎。
在 application.properties
中配置 Freemarker 的模板路径和后缀:
spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.suffix=.ftl
在 src/main/resources/templates
目录下创建 freemarkerIndex.ftl
文件:
Freemarker Index
${message}
package com.ffyc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
@Controller
public class FreemarkerIndexController {
@RequestMapping("/freemarkerIndex")
public String freemarkerIndex(Map result) {
result.put("message", "Hello, Freemarker!");
return "freemarkerIndex";
}
}
@Value
注解进行页面渲染@Value
注解package com.ffyc.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MemberService {
@Value("${management.name}")
private String name;
@Value("${management.age}")
private String age;
@RequestMapping("/management")
public String management() {
return name + "--" + age;
}
}
注意页面现在只需要这种写法就可以,${message}
Freemarker Index
${message}
介绍了 Spring Boot 的启动方式、核心注解的用法及含义、配置文件的书写格式以及模板引擎 Freemarker 的使用方法。通过这些内容,你应该能够更好地理解和应用 Spring Boot 进行开发。希望这篇博客能帮助你更好地理解 Spring Boot 的各种概念和用法。如果你有任何问题或建议,欢迎在评论区留言!