SpringBoot(一):构建第一个SpringBoot工程

      版权声明:本文为博主原创文章,转载请注明出处http://blog.csdn.net/saytime          https://blog.csdn.net/saytime/article/details/74781696        </div>
        <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-f57960eb32.css">
                          <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-f57960eb32.css">
      <div class="htmledit_views" id="content_views">

现如今,互联网的热点已经成为微服务了,如果你到现在还没有听说过微服务,或者还不了解,那表示你已经out了,需要开始作准备了。现在主流的微服务框架包括早期阿里的Dubbo,以及现在热门的SpringCloud,所以我们开始要准备开始学习SpringCloud了,扯远了,因为SpringCloud是基于SpringBoot结构上去进行开发,所以我们也要学习SpringBoot,平时我们使用SSM,SSH等,是不是配置文件一大堆,而且经常不知道哪里配置错了,导致问题很多。SpringBoot就是为了解决这个问题的。使用起来非常简单。

一、本教程环境

  • IDE : IntelliJ IDEA 2017.1.4 x64
  • JDK : 1.8
  • SpringBoot :1.5.4.RELEASE

二、搭建SpringBoot工程

2.1 先用Idea新建一个Empty Project

这里写图片描述

名字随意,这里叫springboot-demo

这里写图片描述

2.2 使用IDEA Spring Initializr 构建SpringBoot工程

New Module

这里写图片描述

这里写图片描述

这里写图片描述

勾选web依赖,继续下一步就好了。

这里写图片描述

pom.xml


 
 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
  4. <modelVersion>4.0.0 </modelVersion>
  5. <groupId>cn.saytime </groupId>
  6. <artifactId>springboot-first-demo </artifactId>
  7. <version>0.0.1-SNAPSHOT </version>
  8. <packaging>jar </packaging>
  9. <name>springboot-first-demo </name>
  10. <description>Demo project for Spring Boot </description>
  11. <parent>
  12. <groupId>org.springframework.boot </groupId>
  13. <artifactId>spring-boot-starter-parent </artifactId>
  14. <version>1.5.4.RELEASE </version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8 </project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8 </project.reporting.outputEncoding>
  20. <java.version>1.8 </java.version>
  21. </properties>
  22. <dependencies>
  23. <!-- Add typical dependencies for a web application -->
  24. <dependency>
  25. <groupId>org.springframework.boot </groupId>
  26. <artifactId>spring-boot-starter-web </artifactId>
  27. </dependency>
  28. <!-- SpringBoot test -->
  29. <dependency>
  30. <groupId>org.springframework.boot </groupId>
  31. <artifactId>spring-boot-starter-test </artifactId>
  32. <scope>test </scope>
  33. </dependency>
  34. </dependencies>
  35. <!-- Package as an executable jar -->
  36. <build>
  37. <plugins>
  38. <plugin>
  39. <groupId>org.springframework.boot </groupId>
  40. <artifactId>spring-boot-maven-plugin </artifactId>
  41. </plugin>
  42. </plugins>
  43. </build>
  44. </project>

搭建完成,下面是SpringBoot项目结构

这里写图片描述

三、Hello SpringBoot

先来看看 SpringBoot 工程结构:


 
 
  1. cn
  2. +- saytime
  3. +- Application.java //main方法所在类在最底层
  4. |
  5. +- bean //bean类
  6. | +- Hello.java
  7. |
  8. +- service //service层
  9. | +- HelloService.java
  10. |
  11. +- web //controller层
  12. +- HelloController.java

国际惯例,Hello world

再cn.saytime目录下,也就是SpringbootFirstDemoApplication类同级目录新建web包以及HelloController类。


 
 
  1. package cn .saytime .web;
  2. import org .springframework .web .bind .annotation .RequestMapping;
  3. import org .springframework .web .bind .annotation .RequestMethod;
  4. import org .springframework .web .bind .annotation .RestController;
  5. /**
  6. * @author zh
  7. * @ClassName cn.saytime.web.HelloController
  8. * @Description
  9. * @date 2017-06-25 21:20:41
  10. */
  11. @RestController
  12. public class HelloController {
  13. @RequestMapping(value = "sayhello", method = RequestMethod.GET)
  14. public String sayhello(){
  15. return " Hello SpringBoot !";
  16. }
  17. }

@RestController == @Controller + @ResponseBody

运行SpringBoot工程入口类SpringbootFirstDemoApplication的main方法。


 
 
  1. . ___ _ _ _ _ _ _
  2. /\\ / __ _ '_ __ _ _(_)_ __ __ _ \ \ \ \
  3. ( ( )\___ | '_ | '_ | | '_ \/ _` | \ \ \ \
  4. \\/ ___) | | _) | | | | | | | (_ | | ) ) ) )
  5. ' | ____ | .__ | _ | | _ | _ | | _\__, | / / / /
  6. ========= | _ | ============== | ___/=/_/_/_/
  7. :: Spring Boot :: (v1.5.4.RELEASE)
  8. s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
  9. 2017-06-25 21:26:02.338 INFO 8620 --- [ main] c.s.SpringbootFirstDemoApplication : Started SpringbootFirstDemoApplication in 1.624 seconds (JVM running for 2.186)

点击进入 http://127.0.0.1:8080/sayhello

这里写图片描述

SpringBoot Hello World 执行成功

四、使用感受:

没有做任何的web.xml配置。 没有做任何的sping mvc的配置 没有配置tomcat (springboot内嵌tomcat)

五、SpringBoot 工程启动方式

  1. Application类main方法启动
  2. mvn clean install/package cd 到target目录,java -jar 项目.jar,注意这里需要加入依赖spring-boot-maven-plugin生成可执行的jar
  3. mvn spring-boot: run 启动

六、Application 分析


 
 
  1. package cn.saytime;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class SpringbootFirstDemoApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(SpringbootFirstDemoApplication. class, args);
  8. }
  9. }

通过查看源代码可以发现:

@SpringBootApplication = @SpringBootConfiguration + @EnableAutoConfiguration + @ComponentScan

@SpringBootConfiguration = @Configuration = @Component 在Spring中@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。 当**@ComponentScan**扫描到该注解时,加入到Spring容器中,类似的注解除了@Component,还有@Service,@Controller,@Repository等。

@ComponentScan 指定注解扫描地址,默认为该类同级路径。即能扫描到本项目中web包下面的HelloController的@RestController注解

@EnableAutoConfiguration 注解用来自动配置,我们pom中配置了 spring-boot-starter-web,所以spring会来创建一 个web应用来配置程序,也就是根据依赖jar,自动进行装配操作,同样,如果Pom中或者加入了mysql jdbc 包,那么会自动去构建一个数据库配置,如果你在application.properties/.yml中没有配置mysql jdbc,那么启动会报错,如果引入了相关jar包,但是需要springboot不自动配置,那么需要配置exclude 比如:@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值