使用Spring Boot来加速Java web项目的开发
2016-09-23 20:35
405 查看
我想,现在企业级的Java web项目应该或多或少都会使用到Spring框架的。
回首我们以前使用Spring框架的时候,我们需要首先在(如果你使用Maven的话)pom文件中增加对相关的的依赖(使用gradle来构建的话基本也一样)然后新建Spring相关的xml文件,而且往往那些xml文件还不会少。然后继续使用tomcat或者jetty作为容器来运行这个工程。基本上每次创建一个新的项目都是这么一个流程,而我们有时候仅仅想快速的创建一个Spring web工程来测试一些东西,或者是希望能节省时间。
现在我们使用Spring Boot就可以快速的做到这些了。
我们先来看一个非常简单的使用Spring boot的例子吧:
1. 我们创建一个Maven工程,假定工程名字为spring-boot,然后我们在pom.xml文件中加入依赖:
2. 新建一个Controller来接受处理我们的请求:
相信大家已经看到了这个Controller有一个main方法,不要急,我们直接运行这个main方法:
会产生上面的输出,查看日志可以发现默认使用的是tomcat,端口绑定在8080,现在让我们来访问:http://localhost:8080/hello
就可以看到我们代码中输出的字样:hello world了。
回首这个过程,是不是相比于以前快速了许多呢
回首我们以前使用Spring框架的时候,我们需要首先在(如果你使用Maven的话)pom文件中增加对相关的的依赖(使用gradle来构建的话基本也一样)然后新建Spring相关的xml文件,而且往往那些xml文件还不会少。然后继续使用tomcat或者jetty作为容器来运行这个工程。基本上每次创建一个新的项目都是这么一个流程,而我们有时候仅仅想快速的创建一个Spring web工程来测试一些东西,或者是希望能节省时间。
现在我们使用Spring Boot就可以快速的做到这些了。
我们先来看一个非常简单的使用Spring boot的例子吧:
1. 我们创建一个Maven工程,假定工程名字为spring-boot,然后我们在pom.xml文件中加入依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.0.2.RELEASE</version> </dependency>
2. 新建一个Controller来接受处理我们的请求:
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方法:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.0.2.RELEASE) 2016-09-23 20:35:05.891 INFO 11956 --- [ main] com.jintian.controller.SimpleController : Starting SimpleController on DESKTOP-MHLT9GD with PID 11956 (E:\JAVAWork\Boot\target\classes started by yyt in E:\JAVAWork\Boot) 2016-09-23 20:35:05.915 INFO 11956 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7f416310: startup date [Fri Sep 23 20:35:05 CST 2016]; root of context hierarchy 2016-09-23 20:35:06.582 INFO 11956 --- [ main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8080 2016-09-23 20:35:06.759 INFO 11956 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat 2016-09-23 20:35:06.759 INFO 11956 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/7.0.47 2016-09-23 20:35:06.837 INFO 11956 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2016-09-23 20:35:06.837 INFO 11956 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 924 ms 2016-09-23 20:35:07.168 INFO 11956 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 2016-09-23 20:35:07.170 INFO 11956 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2016-09-23 20:35:07.257 ERROR 11956 --- [ main] o.a.coyote.http11.Http11NioProtocol : Failed to start end point associated with ProtocolHandler ["http-nio-8080"]
会产生上面的输出,查看日志可以发现默认使用的是tomcat,端口绑定在8080,现在让我们来访问:http://localhost:8080/hello
就可以看到我们代码中输出的字样:hello world了。
回首这个过程,是不是相比于以前快速了许多呢
相关文章推荐
- 使用Spring Boot来加速Java web项目的开发
- 使用Spring Boot来加速Java web项目的开发
- 使用Spring Boot来加速Java web项目的开发
- 使用Spring Boot来加速Java web项目的开发
- 使用Spring Boot来加速Java web项目的开发
- 使用Spring Boot来加速Java web项目的开发
- 使用Spring Boot来加速Java web项目的开发
- 使用Spring Boot来加速Java web项目的开发
- 使用Spring Boot来加速Java web项目的开发
- 使用Spring Boot来加速Java web项目的开发
- windows下关于使用eclipse开发web服务器后台项目完整配置(jsp+javabean+servlet)
- java-使用 freemarker 替换 jstl EL 开发 springmvc web项目
- 使用SpringMVC开发RestFul风格接口的java-web项目,tomcat部署并客户端访问
- 使用InteIIiJ IDEA开发Java web 项目
- 黑马程序员_加速Java应用开发速度4:使用模板技术加速项目开发速度
- eclipse开发java的web项目时使用SSM框架的方法
- javaweb项目中使用百度地图进行项目开发
- 使用maven开发javaweb项目
- IntelliJ IDEA使用Application Server开发普通Java Web项目
- 实战突击:Java Web项目整合开发光盘使用