为什么在 Maven 中添加依赖会触发功能?

Why does the addition of a dependency in Maven trigger functionality?

我有一个简单的问题:我刚刚开始使用 Open API 3. 为此,我在 Maven 中添加了以下依赖项。

  <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-ui</artifactId>
        <version>1.2.30</version>
    </dependency>

添加这个依赖后,我是否可以通过localhost:8082/v3/api-docs访问服务而不需要事先设置任何东西并调用依赖的函数?这怎么会发生?这背后的概念是什么?

在您的 Maven 中添加 OpenAPI 依赖项 pom.xml 只是将库添加到您的项目中。就这些了。

如果这是一个 "traditional" 项目(例如 JSP 网络应用程序),您必须编写代码来创建网络服务(例如 "localhost:8082/v3/api-docs" ).

但听起来您的项目可能是 Spring 引导:

https://developer.ibm.com/technologies/java/tutorials/j-spring-boot-basics-perry/

If you let it, Spring Boot will use its @EnableAutoConfiguration annotation to automatically configure your application. Auto-configuration is based on the JARS in your classpath and how you’ve defined your beans:

  • Spring Boot uses the JARs you have specified to be present in the CLASSPATH to form an opinion about how to configure certain automatic behavior. For example, if you have the H2 database JAR in your classpath and have configured no other DataSource beans, then your application will be automatically configured with an in-memory database.

  • Spring Boot uses the way you define beans to determine how to automatically configure itself. For example, if you annotate your JPA beans with @Entity, then Spring Boot will automatically configure JPA such that you do not need a persistence.xml file.

这叫做约定优于配置。 维基 link https://en.wikipedia.org/wiki/Convention_over_configuration