码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot版hello world

时间:2020-05-28 16:43:36      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:管理   encoding   ring   ons   ase   运行   xmlns   des   public   

1、新建springboot项目

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

SpringBoot版hello world

标签:管理   encoding   ring   ons   ase   运行   xmlns   des   public   

原文地址:https://www.cnblogs.com/lxy-java/p/12981419.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!