狂神SpringBoot学习笔记

快速创建

编写controller

package com.lcr.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/hello")
public class demo01controller {
    @GetMapping("/hello")
    @ResponseBody
    public String hello(){
        return "hello";
    }
}

原理

自动配置

pom.xml

        spring-boot-dependencies:核心依赖在父工程中

        引入springboot依赖的时候不需要指定版本,因为有这些版本仓库

启动器

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

说白了就是spring boot的启动场景,上述代码会自动导入web环境的所有依赖

主程序

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//@SpringBootApplication标注这个类是一个springboot的应用
@SpringBootApplication
public class Demo01Application {
    //将springboot应用启动
    public static void main(String[] args) {
        SpringApplication.run(Demo01Application.class, args);
    }

}

结论:springboot所有的自动配置都是在启动的时候扫描并加载,所有的自动配置类都在spring.factories,但是不一定生效,要判断条件是否成立。只要导入了对应的start,就有对应的启动器了,自动装配就会生效,然后配置成功

yaml

 

yaml可以直接给实体类赋值

# k=v

#对空格要求十分高
#普通的key-value
name: lcr

#可以注入到配置类中

#对象
student:
  name: lcr
  age: 3

#行内写法
student: {name: lcr,age: 3}

#数组
pets:
  - cat
  - dog
  - pig

pet: [cat,dog,pig]

给属性赋值的方法

建两个类

Dog.java

package com.lcr.pojo;

import org.springframework.stereotype.Component;

@Component
public class Dog {
    private String name;
    private Integer age;


    public Dog() {
    }

    public Dog(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

 Person.java

package com.lcr.pojo;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.xml.crypto.Data;
import java.util.List;
import java.util.Map;

@Component
//绑定yaml的person
@ConfigurationProperties(prefix = "person")
public class Person {
    private String name;
    private Integer age;
    private Boolean happy;
    private Data birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;

    public Person() {
    }

    public Person(String name, Integer age, Boolean happy, Data birth, Map<String, Object> maps, List<Object> lists, Dog dog) {
        this.name = name;
        this.age = age;
        this.happy = happy;
        this.birth = birth;
        this.maps = maps;
        this.lists = lists;
        this.dog = dog;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Boolean getHappy() {
        return happy;
    }

    public void setHappy(Boolean happy) {
        this.happy = happy;
    }

    public Data getBirth() {
        return birth;
    }

    public void setBirth(Data birth) {
        this.birth = birth;
    }

    public Map<String, Object> getMaps() {
        return maps;
    }

    public void setMaps(Map<String, Object> maps) {
        this.maps = maps;
    }

    public List<Object> getLists() {
        return lists;
    }

    public void setLists(List<Object> lists) {
        this.lists = lists;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", happy=" + happy +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}

application.yaml

person:
  name: lcr
  age: ${random.int}
  happy: false
  maps: {k1: v1,k2: v2}
  lists:
    - code
    - girl
    - play
  dog:
    name: wangcai
    age: 3

test.java

package com.lcr;

import com.lcr.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Demo01ApplicationTests {

    @Autowired
    private Person person;

    @Test
    void contextLoads() {
        System.out.println(person);
    }

}

松散绑定:yml中写的last-name,这个和lastName是一样的,-后面跟着的字母默认是大写的。这就是松散绑定


JSR303数据校验:这个就是我们可以在字段是增加一层过滤器验证,可以保证数据的合法性

 

复杂类型封装:yml中可以封装对象,使用@value就不支持

多环境切换

server:
  port: 8081
spring:
  profiles:
    active: dev
//选择
---
server:
  port: 8082
spring:
  profiles: dev
//开发环境

---
server:
  port: 8083
spring:
  profiles: test
//测试环境


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值