本篇文章讲述springboot搭建,从maven安装开始说起。
笔者有个习惯,就是学习一个技术,都是先从demo看起来,然后再深入去学习。所以每次接触一个新技术,我都会习惯性的去搭建可以用的小demo。
所以新手学习找demo,一定要找同样是新手总结的教程,因为他们总结的教程更细。
下面是详细的教程:
1、maven安装
1)maven安装,首先需要从官网下载maven文件:http://mirrors.hust.edu.cn/apache/maven/binaries/.
笔者下载的是apache-maven-3.2.2-bin.tar.gz,这边重点看后缀名,后缀名是:bin.tar.gz,至于版本,可以根据自己的喜好来选择。下下来之后解压出apache-maven-3.2.2,并将这个文件夹拷贝到d盘根目录下,笔者喜欢把软件放在d盘。
2)配置maven的环境变量
右击计算机–选择属性–选择更改配置–选择高级–选择环境变量–在下面系统变量里头新建系统变量,如下:
系统变量:
MAVEN_HOME = D:\Development\apache-maven-3.2.2(路径就是我们存放maven文件夹的路径)
path = %MAVEN_HOME%\bin(path如果本来就有,那么就把这个值添加到原来的后面即可)
3)验证maven是否配置成功
打开 cmd,在里面敲:mvn -version
配置成功的控制台日志如下:
C:\Users\wlt>mvn -version
Apache Maven 3.2.2 (45f7c06d68e745d05611f7fd14efb6594181933e; 2014-06-17T
2+08:00)
Maven home: D:\apache-maven-3.2.2\bin..
Java version: 1.7.0_71, vendor: Oracle Corporation
Java home: D:\Java\jdk1.7.0_71\jre
Default locale: zh_CN, platform encoding: GBK
OS name: “windows 7”, version: “6.1”, arch: “amd64”, family: “windows”
4)maven配置
4.1)如果有过工作经验的人,都懂的maven的D:\apache-maven-3.2.2\conf目录下有一个setting.xml文件,这个文件很多公司都会去重写它,把里面的镜像地址换成公司自己的nexus地址和把本地仓库换成统一的地址。笔者这边因为是自己学习用的,所以并没有去修改镜像地址,只修改了本地仓库地址,目的就是避免使用默认的仓库地址会让c盘空间浪费。
本地仓库地址修改方法:
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<!--默认是注释掉的,就是使用默认的地址,笔者这边新增下面这行,意思是使用配置的地址作为本地仓库-->
<localRepository>E:\maven\repository</localRepository>
5)myeclpise maven配置
myeclipse配置路径:
window–preference–输入maven回车
如下两图做设置就好了:
至此,maven工作全部结束。
2、springboot最简单demo搭建
demo源码我已经丢到了csdn的下载里头了,连接如下:
http://download.csdn.net/detail/wltsysterm/9800098
在导入项目之后,我这边遇到的问题是,pom文件报错,一般都是第一行报错。经过查看,我发现,其实是因为第一行换行结果导致了,只要把如下内容整成一行,就没问题了。
<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">
代码到此都over了。
3、运行demo
springBoot\src\main\java\com\neo\Application.java直接run它的main方法项目就起来了,控制台日志如下:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.3.8.RELEASE)
2017-03-31 13:55:21.027 INFO 5800 --- [ main] com.neo.Application : Starting Application on wlt-PC with PID 5800 (G:\MyEclipsepro\springBoot\target\classes started by wlt in G:\MyEclipsepro\springBoot)
2017-03-31 13:55:21.031 INFO 5800 --- [ main] com.neo.Application : No active profile set, falling back to default profiles: default
2017-03-31 13:55:21.097 INFO 5800 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@30583b71: startup date [Fri Mar 31 13:55:21 CST 2017]; root of context hierarchy
2017-03-31 13:55:22.928 INFO 5800 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-03-31 13:55:22.939 INFO 5800 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-03-31 13:55:22.940 INFO 5800 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.37
2017-03-31 13:55:23.023 INFO 5800 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-03-31 13:55:23.023 INFO 5800 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1928 ms
2017-03-31 13:55:23.300 INFO 5800 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-03-31 13:55:23.303 INFO 5800 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-03-31 13:55:23.303 INFO 5800 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-03-31 13:55:23.304 INFO 5800 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-03-31 13:55:23.304 INFO 5800 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-03-31 13:55:23.449 INFO 5800 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@30583b71: startup date [Fri Mar 31 13:55:21 CST 2017]; root of context hierarchy
2017-03-31 13:55:23.514 INFO 5800 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.neo.controller.HelloWorldController.index()
2017-03-31 13:55:23.518 INFO 5800 --- [ 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)
2017-03-31 13:55:23.519 INFO 5800 --- [ 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)
2017-03-31 13:55:23.556 INFO 5800 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-03-31 13:55:23.556 INFO 5800 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-03-31 13:55:23.603 INFO 5800 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-03-31 13:55:23.726 INFO 5800 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
在浏览器控制台输入:http://localhost:8080/hello
你的第一个springboot程序就起来了。
over。