spring-boot快速入门实践代码

spring-boot我想已经有很多人了解太的概念了吧,不了解的你可以网上一查一大堆,但是概念有的时候还是很难让人深入理解,我知道它,但是它是干什么的就不知道了。

以下摘要是网上对比spring的,看客可以参考以下:

[plain] view plain copy
  1. Springboot之Spring与Spring boot的区别:一直以来,由于对SPRING的理解不深入。在使用中总是Spring boot的时候认为它是SPRING的一部分,事实不然。  
  2.   
  3. Spring boot是一个在Spring 的基础上搭建的全新的微框架,其目的是简化Spring的搭建和开发过程。  
  4.   
  5. 有以下的特点:  
  6. 1)简单易用,初学者和大牛都可以轻松上手,其中的注解会给使用者提供方便;  
  7.   
  8. 2)Spring boot对第三方技术进行了很好的封装和整合,提供了大量第三方接口;  
  9.   
  10. 3)可以通过依赖自动配置,不需要XML等配置文件  
  11.   
  12. 4)还提供了安全等特性姑且先不做理会。  

 

我们今天主要是快速启动一个spring-boot的项目,然后每天往里面加点东西,一点一点的加进去,项目再不断的完善,即可慢慢了解它到底有什么好处,能给我们带来哪些好处和便捷。

今天首先第一步,从头搭建项目,你也可以直接从官网上下载一个,也可以通过maven自己创建,我这里是通过我本地自己创建maven的

spring-boot快速入门实践代码

spring-boot快速入门实践代码

spring-boot快速入门实践代码

填写好相关的信息,然后点击finish完成即可

项目创建完之后,就需要往里面添加spring-boot所需要的jar包了,首先找到项目的pom.xml文件,然后在里面添加如下代码:

[html] view plain copy
  1. <parent>  
  2.       <groupId>org.springframework.boot</groupId>  
  3.       <artifactId>spring-boot-starter-parent</artifactId>  
  4.       <version>1.5.7.RELEASE</version>  
  5.   </parent>  
  6.   
  7.   <dependencies>  
  8.       <dependency>  
  9.           <groupId>org.springframework.boot</groupId>  
  10.           <artifactId>spring-boot-starter-web</artifactId>  
  11.       </dependency>  
  12.   </dependencies>  

添加完之后,接下来就是要设置启动类了,在项目的src目录下,创建一个com.lwl.boot.controller的包,然后创建MyApplication.java类:具体如下

[java] view plain copy
  1. package com.lwl.boot.controller;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;  
  5. import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;  
  6. import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;  
  7. import org.springframework.boot.web.servlet.ErrorPage;  
  8. import org.springframework.context.annotation.Bean;  
  9. import org.springframework.http.HttpStatus;  
  10. import org.springframework.stereotype.Controller;  
  11. import org.springframework.web.bind.annotation.RequestMapping;  
  12. import org.springframework.web.bind.annotation.ResponseBody;  
  13.   
  14. @Controller  
  15. @EnableAutoConfiguration  
  16. /** 
  17.  * @EnableAutoConfiguration 用于自动配置。简单的说,它会根据你的pom配置(实际上应该是根据具体的依赖)来判断这是一个什么应用,并创建相应的环境。 
  18.  */  
  19. public class MyApplication {  
  20.   
  21.   
  22.     @RequestMapping("/")  
  23.     @ResponseBody  
  24.     public String index() {  
  25.         return "你现在看到的是帅帅的我";  
  26.     }  
  27.       
  28.       
  29.     @RequestMapping("/hello")  
  30.     @ResponseBody  
  31.     public String hello() {  
  32.         return "hello 帅帅的我出现啦";  
  33.     }  
  34.   
  35.       
  36.       
  37.     public static void main(String[] args) throws Exception {  
  38.         /** 
  39.          * SpringApplication 则是用于从main方法启动Spring应用的类。默认,它会执行以下步骤: 
  40.             1.创建一个合适的ApplicationContext实例 (取决于classpath)。 
  41.             2.注册一个CommandLinePropertySource,以便将命令行参数作为Spring properties。 
  42.             3.刷新application context,加载所有单例beans。 
  43.             4.**所有CommandLineRunner beans。 
  44.             默认,直接使用SpringApplication 的静态方法run()即可。但也可以创建实例,并自行配置需要的设置。 
  45.          */  
  46.         SpringApplication.run(MyApplication.class, args);  
  47.           
  48.         //通过自定义参数设置,启动  
  49. //        SpringApplication app = new SpringApplication(MyApplication.class);  
  50. //        app.run(args);  
  51.           
  52.           
  53.         /** 
  54.          * 如果设置 properties 则配置参数会读取properties设置的值 
  55.          */  
  56.         
  57.           
  58.     }  
  59.       
  60.     /** 
  61.      * 设置基本异常处理抛出页面 
  62.      * @return 
  63.      * @create 2017-10-9 上午11:44:13 
  64.      */  
  65.     @Bean  
  66.     public EmbeddedServletContainerCustomizer containerCustomizer() {  
  67.         return new EmbeddedServletContainerCustomizer() {  
  68.             @Override  
  69.             public void customize(ConfigurableEmbeddedServletContainer container) {  
  70.                 ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");  
  71.                 ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");  
  72.                 ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");  
  73.                 container.addErrorPages(error401Page, error404Page, error500Page);                
  74.             }  
  75.         };  
  76.     }  
  77.       
  78. }  

右击→启动这个类即可加载项目

[html] view plain copy
  1. 如果设置 properties 则配置参数会读取properties设置的值  

这个有个特别的配置文件,以后会慢慢详解,先来看看项目运行起来的效果

spring-boot快速入门实践代码

打开浏览器,查看运行效果http://localhost:8090/hello

spring-boot快速入门实践代码


代码已经上传到我的github上面,如果有需要可以去下载:https://github.com/1181888200/spring-boot.git