springboot新建项目

springboot新建项目

SpringBoot2核心技术与响应式编程

语雀:https://www.yuque.com/atguigu/springboot

1 springboot工程

1.1 创建maven工程

1.2 引入依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.3</version>
</parent>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

1.3 创建主程序

/*
 * @SpringBootApplication:主方法
 */

@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
} 

1.4 写业务代码

@RestController
public class Hello {

    @RequestMapping("/hello")
    public static String hello() {
        return "hello,spring boot2...";
    }

    @RequestMapping("/home")
    public static String home() {
        return "spring boot2 home...";
    }

}

1.5 简化配置

#application.properties
server.port=8888

1.6 简化部署

1、创建可执行jar包

创建可执行jar需引入插件spring-boot-maven-plugin

命令行下执行:

mvn clean,mvn package

<build>
    <finalName>boot01-hello</finalName>

    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>2.4.3</version>
      </plugin>
    </plugins>
  </build>

cmd dir

进入jar包所在目录,打开cmd,执行jar,启动项目

image-20220410170145763

java -jar *.jar

image-20220410170257392

2 自动配置原理

2.1 依赖管理

  • 父项目做依赖管理
依赖管理
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.3</version>
</parent>

父项目的父项目
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.4.3</version>
</parent>
声明了常用依赖的版本号,自动版本仲裁机制
  • 开发导入starter场景启动器
  1. spring-boot-starter-*
  2. starter列表

https://docs.spring.io/spring-boot/docs/2.4.3/reference/html/using-spring-boot.html#using-boot

  1. 可以自定义starter
  2. 所有starter最底层的依赖是spring-boot-starter
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  • 无需关注版本号,自动仲裁

  • 自定义依赖版本号

1. 常看spring-boot-dependencies中依赖的版本号
2. 在当前项目的pom中自定义依赖版本号
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <mysql.version>5.1.43</mysql.version>
</properties>

2.2 自动配置

  • 自动配好tomcat

引入tomcat依赖

  • 自动配好SpringMVC

  • 自动配好web常见功能

  • 默认的包结构

    主程序所在的包,及其下面所有子包的组建都会被扫描进来

  • 各种配置拥有默认值

  • 按需加载所有自动配置项

3 容器功能

3.1 组建添加

  1. @Configuration
  2. @Bean, @component, @Controller, @Service, @Repository
  3. @ComponentScan, @Import
  4. @Conditional

3.2 配置绑定

// 1.属性配置在主配置文件application.properties
@Component
@ConfigurationProperties(prefix = "mycar")

// 2. 在配置类中用@EnableConfigurationProperties
@EnableConfigurationProperties(Car.class)
@ConfigurationProperties(prefix = "mycar")
posted @ 2022-04-10 17:34  msnhce  阅读(61)  评论(0)    收藏  举报