`
zhoujiangzi
  • 浏览: 92732 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Spring Boot的使用(一) HelloWorld

 
阅读更多

    前段时间在spring的官网上看到了spring boot这个工程,感觉挺好的,能够最大化的减少基于Spring web应用的开发,可以将项目打成独立的jar,然后再使用,从文档上看,目前支持Maven和Groovy 。于是就跟着文档写了下代码,还是熟悉的HelloWorld,采用maven来构建。

    首先是创建个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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.jacksoft</groupId>
  <artifactId>iSpring-test</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>iSpring-test Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
  
  <parent>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-parent</artifactId>
	    <version>1.0.0.RC3</version>
  </parent>
  <dependencies>
  	<!-- spring jar start -->
  		<dependency>
	        <groupId>org.springframework.boot</groupId>
	        <artifactId>spring-boot-starter-web</artifactId>
	    </dependency>
  		
  	<!-- spring jar end -->
  
  
  </dependencies>
  <build>
    <finalName>iSpring-test</finalName>
  </build>
  
  <repositories>
  	<repository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
	  	<repository>
	  		<id>repository.hibernate</id>
	  		<name>jar</name>
	  		<url>http://mirrors.ibiblio.org/maven2</url>
	  	</repository>
  </repositories>
  <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>
  
</project>

     需要将工程的parent设置为spring-boot-starter-parent,同时添加依赖spring-boot-starter-web,这样就基本可以了,接下来就是写个controller来试试:

 

package com.jacksoft.spring.controller;

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 HelloController {

	@RequestMapping(value="hello.do",method=RequestMethod.GET)
	@ResponseBody
	public String index(){
		return "Hello,World!!!";
	}
	
}

 

    很简单,就是返回Hello Wolrd字符,然后再写启动类,通过main方法来启动:

 

package com.jacksoft.spring.bootstrap;

import org.springframework.boot.SpringApplication;

import com.jacksoft.spring.controller.HelloController;

public class Bootstrap {
	
	public static void main(String[] args) {
		Object[] source = new Object[]{HelloController.class};
		SpringApplication.run(source, args);
	}
}

   使用SpringApplication来启动容器,spring已经为我们集成了tomcat容器,当然也可以使用Jetty服务器

    运行这个main函数,看控制台的信息:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::            (v1.0.0.RC3)

2014-02-21 11:24:31.995  INFO 4608 --- [           main] com.jacksoft.spring.bootstrap.Bootstrap  : Starting Bootstrap on Jack-PC with PID 4608 (E:\MyWorkSpaces\myspace\two\iSpring-test\target\classes started by Jack)
2014-02-21 11:24:32.046  INFO 4608 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1d332b: startup date [Fri Feb 21 11:24:32 CST 2014]; root of context hierarchy
2014-02-21 11:24:33.185  INFO 4608 --- [           main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8080
2014-02-21 11:24:33.412  INFO 4608 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2014-02-21 11:24:33.413  INFO 4608 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/7.0.47
2014-02-21 11:24:33.571  INFO 4608 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2014-02-21 11:24:33.572  INFO 4608 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1528 ms
2014-02-21 11:24:34.446  INFO 4608 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-02-21 11:24:34.533  INFO 4608 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello.do],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.jacksoft.spring.controller.HelloController.index()
2014-02-21 11:24:34.561  INFO 4608 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-02-21 11:24:34.562  INFO 4608 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-02-21 11:24:34.724  INFO 4608 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2014-02-21 11:24:34.754  INFO 4608 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port: 8080
2014-02-21 11:24:34.756  INFO 4608 --- [           main] com.jacksoft.spring.bootstrap.Bootstrap  : Started Bootstrap in 3.221 seconds (JVM running for 3.634)

   

   可以看到项目部署的一些基本信息,如端口,访问路径,容器啥的,最后就是在浏览器里面访问了

   访问地址:http://localhost:8080/hello.do

  就可以看到返回的helloworld了。

  到此,一个简单的例子就算完成了,

参考文档:http://projects.spring.io/spring-boot/

http://projects.spring.io/spring-boot/docs/README.html

分享到:
评论

相关推荐

    1. [视频]spring boot起步之Hello World【从零开始学Spring Boot】

    在本节中,我们将深入探讨"Spring Boot起步之Hello World"这一主题,这是学习Spring Boot框架的典型入门教程。Spring Boot是由Pivotal团队开发的一个框架,它旨在简化Spring应用程序的初始搭建以及开发过程,通过...

    spring boot - hello world

    "Hello World"是任何编程语言入门的第一个示例,对于Spring Boot也不例外。下面我们将深入探讨Spring Boot创建"Hello World"应用的关键知识点。 1. **起步依赖(Starter Dependencies)** Spring Boot的特性之一是...

    spring-boot-helloworld

    在“spring-boot-helloworld”项目中,我们看到这是一个入门级的Spring Boot应用,虽然规模不大,但包含了Spring Boot的核心特性,为初学者提供了很好的学习模板。下面我们将深入探讨其中的关键知识点。 1. **...

    spring-boot-helloworld.zip

    在 "spring-boot-helloworld.zip" 这个压缩包中,我们很可能是找到了一篇关于 Spring Boot 入门的博客文章示例代码,用于展示如何构建一个简单的 "Hello World" 应用。 在 Spring Boot 中创建一个 "Hello World" ...

    Spring boot 示例 官方 Demo

    spring-boot-helloWorld:spring-boot的helloWorld版本 spring-boot-mybaits-annotation:注解版本 spring-boot-mybaits-xml:xml配置版本 spring-boot-mybatis-mulidatasource:springboot+mybatis多数据源最简解决...

    spring-boot的helloWorld

    这个简单的"spring-boot-01-helloworld"程序,是学习Spring Boot的起点,它展示了如何快速构建一个可以运行的web应用。随着对Spring Boot的理解加深,你可以逐渐添加更多功能,如数据库操作、安全控制、定时任务等,...

    about learning Spring Boot. Spring Boot 教程、技术栈示例代码,快速简单上手教程.zip

    Spring Boot 学习示例 Spring Boot 2.0 Mysql 5.6 ...spring-boot-helloworld:Spring Boot 3.0 Hello World Test 单元测试示例 spring-boot-scheduler:Spring Boot 3.0 定时任务 scheduler 使用示例 .....

    精通 Spring Boot 42 讲

    第一部分是从零起步的基础内容,共4课,帮助大家快速认识 Spring Boot ,我会带领大家熟悉 Spring Boot 产生的背景和设计理念,同时也会讲解 Spring Boot 的环境搭建和项目介绍,最后以一个 Hello World 为例,来...

    spring-boot-01-helloworld.zip

    Spring Boot学习笔记-------(二)spring boot入门,配套例子代码,博客地址:https://blog.csdn.net/huaya1127/article/details/104130300

    Spring Boot Examples

    spring-boot-helloWorld:spring-boot的helloWorld版本 spring-boot-mybaits-annotation:注解版本 spring-boot-mybaits-xml:xml配置版本 spring-boot-mybatis-mulidatasource:springboot+mybatis多数据源最简...

    IDEA JAVA Spring Boot运行Hello World(1.8)

    在本教程中,我们将深入探讨如何使用IntelliJ IDEA(简称IDEA)这一强大的Java集成开发环境来创建和运行一个基本的Spring Boot项目,展示"Hello World"的应用。Spring Boot是一个简化Spring应用初始搭建以及开发过程...

    从零开始学Spring Boot

    1.3 spring boot起步之Hello World 1.4 Spring Boot返回json数据 1.5 Spring Boot热部署 1.6 Spring Boot使用别的json解析框架 1.7 全局异常捕捉 1.8 Spring Boot datasource - mysql 1.9 JPA - Hibernate 1.10 使用...

    Spring Boot Hello World 入门源代码,直接返回一个http的JSON的输出

    综上所述,"Spring Boot Hello World 入门源代码"演示了如何利用 Spring Boot 快速构建一个返回 JSON 的 Web 服务。这个过程中涉及到的核心概念包括 Spring Boot 框架、RESTful API 设计、HTTP 协议、以及 JSON 数据...

    spring boot 42讲配套源码.zip

    第 1-4 课:写一个 Hello World 来感受 Spring Boot/hello 第 2-1 课: Spring Boot 对基础 Web 开发支持/spring-boot-web 第 2-10 课: 使用 Spring Boot WebSocket 创建聊天室/spring-boot-websocket 第 2-2 课...

    Spring Boot 进阶笔记(详细全面) 中文PDF完整版.pdf

    要运行第一个“Hello World”程序,你可以创建一个 `@RestController` 注解的控制器类,如 `HelloWorldController`,使用 `@RequestMapping` 映射请求。启动应用后,通过访问 `http://127.0.0.1:8080/hello` 即可...

    Spring Boot——2分钟构建spring web mvc REST风格HelloWorld

    查看`spring-boot-helloworld`压缩包中的源码,可以看到项目的基本结构和代码实现。`pom.xml`是Maven的配置文件,定义了项目依赖;`src/main/resources`存放资源配置文件;`src/main/java`是代码目录。 **工具使用*...

    史上最简单的spring boot程序

    Java是编程语言,Spring Boot是构建在这个语言上的框架,而“helloworld”则暗示了这是一个基础的教学示例,用来展示如何使用Spring Boot来开发简单的Java应用。 至于压缩包中的文件“helloworld-1”,根据命名习惯...

    IntelliJ IDEA 创建spring boot 的Hello World 项目(图解)

    使用 IntelliJ IDEA 创建 Spring Boot 项目的 Hello World 项目非常简单,只需要按照上述步骤进行操作就可以快速创建一个 Spring Boot 项目。同时,我们也可以使用 IntelliJ IDEA 的其他功能,例如代码完成、代码...

    Spring Boot教程.pdf

    Hello World 是 Spring Boot 的入门项目,通过建立简单的 Web 项目来了解 Spring Boot 的基本结构和配置。 Hello World 项目包括了 Controller、Service、Dao 等层次,通过这些层次的设计和实现,我们可以了解 ...

    spring boot helloworld

    SpringBoot 入门Demo源码,只是单纯的分享给有需要的人使用,俗话说的好,万事开头难,当迈开了第一步,就相当于打开了一扇门,发现了新大陆,所以我们要持之以恒,才能取得成功,让我们一起努力,为心中最美好的...

Global site tag (gtag.js) - Google Analytics