workshop学习总结:spring boot入门 (一)

前言

这是在老师 @阿尔法二狗 的指导下第一次参与workshop,从一开始的项目要求到需求分析再到代码设计,整个过程下来对Springboot有了较全面的了解,也对项目的开发应该具备的思路有了宏观的概念,我以前拿到项目就开始分析,代码设计,到最后看清楚需求的具体要求致使之前设计的代码全部推倒重新设计,窃以为,设计的过程应该是又大到小再到大的过程,将需求细化分析,整理分到各个步骤模块,在从最基础的部分一个模块一个模块的实现,最后由这些小模块再组成整个系统。

参考资料:
http://www.infoq.com/cn/news/2017/06/Dont-learn-code-Learn-think
http://blog.csdn.net/jsyxcjw/article/details/46763639/
http://www.infoq.com/cn/articles/microframeworks1-spring-boot/

1、spring boot

1.1 spring boot 简介

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。

从最根本上来讲,Spring Boot就是一些库的集合,它能够被任意项目的构建系统所使用。

  • Spring Boot具有如下特性:
  • 为基于Spring的开发提供更快的入门体验
  • 开箱即用,没有代码生成,也无需XML配置。同时也可以修改默认值来满足特定的需求。
  • 提供了一些大型项目中常见的非功能性特性,如嵌入式服务器、安全、指标,健康检测、外部配置等。
  • Spring Boot的目标不在于为已解决的问题域提供新的解决方案,而是为平台带来另一种开发体验,从而简化对这些已有技术的使用

1.2 系统配置

开发工具为intellij ideal 2017.2
使用到开发工具中的maven和spring initializr
Spring Boot建议使用Maven或Gradle,本文以Maven为例。
(maven要使用Java本) 打开cmd检查版本

C:\Users\Administrator>mvn -v
Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-04T03:39:06+08:00)
Maven home: D:\Apache-MAVEN\bin\..
Java version: 1.8.0_92, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.8.0_92\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

2、第一个Spring boot项目:基于RestfulAPI的Service服务-Micro-Service to ingestion auto seller data

2.1 pom.xml文件配置

新建springboot 项目,目录src下有两个文件main和Test,代码写在main中,Test是单元测试代码

在目录中有一个pom.xml

@requires_authorization
<?xml version="1.0" encoding="UTF-8"?>
<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.kbkj</groupId>
    <artifactId>plant</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>plant</name>
    <description>SpringMarket</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

pom.xml 可以进行依赖管理
GroupID 项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构。
ArtifactID 是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称。
version 使用版本

    <groupId>com.kbkj</groupId>
    <artifactId>plant</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

2.2 创建新项目

在plant\src\main\resources\application.properties,配置数据库连接

spring.datasource.url = jdbc:mysql://localhost:3306/supermaker?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8&autoReconnect=true
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

因为maven会默认编译在src/main/java下的代码,所以/java文件中创建三个文件夹,分别是contrller(控属于展示层的一部分)、services(业务层)、modle(作为数据的结构化载体贯穿整个展示层、业务层和数据层).
在编写代码时,可以由上至下的开发,也可以由下至上开发.

package com.kbkj.plant.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/hello"})
public class PlantsController {

    @RequestMapping(value = "/text",method = {RequestMethod.GET})
    public String RepublecController(){
        return "hello world";
    }

}

@RestController和@RequestMapping说明:

@RestController 被称为一个构造型(stereotype)注解。它为阅读代码的人们提供建议。对于Spring,该类扮演了一个特殊角色。我们的类是一个web @Controller,所以当处理进来的web请求时,Spring会询问它。

@RequestMapping 注解提供路由信息。它告诉Spring任何来自"/"路径的HTTP请求都应该被映射到home方法。@RestController注解告诉Spring以字符串的形式渲染结果,并直接返回给调用者。

package com.kbkj.plant;

import org.springframework.boot.SpringApplication;

@SpringBootApplication
public class PlantApplication {

    public static void main(String[] args) {
        SpringApplication.run(PlantApplication.class, args);
    }
}

main方法。这只是一个标准的方法,它遵循Java对于一个应用程序入口点的约定。我们的main方法通过调用run,将业务委托给了Spring Boot的SpringApplication类。SpringApplication将引导我们的应用,启动Spring,相应地启动被自动配置的Tomcat web服务器。

除了常见的Spring框架事件,比如ContextRefreshedEvent,一个SpringApplication也发送一些额外的应用事件。一些事件实际上是在ApplicationContext被创建前触发的。

  • 可以使用多种方式注册事件监听器,最普通的是使用SpringApplication.addListeners(…)方法。在应用运行时,应用事件会以下面的次序发送:
  • 在运行开始,但除了监听器注册和初始化以外的任何处理之前,会发送一个ApplicationStartedEvent。

  • 在Environment将被用于已知的上下文,但在上下文被创建前,会发送一个ApplicationEnvironmentPreparedEvent。

  • 在refresh开始前,但在bean定义已被加载后,会发送一个ApplicationPreparedEvent。

  • 启动过程中如果出现异常,会发送一个ApplicationFailedEvent。

开始运行,控制台输出

"C:\Program Files\Java\jdk1.8.0_92\bin\java" -XX:TieredStopAtLevel=1 -noverify -

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

2017-06-20 11:09:12.697  INFO 2764 --- [           main] com.kbkj.plant.PlantApplication          : Starting PlantApplication on BF-20160708YRVN with PID 2764 (E:\plant\target\classes started by Administrator in E:\plant)
2017-06-20 11:09:12.703  INFO 2764 --- [           main] com.kbkj.plant.PlantApplication          : No active profile set, falling back to default profiles: default
2017-06-20 11:09:12.795  INFO 2764 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4b9e255: startup date [Fri Jun 20 11:09:12 CST 2017]; root of context hierarchy
2017-06-20 11:09:14.442  INFO 2764 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$f5fdcdc6] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

打开浏览器输入localhost:8080 可以看到

hello world

2.3 Spring Beans和依赖注入

在程序的编写中可以自由地使用任何标准的Spring框架技术去定义beans和它们注入的依赖。简单起见,常用@ComponentScan注解搜索beans,并结合@Autowired构造器注入。

如果使用上面建议的结构组织代码(将应用类放到根包下),可以添加@ComponentScan注解而不需要任何参数。你的所有应用程序组件(@Component, @Service, @Repository, @Controller等)将被自动注册为Spring Beans。

2.4 Micro-Service to ingestion auto seller data 代码设计

设计之前,要知道本次项目是为了设计一个超市自动化收银程序,目的是在银行打折,和国内外产品,买多赠送,这些条件的基础上自动计算出最后总价格。

首先知道应该具有的属性,

package com.kbkj.plant.model;

/**
 * Created by Administrator on 2017/6/10 0010.
 */
public class SaleInfo {
    private String productNum;
    private int count;

    public String getProductNum() {
        return productNum;
    }

    public void setProductNum(String productNum) {
        this.productNum = productNum;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}

设计接口

package com.kbkj.plant.services;

import com.kbkj.plant.model.SaleInfo;

import java.util.List;

public interface DataFormat {
    List<SaleInfo> format (List<String> saleInfo);
}

实现方法

package com.kbkj.plant.services;

import com.kbkj.plant.model.SaleInfo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/6/10 0010.
 */
public class DataFormatImpl implements DataFormat {
    public List<SaleInfo> format(List<String> saleInfo){
        HashMap<String,Integer> output = new HashMap<String,Integer>();
        for(String item:saleInfo){
            String productNumber = item;
            int count = 1;
            if(item.contains("-")){
                String [] splitted = item.split("-");
                productNumber = splitted[0];
                count = new Integer(splitted[1]);
            }
            if(!output.containsKey(productNumber)){
                output.put(productNumber,count);
            }else {
                output.put(productNumber,output.get(productNumber)+count);
            }
        }

        List<SaleInfo> results = new ArrayList();
        for (String key : output.keySet()) {
            SaleInfo item = new SaleInfo();
            item.setProductNum(key);
            item.setCount(output.get(key));
            results.add(item);
        }

        return results;
    }
}

2.5 @Test 单元测试

在pom.xml里面可以添加单元测试需要的jar

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>

@RunWith(SpringJUnit4ClassRunner.class) 指定我们SpringBoot工程的Application启动类
@SpringApplicationConfiguration(classes = App.class)由于是Web项目,Junit需要模拟ServletContext,因此我们需要给我们的测试类加上@WebAppConfiguration。
@WebAppConfiguration 测试环境使用,用来表示测试环境使用的ApplicationContext将是WebApplicationContext类型的,value指定web应用的根

package com.kbkj.plant.services;
import com.kbkj.plant.PlantApplication;
import com.kbkj.plant.model.SaleInfo;
import org.junit.Before;
import org.junit.Test;
import org.junit.Assert;
import org.junit.runner.RunWith;

import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = PlantApplication.class)
@WebAppConfiguration
public class DataFomatImplTest {
    private DataFormat subject;
    @Before
    public void setup(){
        subject = new DataFormatImpl();
    }
    @Test
    public void testIfDataFormatCanFormatData(){
        List<String> input = new ArrayList();
        input.add("10001");
        input.add("10001");
        input.add("10001-2");
        input.add("10002-2");
        input.add("10002-3");
        input.add("10003-1");
        List<SaleInfo> output = subject.format(input);
        Assert.assertEquals(output.get(0).getProductNum(), "10001");
        Assert.assertEquals(output.get(0).getCount(), "4");
        Assert.assertEquals(output.get(1).getProductNum(), "10002");
        Assert.assertEquals(output.get(1).getCount(), "5");
        Assert.assertEquals(output.get(2).getProductNum(),"10003");
        Assert.assertEquals(output.get(2).getCount(),"1");


    }
}

3 学习总结

熟悉了解spring boot的创建,以及设计结构,依赖的配置,maven的使用。

对于项目的需求分析方法

在收到客户需求后,一定要对需求进行详尽的分析,将需求按照重要程度排序,将需求尽可能的细化,功能进行拆分,将有逻辑关系的功能模块进行分析,找到进入模块的类型再考虑从逻辑模块出来的类型,有进必要有出,数据类型也要统一。设计好模块的输入和要求的输出,完成好最低级需求的分析后才可开始按模块的进行开发。

总结一下上面所说的,在做项目时,开发与测试应该同步进行,编程能力是每个工程师都应该提升的能力,但编程本身并不是我们的目的。计算机和程序只是工具,它们是我们通向终点的桥梁。学习编程思想的真正的目标应该是教会人们如何思考,

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,290评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,399评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,021评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,034评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,412评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,651评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,902评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,605评论 0 199
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,339评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,586评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,076评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,400评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,060评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,083评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,851评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,685评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,595评论 2 270

推荐阅读更多精彩内容