SpringBoot入门

作者:gqk


1:什么是SpringBoot

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。

2:环境准备

  • apache-maven-3.6.0
  • jdk1.8
  • Tomcat8
  • IntelliJ IDEA 2017.3.6 x64
  • SpringBoot 1.5.20

3:Maven设置

<profile> 
               <id>jdk-1.8</id>  
               <activation> 
                   <activeByDefault>true</activeByDefault>  
                   <jdk>1.8</jdk> 
               </activation>  
               <properties> 
                   <maven.compiler.source>1.8</maven.compiler.source>  
                   <maven.compiler.target>1.8</maven.compiler.target>  
                   <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> 
               </properties> 
     </profile>

4,IntelliJ IDEA配置Maven

5,SpringBoot---HelloWorld案例

5.1创建Maven工程(选择maven项目和自己的jdk安装目录)

 

自动导入开启

5.2导入SpringBoot依赖

 

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

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

5.3主程序编写启动SpringBoot

 

package com.gqk;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

此时启动程序出现一下界面:

 

 5.4编写控制层

package com.gqk;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloWorldController {
    @ResponseBody
    @RequestMapping("/helloWorld")
    public String helloWorld(){
        System.out.print("2222222");
        return "HelloWorld!";
    }

}

5.5简化部署

导入依赖

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

最终只要运行java -jar的命令

6:程序注解解读

@SpringBootApplication:Springboot启动(复合注解)

  • @SpringBootConfiguration继承自@Configuration,二者功能也一致,标注当前类是配置类,并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到srping容器中,并且实例名就是方法名。

  • @EnableAutoConfiguration的作用启动自动的配置,@EnableAutoConfiguration注解的意思就是Springboot根据你添加的jar包来配置你项目的默认配置,比如根据spring-boot-starter-web ,来判断你的项目是否需要添加了webmvctomcat,就会自动的帮你配置web项目中所需要的默认配置。

  • @ComponentScan,扫描当前包及其子包下被@Component@Controller@Service@Repository注解标记的类并纳入到spring容器中进行管理。是以前的<context:component-scan>(以前使用在xml中使用的标签,用来扫描包配置的平行支持)。

 

posted @ 2019-04-06 12:27  少侠gqk  阅读(178)  评论(0编辑  收藏  举报