spring root的开发环境搭建

来源:互联网 发布:java array list 编辑:程序博客网 时间:2024/06/08 09:55

一,基本介绍

spring boot是什么有哪些作用?优缺点有哪些?

  • Spring Boot提供了一个强大的一键式Spring的集成开发环境,能够单独进行一个Spring应用的开发,其中: (1)集中式配置(application.properties)+注解,大大简化了开发流程 (2)内嵌的Tomcat和Jetty容器,可直接打成jar包启动,无需提供Java war包以及繁琐的Web配置 (3)提供了Spring各个插件的基于Maven的pom模板配置,开箱即用,便利无比。(4)可以在任何你想自动化配置的地方,实现可能 (5)提供更多的企业级开发特性,如何系统监控,健康诊断,权限控制 (6) 无冗余代码生成和XML强制配置 (7)提供支持强大的Restfult风格的编码,非常简洁
  • 简而言之spring boot的主要优点是优点是对新手无需任何门槛,只要懂Maven会看文档就能亦步亦趋的开始一个新项目。对高手来说,改配置也是分分钟的事。虽然Spring Boot的auto configuration loading真的非常绕,但熟悉spring的人还是蛮容易找到问题所在的。
  • 缺点缺点就是简单的背后蕴藏了巨大的学习曲线。入门容易,但是如果没有完整学习spring的体系,碰到问题就一脸懵逼。据我所知,很多玩spring boot的人连怎么打开底层包的log都不知道,怎么可能能发现问题呢?

二,环境准备

1.eclipse开发工具

2.maven

三,新建maven项目

1.maven项目的新建很简单,可以参考此博客http://blog.csdn.net/chuyuqing/article/details/28879477。

2.随后打开新建的项目,添加我们的pom文件添加SpringBoot的相关依赖和插件,我写好的pom.xml文件如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>springbootdemo</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>springbootdemo Maven Webapp</name><url>http://maven.apache.org</url><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.3.4.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!-- 连接mysql数据库所添加的依赖 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency></dependencies><build><finalName>springbootdemo</finalName></build></project>

四,编写启动类

编写HWController.java启动类,代码如下:

package com.example;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;@RestController@SpringBootApplicationpublic class HWController {@RequestMapping("/helloworld")public String sayHello() {return "Hello world,Hello boot!";}public static void main(String[] args) {SpringApplication.run(HWController.class, args);   //运行之后在浏览器中访问:http://localhost:8080/helloworld}} 

说明:其中需要说明的一点是spring-boot-starter-web除了聚集了SpringBoot自动配置的一些jar包还包含了基本的Spring的jar包,spring-aop,spring-beans,spring-context,spring-webmvc,spring-boot-start-tomcat等等jar包,其中添加的mysql-connector-java和本文示例无关,各位在参考时可以不添加此项依赖。正是有内置的tomcat,我们的SpringBoot应用才能达成jar包直接运行,这样的特性很利于我们分布式应用的部署,部署携带都很方便。到目前为止一个基本的springboot应用环境已经搭建完毕,接下来需要编写启动类

五,运行程序

运行我们的Application
1.右键Run As -> Java Application。
2.右键project – Run as – Maven build – 在Goals里输入spring-boot:run ,然后Apply,最后点击Run。
之后打开浏览器输入地址:http://127.0.0.1:8080/ 就可以看到Hello world!了。
作者: JohnR
链接:http://www.imooc.com/article/9871
来源:慕课网
运行我们的Application
1.右键Run As -> Java Application。
2.右键project – Run as – Maven build – 在Goals里输入spring-boot:run ,然后Apply,最后点击Run。
之后打开浏览器输入地址:http://127.0.0.1:8080/helloworld 就可以看到
Hello world,Hello boot!
了。

运行效果如图,控制台输出如下信息,说明启动成功:

接下来在浏览器输入http://localhost:8080/helloworld,访问后效果如图:

至此,我的第一个spring boot的demo完成,如有疏漏及不完美的地方,敬请谅解(第一次写博客)。


0 0