SpringBoot详解(一) 从入门到入土

SpringBoot

一、第一个项目的步骤:

1.创建Maven项目

选择自己的maven D:/apache-maven-3.6.3

2.修改pom.xml

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

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

3.编写一个主程序:启动spring boot应用

/**
 * @SpringBootApplication 来标注一个主程序类,说明在是一个spring boot应用
 */
@SpringBootApplication
public class HelloWorldMainApplication {
    public static void main(String[] args) {

        //Spring应用启动起来
        SpringApplication.run(HelloWorldMainApplication.class,args);
    }
}

4.编写相关的Controller、servlet

@Controller
public class HelloController {
    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "Hello World!";
    }
}

5.运行主程序测试

6.简化部署

<!--这个插件,可以将应用打包成一个可执行的jar包-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

将这个应用打成jar包,直接使用java -jar的命令进行执行;

二、Hello World探究

1.pom文件

1.父项目

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

他的父项目是:
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.1.13.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
  </parent>
他来真正管理Spring Boot应用里的所有依赖版本;

Spring Boot的版本仲裁中心

以后我们导入依赖默认是不需要写版本: spring-boot-dependencies

(没有在dependencies的依赖需要写版本)

2.导入的依赖

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

spring-boot-starter-web

spring-boot-starter:springboot场景启动器:帮我们导入了web模块正常运行所依赖的组件.

Spring Boot 将所有的功能场景都抽取出来,做成一个个的Starters(启动器),只需要在项目里面引用这些starter相关场景的所有依赖都会导入进来,要用什么功能就导入什么场景的启动器

2.主程序类,主入口类

/**
 * @SpringBootApplication 来标注一个主程序类,说明在是一个spring boot应用
 */
@SpringBootApplication
public class HelloWorldMainApplication {
    public static void main(String[] args) {

        //Spring应用启动起来
        SpringApplication.run(HelloWorldMainApplication.class,args);
    }
}

@SpringBootApplication: spring boot应用 标注在某个类上说明这个类是Spring Boot的主配置类,SpringBoot就应该运行这个类的main方法来启动SpringBoot应用

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {

@SpringBootConfiguration :springBoot的配置类

​ 标注在某个类上,表示这是一个springBoot的配置类:

​ @Configuration:配置类上来标注这个注解

​ 配置类 ----- 配置文件;配置类也是容器中的一个组件:@Component

@EnableAutoConfiguration :开启自动配置功能.

下一篇学习
SpringBoot详解(二) 从入门到入土 (配置文件)

猜你喜欢

转载自blog.csdn.net/weixin_45262118/article/details/108482733
今日推荐