spring-boot快速入门实践代码

来源:互联网 发布:防辐射服 知乎 编辑:程序博客网 时间:2024/05/17 08:59

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

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

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

 

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

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




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

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

  <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.7.RELEASE</version>    </parent>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>    </dependencies>

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

package com.lwl.boot.controller;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;import org.springframework.boot.web.servlet.ErrorPage;import org.springframework.context.annotation.Bean;import org.springframework.http.HttpStatus;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controller@EnableAutoConfiguration/** * @EnableAutoConfiguration 用于自动配置。简单的说,它会根据你的pom配置(实际上应该是根据具体的依赖)来判断这是一个什么应用,并创建相应的环境。 */public class MyApplication {@RequestMapping("/")    @ResponseBody    public String index() {        return "你现在看到的是帅帅的我";    }@RequestMapping("/hello")@ResponseBodypublic String hello() {return "hello 帅帅的我出现啦";}    public static void main(String[] args) throws Exception {    /**     * SpringApplication 则是用于从main方法启动Spring应用的类。默认,它会执行以下步骤:    1.创建一个合适的ApplicationContext实例 (取决于classpath)。    2.注册一个CommandLinePropertySource,以便将命令行参数作为Spring properties。    3.刷新application context,加载所有单例beans。    4.激活所有CommandLineRunner beans。    默认,直接使用SpringApplication 的静态方法run()即可。但也可以创建实例,并自行配置需要的设置。     */        SpringApplication.run(MyApplication.class, args);                //通过自定义参数设置,启动//        SpringApplication app = new SpringApplication(MyApplication.class);//        app.run(args);                        /**         * 如果设置 properties 则配置参数会读取properties设置的值         */                  }        /**     * 设置基本异常处理抛出页面     * @return     * @create 2017-10-9 上午11:44:13     */    @Bean    public EmbeddedServletContainerCustomizer containerCustomizer() {        return new EmbeddedServletContainerCustomizer() {@Overridepublic void customize(ConfigurableEmbeddedServletContainer container) {ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");                ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");                ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");                container.addErrorPages(error401Page, error404Page, error500Page);}        };    }}

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

 如果设置 properties 则配置参数会读取properties设置的值

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


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



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


原创粉丝点击