引言
Spring Boot 是一个用于简化 Spring 应用初始搭建以及开发过程的框架。它通过约定大于配置的方式,大大减少了开发者需要编写的配置代码。本文将详细介绍 Spring Boot 的启动方式、核心注解的用法及含义、配置文件的书写格式以及模板引擎的使用方法。
Spring Boot 的启动方式
Spring Boot 应用有多种启动方式,以下是最常见的三种方式:
1.直接运行主类的 main 方法
        这是最常见的启动方式。只需在主类中添加 @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);}
}2.使用 @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);}
} 3.使用@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 {@RequestMappingpublic 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=.ftlapplication.yml
 
application.yml 是 YAML 格式的配置文件,支持层次化配置,也是大多数常用的。
# application.yml
management:name: zhangsanage: 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 访问。
模板引擎:Freemarker
什么是模板引擎
模板引擎是一种用于生成文本输出的工具,通常用于生成 HTML 页面。它允许你将代码和内容分离,从而使代码更清晰、更易于维护。
常用的模板引擎
Thymeleaf: 一个现代化的模板引擎,支持 HTML5。
Freemarker: 一个强大的模板引擎,支持复杂的逻辑。
Velocity: 一个简单易用的模板引擎。
Freemarker 的配置与使用
配置 Freemarker
在 application.properties 中配置 Freemarker 的模板路径和后缀:
spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.suffix=.ftl创建 Freemarker 模板
在 src/main/resources/templates 目录下创建 freemarkerIndex.ftl 文件:
<!doctype html>
<html lang="en">
<head><meta charset="UTF-8"><title>Freemarker Index</title>
</head>
<body>
${message}
</body>
</html>控制器类
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<String, String> 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;}
}在 Freemarker 模板中使用变量
注意页面现在只需要这种写法就可以,${message}
<!doctype html>
<html lang="en">
<head><meta charset="UTF-8"><title>Freemarker Index</title>
</head>
<body>
${message}
</body>
</html>总结
介绍了 Spring Boot 的启动方式、核心注解的用法及含义、配置文件的书写格式以及模板引擎 Freemarker 的使用方法。通过这些内容,你应该能够更好地理解和应用 Spring Boot 进行开发。希望这篇博客能帮助你更好地理解 Spring Boot 的各种概念和用法。如果你有任何问题或建议,欢迎在评论区留言!