Spring Boot 介绍
What is Spring Boot?
Spring Boot 是一个收集很多模块的项目。
- spring-boot-dependencies
包含 Spring Boot 使用的依赖项,Maven 插件等版本,包括 starter-modules 使用的依赖项。包含依赖项的托管依赖项。
- spring-boot-starter-parent
父 pom.xml 文件,使用 Maven 为 Spring Boot 应用程序提供依赖性和插件管理。
- spring-boot-starters
所有 Spring Boot 启动器模块的父级。
- spring-boot-autoconfigure
包含 Spring Boot 中启动器的自动配置模块。
- spring-boot-actuator
允许监视和管理使用 Spring Boot 创建的应用程序。
- spring-boot-tools
与 Spring Boot 结合使用的工具,如 Spring Boot Maven 和 Gradle 插件等。
- spring-boot-devtools
开发 Spring Boot 应用程序时可以使用的开发人员工具。
其中,最重要的模块是:起步依赖和自动配置。
What are the advantages of using Spring Boot?
Spring Boot 的优点:
• 自动配置“合理默认值”,减少样板配置。
配置适应于类路径的依赖性,例如,如果类路径上有 HSQLDB 依赖项,则创建连接到内存 HSQLDB 数据库的数据源 bean。
• 启用快速开发应用程序的入门。
• 当默认值不再足够时,可轻松自定义。
这种自定义可以通过在应用程序的属性文件中设置属性值,或创建与 Spring Boot 创建的名称相同的 Spring bean 来替换 Spring bean 定义来完成。
• 可生成可执行的独立 JAR 文件。
这样的 JAR 文件可以使用常规 java -jar 命令从命令行运行,甚至可以包含嵌入式 Web 服务器,用于 Web 应用程序,如 Tomcat 或 Jetty。
• 提供已验证可协同工作的一组托管依赖项。
• 提供一组托管 Maven 插件,这些插件配置为生成某些工件。
• 提供项目中通常需要的非功能性功能。
一些此类功能是安全性,外部化配置,度量和健康检查。
• 不生成代码。
• 不需要 XML 配置。
• 使用标准 Spring Framework 机制。
这允许熟悉 Spring Framework 的开发人员快速学习和使用 Spring Boot。
• 在开发者社区中很受欢迎。
关于如何使用 Spring Boot 开发不同类型的应用程序有很多资源。
• Spring Boot 是一个成熟的,支持良好且积极开发的产品,它基于一个更加成熟,支持良好且积极开发的框架。
• 标准化应用程序结构的各个部分。
在使用 Spring Boot 的不同项目之间移动时,开发人员将识别共同元素。
Why is it “opinionated”?
Spring Boot 对于如何完成应用程序的开发有一个意见,例如关于技术相关模块(启动器和自动配置),属性组织,模块配置等。
Spring Boot 的真正成就在于它是固执己见的,但允许开发人员在需要的范围内定制他们的项目而不会成为障碍。
What things affect what Spring Boot sets up?
什么东西会影响 Spring Boot 的设置?
Spring Boot 中有许多条件注释,每个条件注释都可用于控制 Spring bean 的创建。
Condition Annotation | Condition Factor |
---|---|
@ConditionalOnClass | 类路径存在该 class |
@ConditionalOnMissingClass | 类路径不存在该 class |
@ConditionalOnBean | 存在 Spring bean 或 bean 类型(类) |
@ConditionalOnMissingBean | 不存在 Spring bean 或 bean 类型(类) |
@ConditionalOnProperty | 存在 Spring 环境属性。 |
@ConditionalOnResource | 存在某资源,如文件。 |
@ConditionalOnWebApplication | 如果应用程序被认为是 Web 应用程序,那就是使用 Spring WebApplicationContext,定义会话范围或具有 StandardServletEnvironment。 |
@ConditionalOnNotWebApplication | 如果该应用程序不被视为 Web 应用程序。 |
@ConditionalOnExpression | 基于 SpEL 表达式的评估结果,激活 Bean 或配置。 |
@ConditionalOnCloudPlatform | 如果指定的云平台,Cloud Foundry,Heroku 或 SAP 处于活动状态。 |
@ConditionalOnEnabledEndpoint | 指定的端点已启用。 |
@ConditionalOnEnabledHealthIndicator | 已启用命名的健康指示器。 |
@ConditionalOnEnabledInfoContributor | 已启用命名的 info contributor。 |
@ConditionalOnEnabledResourceChain | 已启用 Spring 资源处理链。 |
@ConditionalOnInitializedRestarter | Spring DevTools RestartInitializer 已应用非空 URL。 |
@ConditionalOnJava | 存在某个版本的 JVM 或版本范围内的 JVM。 |
@ConditionalOnJndi | 存在 JNDI InitialContext 和指定的 JNDI 位置的可用性。 |
@ConditionalOnManagementPort | Spring Boot Actuator 管理端口是:与服务器端口不同,与服务器端口相同或禁用。 |
@ConditionalOnRepositoryType | 已启用指定类型的 Spring Data 存储库。 |
@ConditionalOnSingleCandidate | 可以确定 bean 工厂中包含的指定类型(类)的 Spring bean 和单个候选者。 |
What is a Spring Boot starter POM? Why is it useful?
Spring Boot 启动器 POM:父 POM.xml 文件,定义了依赖性和版本管理。
启动 POM 的优势:已经收集了开始使用某种技术所需的所有依赖关系。开发人员可以放心,没有缺少依赖项,并且所有依赖项都具有可以很好地协同工作的版本。
Spring Boot supports both properties and YML files. Would you recognize and understand them if you saw them?
- Java properties file:
# This is a comment.
db.username=wdpm
db.password=123456
someKey=someValue
- YAML file:
# This is a comment.
db:
username: wdpm
password: 123456
someKey: someValue
Can you control logging with Spring Boot? How?
默认情况下,Spring Boot application 的 logging 级别为 INFO。
如果要为整个应用程序启用 DEBUG 或 TRACE 日志记录,请使用--debug 或--trace 标志或在 application.properties 中设置属性 debug = true 或 trace = true
Spring Boot 使用 Commons Logging API 进行日志记录。Spring Boot 支持以下基础日志框架:
• Logback(Spring Boot 使用的默认值)-> logback-spring.xml
• Log4J2 -> log4j2.xml
• Java Util Logging(Java 平台的核心日志记录工具)-> logging.properties
Where does Spring Boot look for property file by default?
Spring Boot 应用程序的默认属性位于:应用程序的 JAR 里面名为“application.properties”的文件中。
开发时,此文件位于 src/main/resources 目录。
在启动应用程序时,可以使用命令行参数自定义此 application.properties 文件中定义的各个属性值。
可以使用外部 application.properties 文件或 YAML 等效文件完全覆盖默认属性。
How do you define profile specific property files?
profile 可以分为:dev,test,stage,uat,prod
1.首先,定义一个公用的 application.properties。
2.然后,按需定义 application-{profile}.properties。
3.启动 app 时,在命令行指定激活的 profiles 即可(复数代表可同时激活多个 profile)。
示例:mvn spring-boot:run -Dspring-boot.run.profiles=dev,test
How do you access the properties defined in the property files?
@PropertySource("classpath:foo.properties") @PropertySource("classpath:bar.properties")
public class PropertiesWithJavaConfig {
@Value( "${jdbc.url}" )
private String jdbcUrl;
//...
@Autowired private Environment env;
dataSource.setUrl(env.getProperty("jdbc.url"));
}
或者:
//database.properties
database.url=jdbc:postgresql:/localhost:5432/instance
database.username=foo
database.password=bar
@ConfigurationProperties(prefix = "database")
public class Database {
String url;
String username;
String password;
// standard getters and setters
}
What properties do you have to define in order to configure external MySQL?
# ===============================
# = DATA SOURCE
# ===============================
# Set here configurations for the database connection
spring.datasource.url = jdbc: mysql://192.168.1.12:3306/db_example?characterEncoding=utf8&useSSL=false
# Username and password
spring.datasource.username = ...
spring.datasource.password = ...
#spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
# ============== JPA / HIBERNATE ============ #
# Show or not log for each sql query
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = none
# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
How do you configure default schema and initial data?
Spring Boot 可以自动创建 DataSource 的模式(DDL 脚本)并对其进行初始化(DML 脚本)。
它从标准的根类路径位置加载 SQL:schema.sql 和 data.sql。
详细配置参阅: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html
What is a fat jar? How is it different from the original jar?
fat jar 或超级 jar 是一个 jar,它包含所有项目类文件和资源以及它的所有依赖项。
Spring Boot 借助 maven 或 gradle 插件生成的 jar 就是超级 jar,可以独立运行。
What is the difference between an embedded container and a WAR?
嵌入式容器打包在应用程序 JAR 文件中,并且只包含一个应用程序。
在使用 WAR 文件之前,需要将其部署到 Web 容器(如 Tomcat)。部署 WAR 文件的 Web 容器可能包含其他应用程序。
What embedded containers does Spring Boot support?
• Tomcat
• Jetty
• Undertow
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

上一篇: Spring Testing 单元测试
下一篇: 谈谈自己对于 AOP 的了解
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论