后端框架技术学习笔记

2020/3/8

认识SpringBoot

SpringBoot的优点

  • 创建独立的Spring应用程序
  • 嵌入TomCat,无需部署WAR文件
  • 无代码生成,无xml配置要求

StringBoot 的 Hello World

pom文件依赖配置



    4.0.0

    org.example
    com.first
    1.0-SNAPSHOT

    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.3.RELEASE
    

    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
    

  • spring-boot-starter-parent作用

它提供依赖管理(类比Android的话,作用类似于ben-manes.versions插件??)引入之后,声明其管理范围内的依赖时不在需要填版本了

  • spring-boot-starter-web作用

    • spring-boot-starter-web:

    默认使用嵌套式的TomCat作为Web容器对开开发HTTP服务

    • spring-boot-starter:

    SpringBoot场景启动器,帮我们导入了web模块支出运行的组件

Spring Boot将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里面 引入这些starter相关场景的所有依赖都会导入进来。要用什么功能就导入什么场景的启动器

Application&Controller

//Application
/**标记为主程序启动类**/
@SpringBootApplication
public class ManualChaptApplication {
  public static void main(String[] args) {
    SpringApplication.run(ManualChaptApplication.class);
  }
}
/**
 *Controller
 * 该注解为组合注解
 * @ResponseBody+Controller
 */
@RestController
public class HelloController {

  /**
   *GetMapping:相当于@RequestMapping(value=”/hello“,RequestMethod.Get)
   */
  @GetMapping("/hello")
  public String hello() {
    return "hello World";
  }
}

注解的意思:

  • @SpringBootApplication:
    标注在某个类上,说明此类为SpringBoot的主配置类,SpringBoot运行此类的Main方法来启动StringBoot应用,会将主配置类(@SpringBootApplication标注的类)的所在包及下面所有子包里面的所有组件扫描到 Spring容器;
  • @RestController
    此注解为组合注解,意味着这个controller上的所有方法上都加了@ResponseBody

单元测试

@SpringBootTest  
class DemoApplicationTests {

  @Autowired
  private HelloController helloController;


  @Test
  void contextLoads() {
    String hello = helloController.hello();
    System.out.println("print:  " + hello);
  }
}

注解的意思:

  • @SpringBootTest:
    标记springBoot单元测试,并加载项目的applicationContext的上下文环境
  • @Autowired
    它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。

SpringBoot执行流程

  1. 开始
  2. 初始化Spring Application的实例
    • 判断当前项目的类型
    • 应用的初始化设置
    • 应用的监听设置
    • 设置项目的启动类
  3. 初始化Spring Boot项目的启动
    • 获取并运行监听器
    • 准备项目运行环境
    • 应用上下文装配
    • 启动上下文
    • 运行自定义执行器
    • 持续运行上下文
  4. 结束

2020/3/9

使用kotlin的注意事项

  • 因为底层问题,所以部分类型的声明需要注意对Java的兼容
@JvmOverloads
    @GetMapping("/spica")
    fun spica() = "spica 27"

   @GetMapping("/spica/{name}")
    fun spica(
            @PathVariable(name = "name")
            name: String? = "spica") = "

name=${name.toString()}

"

以下是今天碰见的问题:
- 重写方法加@JvmOverloads注解
- 类型声明要加?允许为null,如上直接使用String类型会报java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics

单元测试·JUnit4与JUnit 5对比

常用注解及其作用对比:

JUnit4 JUnit5 说明
@Test @Test 表示该方法是一个测试方法。JUnit5与JUnit 4的@Test注解不同的是,它没有声明任何属性,因为JUnit Jupiter中的测试扩展是基于它们自己的专用注解来完成的。这样的方法会被继承,除非它们被覆盖
@BeforeClass @BeforeAll 表示使用了该注解的方法应该在当前类中所有使用了@Test @RepeatedTest、@ParameterizedTest或者@TestFactory注解的方法之前 执行;
@AfterClass @AfterAll 表示使用了该注解的方法应该在当前类中所有使用了@Test、@RepeatedTest、@ParameterizedTest或者@TestFactory注解的方法之后执行;
@Before @BeforeEach 表示使用了该注解的方法应该在当前类中每一个使用了@Test、@RepeatedTest、@ParameterizedTest或者@TestFactory注解的方法之前 执行
@After @AfterEach 表示使用了该注解的方法应该在当前类中每一个使用了@Test、@RepeatedTest、@ParameterizedTest或者@TestFactory注解的方法之后 执行
@Ignore @Disabled 用于禁用一个测试类或测试方法
@Category @Tag 用于声明过滤测试的tags,该注解可以用在方法或类上;类似于TesgNG的测试组或JUnit 4的分类。
@Parameters @ParameterizedTest 表示该方法是一个参数化测试
@RunWith @ExtendWith @Runwith就是放在测试类名之前,用来确定这个类怎么运行的
@Rule @ExtendWith Rule是一组实现了TestRule接口的共享类,提供了验证、监视TestCase和外部资源管理等能力
@ClassRule @ExtendWith @ClassRule用于测试类中的静态变量,必须是TestRule接口的实例,且访问修饰符必须为public。

Maven的格式

  • 依赖配置
    • groupId:标识符
    • artifactId :单独的项目标识符
    • version:版本号
    • scope:任务的类路径及其如何限制依赖关系的传递性
    • exclusions:包含一个或多个排除元素,每个排除元素都包含一个表示要排除的依赖关系的 groupId 和 artifactId。与可选项不同,可能或可能不会安装和使用,排除主动从依赖关系树中删除自己。
 
      
            org.springframework.boot
            spring-boot-starter-test
            2.0
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    ...
  
  • build配置
    • plugins 插件
      • plugin
        • extensions:是否加载此插件的扩展名。
        • dependencies :插件本身需要的依赖

    

            org.apache.maven.plugins
                maven-compiler-plugin
                
                    
                        compile
                        compile
                        
                            compile
                        
                    
                    
                        testCompile
                        test-compile
                        
                            testCompile
                        
                    
                
            
            ... ...
    
  

配置文件

  • properties
  • yaml
  1. 基本格式要求:
    1. YAML大小写敏感;
    2. 使用缩进代表层级关系
    3. 缩进只能使用空格,不能使用TAB,不要求空格个数,只需要相同层级左对齐(一般2个或4个空格)
  2. 值的写法
    1. 普通的值:k:v
    2. 对象,Map:键值对
      1. k: v:在下一行写对象的属性和值的关系;注意缩进
      2. 行内写法
man:    
      name: jelly           
      age: 20 
man: {name: jelly,age: 18}

,
3. 数组(List Set)
一个短横代表一个数组项

people:
  - chengkeyu
  - zhaoqian
  - guzhaoyuan
  - yangweizhi

行内写法

people:[chengkeyu,zhaoqian,guzhaoyuan,yangweizhi]

随机值的使用

${random.value}

随机生成一个32位的字符串,如:b21d56d2f10d74f84608dfff01b25552

${random.int}

随机生成一个int范围的随机数,如:-386223791

${random.long}

随机生成一个long范围的随机数,如:2404587442488649534

${random.int(10)}

随机生一个[0,10]之间的随机数,如:8

${random.int[1024,65536]}

随机生成一个[1024,65536]之间的随机数,如:63856

${user.userName}

获取配置文件中已经配置好的属性值,不存在时直接返回整个“${user.userName}”

${user.width:hello}

获取配置文件中已经配置好的属性值,不存在时则用"hello"返回


示例

user:
  lastName: hello
  age: 18
  boss: false
  birth: 2017/12/12
  maps: {qq: 1000 , mobile: 13811002233}
  lists: []
  dog:
    name: jelly
    age: 18

address: 上海${random.value}
person.last-name=hello${user.lastName}

person.age=${random.int}

person.birth=2017/12/15

person.boss=false

person.maps.aa=v1

person.maps.bb=14

person.lists=a,b,c

person.dog.name=${person.hello:hello}_dog

person.dog.age=15

指定bean对象(Java)

//  指定配置文件
@PropertySource(value = {"classpath:application.yaml"})
//放入容器
@Component
//绑定
@ConfigurationProperties(prefix = "user")
public class User {

private String lastName;

private Integer age;

private Boolean boss;

private Date birth;

private Map maps;

private List lists;

private Dog dog;

get...
seting..
}

指定bean对象(Kotlin)

@PropertySource(value = ["classpath:application.yaml"])
@Component
@ConfigurationProperties(prefix = "user")
class User {
    var lastName: String? = null
    var age: Int? = null
    var isBoss = false
    var birth: Date? = null
    var maps: Map? = null
    var lists: List? = null
    var dog: Dog? = null
}

需要注意注解格式变化和添加?允许为空

Profile环境切换

多profile

我们在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml 默认使用application.properties的配置;
例如:
application-dev.properties:开发环境 application-test.properties:测试环境 application-prod.properties:生产环境

激活指定profile

  1. 在配置文件中指定 spring.profiles.active=dev
  2. 命令行: java -jar spring-boot-02-config-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev;可以直接在测试的时候,配置传入命令行参数
  3. 虚拟机参数; -Dspring.profiles.active=dev

2020/2/15

Bean:在计算机英语中有可重用组件的意思
JavaBean>=实体类
JavaBean:用java语言编写的可重用组件

你可能感兴趣的:(后端框架技术学习笔记)