【前提】
maven下载安装,配置环境变量:
新建一个MAVEN_HOME,内容是解压的maven文件路径
在Path中添加 %MAVEN_HOME%\bin
在命令行中,使用mvn -v查看是版本及路径,确定是否配置正确
1、setting.xml文件:
profiles标签添加jdk1.8
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
2、idea配置maven,build->maven,配置安装的maven地址,勾选配置的override,选择安装的maven路径/conf/setting.xml
【第一个示例】:
一、功能说明 :浏览器发送hello请求,服务器接受请求并处理,响应hello world字符串。
1、创建maven工程(jar)。不勾选 create from archetype
2、导入springboot的相关依赖到pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
spring-boot-starter为spring boot场景启动器。
spring-boot-starter-web为web场景的启动器。
其它类型启动器的介绍:https://blog.csdn.net/u010919351/article/details/80238579
3、编写主程序,启动spring应用
package com.springboot.qa;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/*
* class前添加注解 @SpringBootApplication 用来标注一个主程序,说明这是一个spring应用
*/
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args){
//Spring应用启动起来
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
@SpringBootApplication :SpringBoot应用,标注此类为主配置类,SpringBoot就应该运行这个类的main方法来启动SpringBoot应用。
4、编写业务逻辑controller,view
package com.springboot.qa.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/*
* 注解@Controller
* */
@Controller
public class HelloController {
//@RequestMapping("/hello") 注解意义:接收来自浏览器的hello请求 都是Spring MVC的东东
//@ResponseBody 注解意义:把响应结果返回给浏览器
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "Hello World";
}
}
5、运行主程序,可以用浏览器访问
则在页面上打印返回值 : Hello World
6、部署
方法:先打包成jar包,再使用命令java -jar jar包名
1)先打包成jar包
导入maven插件到pom.xml
<!--这个插件,可以将应用打包成一个可执行的jar包-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
打包时使用:右侧工具栏maven Project
选择工程名-Lifecycle(生命周期)-双击package,则开始打包。
查看log日志,可查看到jar包所在位置
[INFO] Building jar: D:\scripts\java_new\springboot01helloworld\target\spring-boot-01-helloworld-1.0-SNAPSHOT.jar
2)运行jar包:使用命令java -jar jar包名
把jar包拷贝出来,使用命令执行。
[问题001]
报错:[ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:2.17:check (checkstyle-validation) on project spring-boot-01-helloworld: Failed during checkstyle execution: Unable to find suppressions file at location: src/checkstyle/checkstyle-suppressions.xml: Could not find resource ‘src/checkstyle/checkstyle-suppressions.xml’. -> [Help 1]
解决:引入parent包错误:应该是spring-boot-parent,而是spring-boot-starter-parent