spring boot–用于简便快捷的搭建基于spring的独立可运行的应用详解编程语言

创建一个Maven工程,在pom.xml文件中加入依赖:    

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>springboot</groupId> 
    <artifactId>springboot</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 
    <name>springboot</name> 
    <description /> 
       
    <!-- 父依赖 --> 
    <parent> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-parent</artifactId> 
        <version>1.2.2.RELEASE</version> 
    </parent> 
    <dependencies> 
        <dependency> 
            <!-- 导入jar包 --> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter-web</artifactId> 
        </dependency> 
    </dependencies> 
</project> 
 

新建一个Controller来接受处理请求:    

package com.i.springboot.controller; 
  
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 
  
@RestController 
@EnableAutoConfiguration 
public class TestController { 
      
    @RequestMapping(value="/test") 
    String home(){ 
        return "hello world"; 
    } 
      
    public static void main(String[] args) throws Exception { 
        SpringApplication.run(TestController.class, args); 
    } 
      
}

[代码]这个Controller有一个main方法,直接运行这个main方法    

  .   ____          _            __ _ _ 
 /// / ___'_ __ _ _(_)_ __  __ _ / / / / 
( ( )/___ | '_ | '_| | '_ // _` | / / / / 
 ///  ___)| |_)| | | | | || (_| |  ) ) ) ) 
  '  |____| .__|_| |_|_| |_/__, | / / / / 
 =========|_|==============|___/=/_/_/_/ 
 :: 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.AnnotationConfigEmbedde[email protected]: 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

[代码]直接访问http://localhost:8080/test    

浏览器输出 hello world,是不是简单方便

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/10139.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论