使用SpringBoot快速构建应用程序
来源:互联网 发布:免费售楼软件 编辑:程序博客网 时间:2024/04/30 17:53
1.Spring MVC
和Spring Boot
自带的web
构建方式有所区别。Spring
提供了spring-boot-starter-web
自动配置模块。
2. 添加如下依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency>
SpringBoot
默认的错误页面Springboot
默认的错误页面是一个称为whitelable error page
,可以在我们的项目中新建如下这样的一个Controller
来实现错误页面的映射
package com.artbrain.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;/** * Created by Administrator on 2016/6/16. */@Controllerpublic class IndexController { @RequestMapping(value = "/") public String index() { return "Here, is a error page!"; }}
但是更一般的做法是注册一个"/"
的controller
,代码如下
在Application.java
中重写addViewControllers
方法,注册一个viewController
@Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/home").setViewName("home"); registry.addViewController("/").setViewName("home"); registry.addViewController("/profile").setViewName("profile"); }
项目结构层面的约定
spring boot
内嵌了一个tomcat
,其项目结构与传统打包为war
包的Java Web
应用有很大的不同,具体为静态文件和页面模板的存放位置变了,原来是放在src/main/webapp
目录下的一系列资源,现在都要求放在src/main/resources
相应的子目录下面。具体体现为:src/main/resources/static
用于存放各种静态资源,比如css,js
等静态资源src/main/resources/templates
用于存放模板文件,比如*.html
可供选择的方式
如果想继续使用war包的形式,可以保持原有的项目结构,不过Spring Boot
使用的打包方式为'jar'
SpringMVC框架层面的约定和定制
spring-boot-starter-web默认自动配置了如下一些SpringMVC必要的组件:- 必要的viewResolver,比如ContentNegotiatingViewResolver和BeanNameResolver。
- 将必要的Converter,GenericConverter和Formatter等bean注册到IoC容器。
- 添加一系列的HttpMessageConverter以便支持对Web请求和相应的类型转换。
- 自动配置和注册MessageCodesConverter
我们可以在任何时候注册新的bean来替换SpringMVC的组件。
嵌入式Web容器层面的约定和定制
Spring-boot-starter-web默认使用嵌入式的tomcat作为web容器对外提供服务,默认的使用tomcat的默认端口:8080
,同时spring-boot-starter-web
提供了以下可选配置:- 替换
tomcat
服务器
引入spring-boot-starter-jetty
或者spring-boot-starter-undertow
作为替代方案 - 更改web容器的默认端口
更改配置选项为:server.port=9000(在spring boot项目的application.properties文件中更改,下面的示例代码中也给出了jdbc的配置方式)
spring.datasource.url=jdbc:mysql://localhost/spring_boot?autoReconnect=truespring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.jdbc.Driverserver.port=9000
- 替换
springBoot提供了很多server.为前缀的配置项用户对嵌入式web容器进行配置,比如:
- server.port
- server.address
- server.ssh.*
- server.tomcat.*
同时Spring
也允许我们直接对嵌入式的web
容器示例进行定制,可以通过IoC
容器中注册一个EmbeddedServletContainerCustomizer
类型的组件来对嵌入式的web容器进行定制。
- 使用SpringBoot快速构建应用程序
- 快速构建SpringBoot程序
- 使用 SCons 代替 Makefile 快速构建应用程序
- 使用 SCons 代替 Makefile 快速构建应用程序
- springboot+maven快速构建项目
- springBoot maven快速构建项目
- Springboot快速构建restful接口
- 使用springboot构建dubbo
- SpringBoot快速构建微服务架构体系
- SpringBoot快速入门-基本项目构建
- [SpringBoot]IDEA快速构建SpringBoot应用项目(一)
- 使用 AJAX 构建应用程序
- 使用SpringBoot快速搭建服务器
- 使用Springboot快速搭建项目
- 使用idea快速搭建springboot
- VC++2005快速构建安全的应用程序
- VC 2005快速构建安全的应用程序
- VC++2005快速构建安全的应用程序
- core dump调试
- 拷贝标准的选择屏幕修改为自定义的样式
- 怎么实现一个定时器?
- jq 淡入淡出 滑动 焦点等实际使用
- Http长连接、短连接、持久连接这三个概念的分析总结
- 使用SpringBoot快速构建应用程序
- Mac使用zsh导致maven命令无效的解决方案
- 【Java】内部类与外部类的互访使用小结
- php-代码优化准则
- 如何在项目启动的时候做一些业务处理 (实现ServletContextListener)
- HTML5 - 用<figure/>添加插图和图题
- 关于IE中image的onload事件失效解决问题
- UFUN学习板的测试与学习
- 数据库事务的特点与隔离级别