parent 依赖管理,减少依赖冲突

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.6.7</version>
  5. <relativePath/> <!-- lookup parent from repository -->
  6. </parent>

starter 定义依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>

引导类

  1. package com.tj;
  2. import com.tj.controller.BookController;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.context.ConfigurableApplicationContext;
  6. @SpringBootApplication
  7. public class Application {
  8. public static void main(String[] args) {
  9. ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);
  10. // 获取bean
  11. BookController bean = ctx.getBean(BookController.class);
  12. System.out.println("bean = " + bean);
  13. // 获取bean
  14. User bean1 = ctx.getBean(User.class);
  15. System.out.println("bean1 = " + bean1);
  16. }
  17. }

可以使用exclusions排除依赖

image.png

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. <!--排除依赖里的依赖,比如tomcat-->
  5. <exclusions>
  6. <exclusion>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-tomcat</artifactId>
  9. </exclusion>
  10. </exclusions>
  11. </dependency>
  12. <!--单独添加依赖,比如tomcat-->
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-tomcat</artifactId>
  16. </dependency>