SpringBoot的学习-入门搭建
内容简要
1.springBoot的pom.xml文件的支持
2.搭建一个简单的springboot项目
3.搭建项目进行访问
详细讲解
springBoot的pom.xml文件的支持
1.springboot必须在pom文件中设置为parent
需要添加spring-boot-starter-parent支持
2.导入spring boot的web支持
spring-boot-starter-web
3.添加Spring boot的插件[可选]
spring-boot-maven-plugin
代码部分
<?xml version="1.0" encoding="UTF-8"?>
<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>
<!--springboot的父类,使用springboot需要继承他,该parent包含了大量默认的配置,大大简化了我们的开发。-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
<groupId>com.pkk</groupId>
<artifactId>springboot0.1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging><!--打包方式-->
<properties>
<springmvc-version>4.3.7.RELEASE</springmvc-version>
<bonecp.version>0.8.0.RELEASE</bonecp.version>
<springboot.version>1.5.8.RELEASE</springboot.version>
</properties>
<dependencies>
<!--BoneCp数据源的支持-->
<dependency>
<groupId>com.jolbox</groupId>
<artifactId>bonecp-spring</artifactId>
<version>${bonecp.version}</version>
</dependency>
<!--springboot需要web的支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--添加Spring boot的插件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${springboot.version}</version>
</dependency>
<!--dan单元测试工具-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
搭建一个简单的springboot项目
代码部分
package com.pkk.springboot.action;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author peikunkun
* @version V1.0
* @Title: SpringBoot
* @Package com.pkk.springboot.action
* @Description: <>
* @date 11/13 0013 20:05
*/
@Controller
@SpringBootApplication
@Configuration
public class HelloAction {
@RequestMapping(value = "hello")
@ResponseBody
public String hello() {
return "Hello Word!";
}
public static void main(String[] args) {
SpringApplication.run(HelloAction.class, args);
}
}
代码讲解
- 注解描述
- @SpringBootApplication:Spring Boot项目的核心注解,主要目的是开启自动配置。;
- @Configuration:这是一个配置Spring的配置类;
- @Controller:标明这是一个SpringMVC的Controller控制器;
- main方法:在main方法中启动一个应用,即:这个应用的入口;
- 启动程序-两种方式
- 一种是直接run Java Application
- Spring Boot的Maven插件运行
- 启动结果
- 看到如下信息就说明启动成功了:
INFO 6188 — [ main] c.i.springboot.demo.HelloApplication : Started HelloApplication in 3.281 seconds (JVM running for 3.601)
- 看到如下信息就说明启动成功了:
项目访问
直接在浏览器就可以进行访问“http//localhoost:8080/hello”
注意
启动默认使用的监听的是Tomcat的808端开口 Tomcat initialized with port(s): 8080 (http)