1、可以建一个working set,便于管理。
2、建一个maven项目(以web项目为例)
选择webapp原型(建web项目选webapp,不建web项目选择默认),点击下一步:
爆红不用管,是因为我关闭了jsp校验。
3、项目创建成功,然后,添加springboot依赖的jar。
打开pom.xml,粘贴一下依赖,junit可要可不要。spring-boot-starter-web 封装了web项目所需的jar。
<!-- 引入SpringBoot父类依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<!-- springboot-web组件 springmvc+spring+mybatis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
4、运行
导入pom.xml依赖后,使用maven install命令编译一下,把相关包依赖进来。
然后新建一个测试Controller类,并添加运行入口,如下图。然后右键运行main方法即可。
@EnableAutoConfiguration:简单理解为自动载入应用程序所需的所有Bean。
@RestController:简单理解为相当于@ResponseBody + @Controller合在一起的作用。
5、启动成功,端口默认8080
6、游览器访问(本地):http://localhost:8080/index
7、部署到tomcat(以上是自嵌套tomcat),传统的部署war到tomcat的webapp目录,对于此,需要做如下修改:
1.修改为war包格式
2.把main方法移出去,新建一个类管理(注:Controller类必须包含在main方法所在的目录之下,不然会找不到路径,因为扫描的是main之下的类)
代码:
@SpringBootApplication
public class App extends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(App.class);
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
8、使用maven install命令打成war包,把war拷入到tomcat的webapps目录下,启动tomcat即可。
运行结果如下:http://localhost/springboot-test/index
(访问地址没有端口号是因为我把端口号改为了80)
9、如有疑问可以添加微信xingsfdz交流