SpringBoot-Day02

1、SpringBoot配置

1.1配置文件类型

application.yml

server:
  port: 80
#person:
#  name: lxj
#  age: 22
#  sex: true
#  maps: {k1: v1,k2: 12}
#  lists:
#      - hahaha
#      - hehehe
#  animal:
#    name: 蝙蝠
#    age: 10

application.properties

server.port=80

1.2yml基本语法

1)字面量:普通的值

name: lxj

“”:双引号;不会转义字符串里面的特殊字符;特殊字符会作为本身想表示的意思

2)k: v:在下一行来写对象的属性和值的关系;注意缩进

person:
  name: lxj
  age: 22

3)数组(List、Set):

用- 值表示数组中的一个元素

lists:
      - hahaha
      - hehehe

行内写法

  pets: [cat,dog,pig]

1.3读取配置文件

application.yml

server:
  port: 80
person:
  name: lxj
  age: 22
  sex: true
  maps: {k1: v1,k2: 12}
  lists:
      - hahaha
      - hehehe
  animal:
    name: 蝙蝠
    age: 10

Person.java

@Component
@ConfigurationProperties(prefix = "person")
@PropertySource(value = {"classpath:person.properties"})
public class Person {
//    @Value("${person.name}")
    private String name;
    private Integer age;
    private Boolean sex;
    private Date birth;

    private Map<String, Object> maps;
    private List<Object> lists;

    private Animal animal;

    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 getSex() {
        return sex;
    }

    public void setSex(Boolean sex) {
        this.sex = sex;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date 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 Animal getAnimal() {
        return animal;
    }

    public void setAnimal(Animal animal) {
        this.animal = animal;
    }

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

1.4Profile多环境支持

application.yml

spring:
  profiles:
    active: test
---
server:
  port: 80
spring:
  profiles:dev
---
server:
  port: 8081
spring:
  profiles:test
---
server:
  port: 8082
spring:
  profiles:fac

2、SpringBoot测试

步骤

2.1导入spring-boot-starter-test

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

2.2创建一个SpringBoot应用

@SpringBootApplication
public class App {
    //启动入口
    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }
}

2.3写SpringBoot测试

@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class SpringTest {
    @Autowired
    private TestService testService;

    @Test
    public void test() {
        testService.Test();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值