Spring-boot快速实现Spring框架配置

本文详细介绍了如何使用Spring Boot快速创建并运行一个简单的Maven工程,包括依赖配置、Controller实现基本功能及运行流程。通过示例代码,展示了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来接受处理我们的请求:


package com.boonya.demo.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;

@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-01-12 11:46:41.038  INFO 21860 --- [           main] c.b.demo.controller.SimpleController     : Starting SimpleController on RD-PJL-1372-01 with PID 21860 (C:\DEVELOPER_WORKSPACE\DEVELOPERS\ECLIPSE\eclipse_luna_SR2\workspace\SpringBootDemo\target\classes started by pengjunlin in C:\DEVELOPER_WORKSPACE\DEVELOPERS\ECLIPSE\eclipse_luna_SR2\workspace\SpringBootDemo)
2016-01-12 11:46:41.067  INFO 21860 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4d5698f: startup date [Tue Jan 12 11:46:41 CST 2016]; root of context hierarchy
2016-01-12 11:46:41.819  INFO 21860 --- [           main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8080
2016-01-12 11:46:41.988  INFO 21860 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-01-12 11:46:41.988  INFO 21860 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/7.0.52
2016-01-12 11:46:42.063  INFO 21860 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2016-01-12 11:46:42.063  INFO 21860 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 998 ms
2016-01-12 11:46:42.402  INFO 21860 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2016-01-12 11:46:42.403  INFO 21860 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-01-12 11:46:42.555  INFO 21860 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-12 11:46:42.611  INFO 21860 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.boonya.demo.controller.SimpleController.hello()
2016-01-12 11:46:42.625  INFO 21860 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-12 11:46:42.626  INFO 21860 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-12 11:46:42.732  INFO 21860 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-01-12 11:46:42.752  INFO 21860 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080/http
2016-01-12 11:46:42.753  INFO 21860 --- [           main] c.b.demo.controller.SimpleController     : Started SimpleController in 1.996 seconds (JVM running for 2.233)
   

会产生上面的输出,查看日志可以发现默认使用的是tomcat,端口绑定在8080,现在让我们来访问:http://localhost:8080/hello

就可以看到我们代码中输出的字样:hello world了。回首这个过程,是不是相比于以前快速了许多呢。


转载于:https://my.oschina.net/boonya/blog/601275

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值