Springboot系列文章

来源:互联网 发布:windows镜像下载网站 编辑:程序博客网 时间:2024/05/29 16:42

一、springboot简介
1.前世今生
  在boot没有出现之前,基于spring的开发,常常需要配置大量的xml文件。工程狮们苦不堪言,渐渐厌倦了配置文件的复制黏贴。spring家族因为这件事,也经常被其它技术阵营所诟病。
  boot是由毕威拓(Pivotal)团队基于spring开发的一个全新框架,可以理解为是对大量xml文件的一个抽象,基础的功能仍然是基于原生的spring,实际应用中开发人员不再需要编写xml就可以实现之前的功能。
2.web传送门
  需求:开发一个web应用,支持根据用户id查询用户信息(用户信息暂时从内存获取),并返回json格式信息.
  没有boot,开发人员需要:
    1.配置web.xml
    2.配置springmvc.xml配置文件
    3.配置spring.xml配置文件
    4.pom文件中增加依赖的各种jar
    5.编写controller类
    6.配置服务器,打成war包发布
  使用boot,开发人员只需要:
    1.编写controller类
    2.pom文件中增加很少的jar依赖配置
    3.编写启动类

居然减少了三个步骤,而且不需要拷贝黏贴xml.

二、下面demo基于boot 1.5.2开发
1.pom文件内容如下:

<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>com.github.braveboo</groupId>  <artifactId>sboot-example</artifactId>  <version>1.0-SNAPSHOT</version>  <modules>    <module>sboot-part1</module>  </modules>  <packaging>pom</packaging>  <name>sboot-example</name>  <url>http://maven.apache.org</url>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </properties>  <dependencyManagement>    <dependencies>      <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-dependencies</artifactId>        <version>1.5.2.RELEASE</version>        <type>pom</type>        <scope>import</scope>      </dependency>    </dependencies>  </dependencyManagement>  <dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>4.12</version>      <scope>test</scope>    </dependency>    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-web</artifactId>    </dependency>  </dependencies></project>
View Code

2.启动类

@SpringBootApplicationpublic class Application {public static void main( String[] args ){SpringApplication.run(Application.class, args);}}
View Code

3.controller

@RestControllerpublic class UserController {@RequestMapping("/getUser")User home() {return new User();}}
View Code

User对象

package com.github.braveboo.domain;public class User {private String name="lb";private String pwd="pwd";private String sex="男";private Integer id=1;private String verifyPassword="pwd";public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getVerifyPassword() {return verifyPassword;}public void setVerifyPassword(String verifyPassword) {this.verifyPassword = verifyPassword;}}
View Code

运行Application方法,通过浏览器访问返回如下内容:

{"name":"lb","pwd":"pwd","sex":"男","id":1,"verifyPassword":"pwd"}
View Code

三、下载地址

github:https://github.com/braveboo/sbootexample

下一篇文章将介绍boot中如何使用mybatis,敬请期待!

原创粉丝点击