(一)SpringBoot——helloworld

1、为何会诞生SpringBoot?java

先看看spring的优点:web

一、代码解耦、简化开发:代码中再也不须要new去构造对象,而是交由spring去管理对象。spring

二、支持AOP:面向切面的编程,方便进行权限拦截、日志监控等。编程

三、声明式事务:经过配置便可完成对事务的管理。浏览器

固然,他还有不少的好处,就不一一举例了。springboot

经过以上咱们能够知道,spring能够经过配置来简化开发,随着时间的推移,咱们发现配置文件愈来愈臃肿和庞大,动辄上百行的xml配置文件,容易看的人眼花缭乱。app

每当程序界发现了问题的时候,总会有一我的来拯救你们,因而springboot诞生了,解决了配置臃肿的难题。工具

 

2、构建SpringBoot项目测试

构建SpringBoot项目的方式:插件

一、使用Spring Initializr的web页面:http://start.spring.io/

界面的可选项包括:

(1)项目管理工具:Maven、Gradle

(2)语言:Java、Kotlin、Groovy

(3)SpringBoot版本:2.0.0或者其余

(4)包名:com.example;工程名:demo

(5)搜索须要依赖的包:如,Config Client、Cloud Stream

点击“Generate Project”便可生成项目的压缩包。

 

二、使用编译器IDEA自带的插件(推荐使用)。

一样须要填写项目的相关配置,例如:

点击“Next”:

 

生成项目以后,在编译器中的目录以下图所示:

package com.solid4j;

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

@SpringBootApplication
public class SpringbootHelloworldApplication {

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

启动main方法,能够经过浏览器访问:http://localhost:8080/

新建一个controller,启动上面的main方法,再次经过浏览器访问:http://localhost:8080/hello

/*
 * Copyright (c) 2018 solidwang. All Rights Reserved
 */
package com.solid4j.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: solidwang
 * @date:2018/4/17 下午6:09
 */
@RestController
@RequestMapping
public class HelloworldController {
    /**
     * 测试helloworld
     * @return
     */
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello() {
        return "helloworld";
    }
}

访问以下:

相关文章
相关标签/搜索