springboot学习之构建简单项目搭建步骤详解
概述
相信对于java开发者而言,spring和springmvc两个框架一定不陌生,这两个框架需要我们手动配置的地方非常多,各种的xml文件,properties文件,构建一个项目还是挺复杂的,在这种情况下,springboot应运而生,他能够快速的构建spring项目,而且让项目正常运行起来的配置文件非常少,甚至只需要几个注解就可以运行整个项目。
总的说来,springboot项目可以打成jar包独立运行部署,因为它内嵌servlet容器,之前spring,springmvc需要的大量依赖,可以通过starter来帮助我们简化配置,当然还有其他好多优点,这里就不一一赘述,小伙伴们可以自行搜索解答。
简单项目构建
工具
eclipse maven
首先,我们新建一个maven项目,在eclipse左侧右击选择new----》other,选择新建maven project
输入group id,artifact id,点击完成
这样一个简单的项目架子就完成了,但是啥都没有,项目结构如下图所示:
下面我们就开始配置搭建springboot项目。
1.添加依赖
完整porm代码如下:
<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/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.cfxmn.springboot</groupid> <artifactid>springbootdemo</artifactid> <version>0.0.1-snapshot</version> <packaging>jar</packaging> <!-- 通过继承spring-boot-starter-parent项目来获得一些合理的默认配置 --> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.5.6.release</version> </parent> <properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding> </properties> <dependencies> <!-- spring boot web 依赖 --> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <!-- spring boot test 依赖 --> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <!-- 使用lombok可以减少很多重复代码的书写。比如说getter/setter/tostring等方法的编写 --> <dependency> <groupid>org.projectlombok</groupid> <artifactid>lombok</artifactid> </dependency> </dependencies> </project>
下面我们新建一些包和添加项目的启动类,如下图所示:
其中,控制器democontroller的内容非常简单,内容如下:
package com.cfxmn.springboot.springbootdemo.controller; import org.springframework.web.bind.annotation.postmapping; import org.springframework.web.bind.annotation.restcontroller; import lombok.extern.slf4j.slf4j; @restcontroller @slf4j public class democontroller { @postmapping("/demo") public void demotest() { // 这边简单起见,打印一下日志 log.info("success call"); } }
可能有些同学对其中的几个注解有些疑问,我这边简单说明下,
1.restcontroller
这个注解其实就是@responsebody + @controller
2.postmapping
这个注解其实就是@requestmapping("xxxxxx", method=requestmethod.post)
这两个其实都是组合注解,简化使用
我们再来看看,项目的启动类springbootdemoapplication的内容:
package com.cfxmn.springboot.springbootdemo; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; @springbootapplication public class springbootdemoapplication { public static void main(string[] args) { springapplication.run(springbootdemoapplication.class, args); } }
是的,你没看错,只要运行这个main方法,就能启动这个spring项目,具体是怎么启动的容器,我们之后再分析,其实主要就是在注解springbootapplication上。
下面我们就来运行下,看下启动日志:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: spring boot :: (v1.5.6.release) 2018-10-25 23:52:41.985 info 1700 --- [ main] c.c.s.s.springbootdemoapplication : starting springbootdemoapplication on desktop-kb78hjk with pid 1700 (e:\workspace\springbootdemo\target\classes started by gepengfa in e:\workspace\springbootdemo) 2018-10-25 23:52:41.990 info 1700 --- [ main] c.c.s.s.springbootdemoapplication : no active profile set, falling back to default profiles: default 2018-10-25 23:52:42.088 info 1700 --- [ main] ationconfigembeddedwebapplicationcontext : refreshing org.springframework.boot.context.embedded.annotationconfigembeddedwebapplicationcontext@7f416310: startup date [thu oct 25 23:52:42 cst 2018]; root of context hierarchy 2018-10-25 23:52:44.561 info 1700 --- [ main] s.b.c.e.t.tomcatembeddedservletcontainer : tomcat initialized with port(s): 8080 (http) 2018-10-25 23:52:44.584 info 1700 --- [ main] o.apache.catalina.core.standardservice : starting service [tomcat] 2018-10-25 23:52:44.588 info 1700 --- [ main] org.apache.catalina.core.standardengine : starting servlet engine: apache tomcat/8.5.16 2018-10-25 23:52:44.813 info 1700 --- [ost-startstop-1] o.a.c.c.c.[tomcat].[localhost].[/] : initializing spring embedded webapplicationcontext 2018-10-25 23:52:44.813 info 1700 --- [ost-startstop-1] o.s.web.context.contextloader : root webapplicationcontext: initialization completed in 2733 ms 2018-10-25 23:52:45.074 info 1700 --- [ost-startstop-1] o.s.b.w.servlet.servletregistrationbean : mapping servlet: 'dispatcherservlet' to [/] 2018-10-25 23:52:45.083 info 1700 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean : mapping filter: 'characterencodingfilter' to: [/*] 2018-10-25 23:52:45.083 info 1700 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean : mapping filter: 'hiddenhttpmethodfilter' to: [/*] 2018-10-25 23:52:45.083 info 1700 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean : mapping filter: 'httpputformcontentfilter' to: [/*] 2018-10-25 23:52:45.085 info 1700 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean : mapping filter: 'requestcontextfilter' to: [/*] 2018-10-25 23:52:45.582 info 1700 --- [ main] s.w.s.m.m.a.requestmappinghandleradapter : looking for @controlleradvice: org.springframework.boot.context.embedded.annotationconfigembeddedwebapplicationcontext@7f416310: startup date [thu oct 25 23:52:42 cst 2018]; root of context hierarchy 2018-10-25 23:52:45.705 info 1700 --- [ main] s.w.s.m.m.a.requestmappinghandlermapping : mapped "{[/demo],methods=[post]}" onto public void com.cfxmn.springboot.springbootdemo.controller.democontroller.demotest() 2018-10-25 23:52:45.710 info 1700 --- [ main] s.w.s.m.m.a.requestmappinghandlermapping : mapped "{[/error]}" onto public org.springframework.http.responseentity<java.util.map<java.lang.string, java.lang.object>> org.springframework.boot.autoconfigure.web.basicerrorcontroller.error(javax.servlet.http.httpservletrequest) 2018-10-25 23:52:45.711 info 1700 --- [ main] s.w.s.m.m.a.requestmappinghandlermapping : mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.modelandview org.springframework.boot.autoconfigure.web.basicerrorcontroller.errorhtml(javax.servlet.http.httpservletrequest,javax.servlet.http.httpservletresponse) 2018-10-25 23:52:45.759 info 1700 --- [ main] o.s.w.s.handler.simpleurlhandlermapping : mapped url path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.resourcehttprequesthandler] 2018-10-25 23:52:45.759 info 1700 --- [ main] o.s.w.s.handler.simpleurlhandlermapping : mapped url path [/**] onto handler of type [class org.springframework.web.servlet.resource.resourcehttprequesthandler] 2018-10-25 23:52:45.817 info 1700 --- [ main] o.s.w.s.handler.simpleurlhandlermapping : mapped url path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.resourcehttprequesthandler] 2018-10-25 23:52:46.321 info 1700 --- [ main] o.s.j.e.a.annotationmbeanexporter : registering beans for jmx exposure on startup 2018-10-25 23:52:46.529 info 1700 --- [ main] s.b.c.e.t.tomcatembeddedservletcontainer : tomcat started on port(s): 8080 (http) 2018-10-25 23:52:46.599 info 1700 --- [ main] c.c.s.s.springbootdemoapplication : started springbootdemoapplication in 5.092 seconds (jvm running for 5.764)
从启动日志标黄的部分可以看出,项目启动成功了,访问端口默认是8080(这个端口是可以改动的)
下面我们通过postman请求下,
查看控制台
2018-10-25 23:59:26.385 info 1700 --- [nio-8080-exec-2] c.c.s.s.controller.democontroller : success call
说明调用成功。
到此,一个简单的springboot项目就构建完成了,但这只是一个空的架子,内容还可载丰富。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Java实现合并两个有序序列算法示例
下一篇: MySql学习心得之存储过程