SpringBoot版hello world

1、新建springboot项目(File-->new-->Module-->Spring Initalizr)

2、导入依赖pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lxy</groupId>
    <artifactId>springboot-01-hellospringboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-01-hellospringboot</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>

  pom.xml文件说明

  1)、父项目

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
</parent>

    它的父项目是

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.2.5.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
  </parent>

    这个是真正来管理Spring Boot应用里面的所有依赖版本,SpringBoot的版本仲裁中心;

    以后我们导入依赖默认不需要写版本;(没有在dependcies里面管理的依赖自然需要声明版本号)

  2)、导入依赖

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

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

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

    要用什么功能,就导入什么场景的启动器 

3、写出程序类,主入口类

// @SpringBootApplication 标注这个类是一个springboot的应用
@SpringBootApplication
public class Springboot01HellospringbootApplication {

    public static void main(String[] args) {
//        将springboot应用启动
        SpringApplication.run(Springboot01HellospringbootApplication.class, args);
    }
}

    @SpringBootApplication:Spring Boot应用标注在某个类上说明这个类是SpringBoot的主配置类,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的配置类;标注在某个类上,表示这是一个Spring Boot的配置类

        @Configuration:配置类上标注这个注解;配置类  ----  配置文件

          @Component 配置类也是容器的一个组件

      @EnableAutoConfiguration:开启自动配置功能     

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {

        @AutoConfigurationPackage: 自动配置包

          @Import(AutoConfigurationPackages.Registrar.class) spring底层注解;给容器中导入一个组件

        将主配置类(@SpringBootApplication标注的类)的所在包以及所有子包里面的所有组件扫描到Spring容器;

        @Import(AutoConfigurationImportSelector.class):给容器导入组件

          AutoConfigurationImportSelector.class:导入哪些组件选择器

            将所需要的导入的组件以全类名的方式返回,这些组件就会添加到容器中。会给容器中导入非常多的自动配置类(xxxAutoConfiguration)

            这些自动配置类的作用就是给容器中导入这个场景所需要的所有组件,并配置好这些组件;免去了手动配置的麻烦

        Spring Boot在启动的时候从路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,自动配置类就生效

5、写Controller类

@Controller
@RequestMapping("/hello")
public class HelloController {
    @GetMapping("/springboot")
    @ResponseBody
    public String hello() {
        return "Hello SpringBoot";
    }
}

 

posted @ 2020-05-28 15:59  遨游java  阅读(152)  评论(0编辑  收藏  举报