SpringBoot -> 第一个注入Bean和Yaml配置

1.YML配置

program:
  age: 12
  list:
    - 12
    - 13
    - 14
  map:
    k1: a
    k2: b
  string: abc
  animal:
    name: dog
    anInt: 11

2.program类–做输出类

package com.rod.springboot.yaml;

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

import java.util.List;
import java.util.Map;

/**
 * 功能描述:prefix = "program"是找到yaml文件或者properties里名字为program的属性
 */
@Service
@ConfigurationProperties(prefix = "program")
public class Program {
    private String string;
    private int age = 0;
    private Animal animal;
    private List<Integer> list;
    private Map<String, String> map;

    @Override
    public String toString() {
        return "Program{" +
                "string='" + string + '\'' +
                ", age=" + age +
                ", animal=" + animal +
                ", list=" + list +
                ", map=" + map +
                '}';
    }

    public String getString() {
        return string;
    }

    public void setString(String string) {
        this.string = string;
    }

    public int getAge() {
        return age;
    }

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

    public Animal getAnimal() {
        return animal;
    }

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

    public List<Integer> getList() {
        return list;
    }

    public void setList(List<Integer> list) {
        this.list = list;
    }

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }
}

3.springboot-测试类

package com.rod.springboot;

import com.rod.springboot.yaml.Program;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class SpringbootApplicationTests {
    @Autowired
    private Program program;

    @Test
    public void contextLoads() {
        System.out.println(program);
        //Program{string='abc', age=22222, animal=Animal{name='dog', anInt=11}, list=[12, 13, 14], map={k1=a, k2=b}}
    }
}

4.pom配置类

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.rod</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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


</project>

5.总结:

1.springboot自带的test是junit5的,不是4.12的,没有junit5的要去下载jar
2.properties配置比yaml配置要优先,会先识别properties里的数据
3.不能在main里写东西,会有错误,如我–死活输不出来,都是null,但是test没有任何问题

@SpringBootApplication
public class SpringbootApplication {
    @Autowired
    private Program program;

    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
        SpringbootApplication springbootApplication = new SpringbootApplication();
        System.out.println(springbootApplication.program);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值