Spring-Boot框架初步搭建
发布时间 - 2026-01-11 00:11:17 点击率:次一、简介

SpringMVC是非常伟大的框架,开源,发展迅速。优秀的设计必然会划分、解耦。所以,spring有很多子项目,比如core、context、bean、mvc等。这对知根底的人来说很简单明了,然而springmvc就是为了傻瓜式的操作而发明的。对于初学springmvc的人来说,想要入手就开发需要拷贝一连串的dependency而不知道这个是干嘛,不知道是不是少了依赖。像我刚接触springmvc的时候到处百度教程而发现各有不同,于是复制了一个又一个代码却不能自己设置,根本原因是不了解各个依赖的包。
Spring-Boot 正是为了解决繁复的代码配置而产生的。Spring-Boot 也是基于java-base 开发的代码,及不用xml文件配置,所有代码都由java来完成。还可以加入Groovy的动态语言执行。
二、搭建一个基本的web-mvc 项目
2.1 Configure environment
- java 1.8+
- maven 3.3+
- spring-boot 1.3.5
- idea 15
- Thymeleaf 3
2.2 Start
在idea中,选择new-》maven创建一个空的maven项目,比如名字springboot-test。
2.2.1pom.xml
设定java版本:
<properties>
<java.version>1.8</java.version>
</properties>
添加依赖版本管理dependencyManagement
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.3.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
添加spring-web项目依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
添加build-plugin
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
如此,一个简单的restful的webservice的项目就搭建好了。如果想要支持视图渲染,即jsp、freeMark、velocity等,添加对应的依赖即可。比如,我使用Thymeleaf模板:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.2.2 创建java代码
如果新建项目的名字是:springboot-test. 则创建包springboot-test/src/main/java/com/test.
com
+- example
+- myproject
+- Application.java
|
+- domain
| +- Customer.java
| +- CustomerRepository.java
|
+- service
| +- CustomerService.java
|
+- web
+- CustomerController.java
com.test是我们的基本包名。下面创建配置类com.test.AppConfig。
package com.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Created by miaorf on 2016/6/19.
*/
@SpringBootApplication
public class AppConfig {
public static void main(String[] args) {
SpringApplication.run(AppConfig.class);
}
}
@SpringBootApplication 标注启动配置入口,可以发现通过一个main方法启动。使用这个注解的类必须放置于最外层包中,因为默认扫描这个类以下的包。否则需要自己配置@ComponentScan。
这样,配置基本完成了。下面开发控制层controller:
创建com.test.controller.HelloController。
package com.test.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
/**
* Created by miaorf on 2016/6/19.
*/
@Controller
public class HelloController {
@RequestMapping("/index")
public String index(Model model){
model.addAttribute("name","Ryan");
return "index";
}
@RequestMapping("/json")
@ResponseBody
public Map<String,Object> json(){
Map<String,Object> map = new HashMap<String,Object>();
map.put("name","Ryan");
map.put("age","18");
map.put("sex","man");
return map;
}
}
创建视图代码:
视图默认放在springboot-test\src\main\resources\templates**.
所以创建springboot-test\src\main\resources\templates\index.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>
D:\workspace\springboot\springboot-test\src\main\webapp\hello.html
<!DOCTYPE HTML> <html> <head> <title>Getting Started: Serving Web Content</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> hello, This is static page. not resolve by server, just the html. </body> </html>
2.2.3 run
启动方式多种,可以启动main方法,也可以通过命令行启动:
D:\temp\springboot-test>mvn spring-boot:run [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for com.test:springboot-test:jar:1.0-SNAPSHOT [WARNING] 'build.plugins.plugin.version' for org.springframework.boot:spring-boot-maven-plugin is missing. @ line 49, column 21 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building springboot-test 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] >>> spring-boot-maven-plugin:1.3.5.RELEASE:run (default-cli) > test-compile @ springboot-test >>> [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ springboot-test --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 2 resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ springboot-test --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ springboot-test --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\temp\springboot-test\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ springboot-test --- [INFO] No sources to compile [INFO] [INFO] <<< spring-boot-maven-plugin:1.3.5.RELEASE:run (default-cli) < test-compile @ springboot-test <<< [INFO] [INFO] --- spring-boot-maven-plugin:1.3.5.RELEASE:run (default-cli) @ springboot-test --- [INFO] Attaching agents: [] . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.3.5.RELEASE)
浏览器访问:localhost:8080/index
demo下载路径:springboot_jb51.rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# spring
# boot框架搭建
# boot搭建
# boot
# 搭建教程
# Spring Boot构建框架详解
# Spring Boot快速搭建Spring框架教程
# Spring Boot 快速搭建微服务框架详细教程
# SpringBoot框架搭建教程分享
# JavaEE微框架Spring Boot深入解读
# 初识Spring Boot框架之Spring Boot的自动配置
# 初识Spring Boot框架和快速入门
# 【spring-boot】快速构建spring-boot微框架的方法
# 的人
# 好了
# 放在
# 还可以
# 有很多
# 而不
# 可以通过
# 很简单
# 少了
# 这对
# 我刚
# 各有
# 不了解
# 来完成
# 创建一个
# 都由
# 使用这个
# 命令行
# 开源
# 大家多多
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
Laravel如何自定义分页视图?(Pagination示例)
Laravel如何监控和管理失败的队列任务_Laravel失败任务处理与监控
智能起名网站制作软件有哪些,制作logo的软件?
JS实现鼠标移上去显示图片或微信二维码
Laravel如何创建自定义中间件?(Middleware代码示例)
Win11怎么关闭专注助手 Win11关闭免打扰模式设置【操作】
如何在沈阳梯子盘古建站优化SEO排名与功能模块?
Edge浏览器提示“由你的组织管理”怎么解决_去除浏览器托管提示【修复】
logo在线制作免费网站在线制作好吗,DW网页制作时,如何在网页标题前加上logo?
JavaScript如何实现继承_有哪些常用方法
消息称 OpenAI 正研发的神秘硬件设备或为智能笔,富士康代工
想要更高端的建设网站,这些原则一定要坚持!
制作ppt免费网站有哪些,有哪些比较好的ppt模板下载网站?
Laravel如何处理CORS跨域问题_Laravel项目CORS配置与解决方案
Laravel怎么实现软删除SoftDeletes_Laravel模型回收站功能与数据恢复【步骤】
Laravel如何实现URL美化Slug功能_Laravel使用eloquent-sluggable生成别名【方法】
IOS倒计时设置UIButton标题title的抖动问题
头像制作网站在线观看,除了站酷,还有哪些比较好的设计网站?
EditPlus 正则表达式 实战(3)
Laravel如何实现本地化和多语言支持_Laravel多语言配置与翻译文件管理
Laravel 419 page expired怎么解决_Laravel CSRF令牌过期处理
Laravel如何使用API Resources格式化JSON响应_Laravel数据资源封装与格式化输出
高性价比服务器租赁——企业级配置与24小时运维服务
如何用狗爹虚拟主机快速搭建网站?
googleplay官方入口在哪里_Google Play官方商店快速入口指南
Laravel如何创建自定义Artisan命令?(代码示例)
如何在VPS电脑上快速搭建网站?
Laravel怎么多语言本地化设置_Laravel语言包翻译与Locale动态切换【手册】
如何在阿里云虚拟服务器快速搭建网站?
PHP的CURL方法curl_setopt()函数案例介绍(抓取网页,POST数据)
制作无缝贴图网站有哪些,3dmax无缝贴图怎么调?
悟空浏览器如何设置小说背景色_悟空浏览器背景色设置【方法】
Laravel如何安装Breeze扩展包_Laravel用户注册登录功能快速实现【流程】
Laravel如何与Vue.js集成_Laravel + Vue前后端分离项目搭建指南
html文件怎么打开证书错误_https协议的html打开提示不安全【指南】
Laravel怎么配置S3云存储驱动_Laravel集成阿里云OSS或AWS S3存储桶【教程】
网站制作软件有哪些,制图软件有哪些?
Laravel怎么生成二维码图片_Laravel集成Simple-QrCode扩展包与参数设置【实战】
Midjourney怎么调整光影效果_Midjourney光影调整方法【指南】
详解免费开源的.NET多类型文件解压缩组件SharpZipLib(.NET组件介绍之七)
Windows10电脑怎么查看硬盘通电时间_Win10使用工具检测磁盘健康
如何快速查询域名建站关键信息?
深入理解Android中的xmlns:tools属性
香港服务器租用费用高吗?如何避免常见误区?
Python正则表达式进阶教程_复杂匹配与分组替换解析
如何确保FTP站点访问权限与数据传输安全?
Laravel如何使用Vite进行前端资源打包?(配置示例)
怎么用AI帮你设计一套个性化的手机App图标?
如何在香港服务器上快速搭建免备案网站?
南京网站制作费用,南京远驱官方网站?

