我想,现在企业级的Java web项目应该或多或少都会使用到Spring框架的。
回首我们以前使用Spring框架的时候,我们需要首先在(如果你使用Maven的话)pom文件中增加对相关的的依赖(使用gradle来构建的话基本也一样)然后新建Spring相关的xml文件,而且往往那些xml文件还不会少。然后继续使用tomcat或者jetty作为容器来运行这个工程。基本上每次创建一个新的项目都是这么一个流程,而我们有时候仅仅想快速的创建一个Spring web工程来测试一些东西,或者是希望能节省时间。
现在我们使用Spring Boot就可以快速的做到这些了。
我们先来看一个非常简单的使用Spring boot的例子吧:
1. 我们创建一个Maven工程,假定工程名字为spring-boot,然后我们在pom.xml文件中加入依赖:
1
2
3
4
5
|
<dependency> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version> 1.0 . 2 .RELEASE</version>
</dependency> |
2. 新建一个Controller来接受处理我们的请求:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
/** * Created by wenchao.ren on 2014/4/26.
*/
@Controller @EnableAutoConfiguration public class SimpleController {
@RequestMapping (value = "/hello" , method = RequestMethod.GET)
@ResponseBody
public String hello(){
return "hello world" ;
}
public static void main(String[] args) {
SpringApplication.run(SimpleController. class , args);
}
} |
相信大家已经看到了这个Controller有一个main方法,不要急,我们直接运行这个main方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | ' _| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1. 0.2 .RELEASE)
2014 - 04 - 26 22 : 54 : 40.985 INFO 7236 --- [ main] c.r.spring.boot.SimpleController : Starting SimpleController on rollen with PID 7236 (D:\workspace\GitHub\SpringDemo\spring-boot\target\classes started by wenchao.ren in D:\workspace\GitHub\SpringDemo\spring-boot)
2014 - 04 - 26 22 : 54 : 41.008 INFO 7236 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext @50de0926 : startup date [Sat Apr 26 22 : 54 : 41 CST 2014 ]; root of context hierarchy
2014 - 04 - 26 22 : 54 : 41.583 INFO 7236 --- [ main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8080
2014 - 04 - 26 22 : 54 : 41.706 INFO 7236 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2014 - 04 - 26 22 : 54 : 41.706 INFO 7236 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/ 7.0 . 52
2014 - 04 - 26 22 : 54 : 41.785 INFO 7236 --- [ost-startStop- 1 ] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2014 - 04 - 26 22 : 54 : 41.785 INFO 7236 --- [ost-startStop- 1 ] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 779 ms
2014 - 04 - 26 22 : 54 : 42.055 INFO 7236 --- [ost-startStop- 1 ] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2014 - 04 - 26 22 : 54 : 42.057 INFO 7236 --- [ost-startStop- 1 ] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [ /*]
2014-04-26 22:54:42.289 INFO 7236 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/ favicon.ico] onto handler of type [ class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014 - 04 - 26 22 : 54 : 42.368 INFO 7236 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.rollenholt.spring.boot.SimpleController.hello()
2014 - 04 - 26 22 : 54 : 42.376 INFO 7236 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [ class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014 - 04 - 26 22 : 54 : 42.377 INFO 7236 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [ class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014 - 04 - 26 22 : 54 : 42.447 INFO 7236 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2014 - 04 - 26 22 : 54 : 42.459 INFO 7236 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 /http
2014 - 04 - 26 22 : 54 : 42.460 INFO 7236 --- [ main] c.r.spring.boot.SimpleController : Started SimpleController in 1.675 seconds (JVM running for 1.944 )
2014 - 04 - 26 22 : 54 : 54.963 INFO 7236 --- [nio- 8080 -exec- 1 ] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2014 - 04 - 26 22 : 54 : 54.963 INFO 7236 --- [nio- 8080 -exec- 1 ] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet' : initialization started
2014 - 04 - 26 22 : 54 : 54.971 INFO 7236 --- [nio- 8080 -exec- 1 ] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet' : initialization completed in 8 ms
|
会产生上面的输出,查看日志可以发现默认使用的是tomcat,端口绑定在8080,现在让我们来访问:http://localhost:8080/hello
就可以看到我们代码中输出的字样:hello world了。
回首这个过程,是不是相比于以前快速了许多呢
参考资料:
相关推荐
java 使用 Swagger 创建一个Spring Boot 的 Web 服务java 使用 Swagger 创建一个Spring Boot 的 Web 服务java 使用 Swagger 创建一个Spring Boot 的 Web 服务java 使用 Swagger 创建一个Spring Boot 的 Web 服务java...
在基于Spring Boot、Spring Cloud 分布微服务开发过程中,根据实际项目环境,需要选择、集成符合项目需求的各种组件和积累各种解决方案。基于这样的背景下,我开源了本示例项目,方便大家快速上手Spring Boot、...
spring-boot-web:web开发综合使用案例 spring-boot-mail:spring boot和邮件服务 spring-boot-mongodb:spring boot和mongodb的使用 spring-boot-multi-mongodb:spring boot和mongodb多数据源的使用 spring-boot-...
使用微服务进行Web服务开发并与Spring Boot应用程序集成 无缝添加持久性和数据层,使您的Spring Boot Web应用程序做得更多 使用Spring Boot集成企业服务以创建更复杂的Java应用程序 本书适用于经验丰富的Java和...
基于 Spring Boot 构建 的 Java Web 平台企业级单体应用快速开发框架
使用 Spring Boot 开发项目,会给我们带来非常美妙的开发体验,可以从以下几个方面展开来说明 Spring Boot 让开发变得更简单 Spring Boot 对开发效率的提升是全方位的,我们可以简单做一下对比: 在没有使用 ...
Spring Boot 应用加速 ,文档清晰,学习Spring Boot的好资源
第 2-1 课: Spring Boot 对基础 Web 开发支持/spring-boot-web 第 2-10 课: 使用 Spring Boot WebSocket 创建聊天室/spring-boot-websocket 第 2-2 课 Spring Boot 项目中使用 JSP/spring-boot-jsp 第 2-3 课 ...
Oryx - 基于 Spring Boot 构建 的 Java Web 平台企业级前后端分离应用快速开发框架,适合中
主要介绍了使用Spring Boot搭建Java web项目及开发过程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
spring-boot-starter-web-2.0.7.0.jar
涵盖使用Spring Boot 进行Java EE 开发的绝大数应用场景,包含:Web 开发、数据访问、安全控制、批处理、异步消息、系统集成、开发与部署、应用监控、分布式系统开发等。 当你学完本书后,你将能使用Spring Boot ...
基于 Spring Boot 的 Java Web Server 框架,简化开发
Learn Spring Boot and how to build Java-based enterprise, web, and microservice applications with it. In this book, you'll see how to work with relational and NoSQL databases, build your first ...
该压缩文件包含了使用Spring Boot、Layui和Mybatis-plus开发的简单后台管理系统的源代码。这个项目提供了一套基本的功能和界面,帮助用户快速搭建和运行一个后台管理系统。 适用人群: 该资源适用于具备一定Java...
Pro Spring Boot 2: An Authoritative Guide to Building Microservices, Web and Enterprise Applications, and Best Practices Quickly and productively develop complex Spring applications and microservices...
围绕 软件优雅设计规约,基于Spring Boot 的 Java Web 开发框架,帮助使用者开发更易于阅读维护的代
该仓库中主要是 Spring Boot 的入门学习教程以及一些常用的 Spring Boot 实战项目教程,包括 Spring Boot 使用的各种示例代码,同时也包括一些实战项目的项目源码和效果展示,实战项目包括基本的 web 开发以及目前...
Spring boot 入门程序,访问html,dev tools 使用, 中文乱码处理
Spring Boot 进行Java EE 开发的绝大数应用场景,包含:Web 开发、数据访问、安全控制、批处理、 异步消息、系统集成、开发与部署、应用监控、分布式系统开发等。 当你学完本书后,你将能使用Spring Boot 解决Java ...