SpringBoot属性赋值的三种方式

方式一:在属性字段上增加@Value注解并赋值

package com.zhang;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Dog {
    @Value("小黑")
    private String name;
    @Value("1")
    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;
    }
}

测试类

package com.zhang;

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

@SpringBootTest
class SpringbootApplicationTests {
	@Autowired
	private  Dog dog;
	@Test
	void contextLoads() {
		System.out.println(dog.getName()+"-----"+dog.getAge());
	}
}

方式二:在yml中赋值

注意:yml中每个冒号后面必须要有空格,否则配置不生效。
ConfigurationProperties注解将yml中相关配置和本类的所有属性,
进行绑定@ConfigurationProperties(prefix = “person”)。

person:
  name: zhangsanfeng
  age: 3
  happy: true
  maps: {key1: 'value1', key2: 'value2'}
  lists:
    - code
    - music
    - gril
  dog:
    name: 小二黑
    age: 1
package com.zhang;

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

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

@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private  String name;
    private  String age;
    private  Boolean happy;
    private  List<Object> lists;
    private  Map<String,Object> maps;
    private  Dog dog;

    public Person() {
    }

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

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

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

    public Boolean getHappy() {
        return happy;
    }

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

    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;
    }

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

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

方式三:通过配置文件application.properties进行赋值

name=zhangsanfeng

PropertySource指定加载的配置文件,通过EL表达式"${name}"或者@Value(“李四光砸缸”)进行赋值

package com.zhang;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

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

@Component
//@ConfigurationProperties(prefix = "person")
@PropertySource(value = "classpath:application.properties")
public class Person {

    @Value("${name}")
    //@Value("李四光砸缸")
    private  String name;
    private  String age;
    private  Boolean happy;
    private  List<Object> lists;
    private  Map<String,Object> maps;
    private  Dog dog;

    public Person() {
    }

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

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

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

    public Boolean getHappy() {
        return happy;
    }

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

    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;
    }

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

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

参考文章
https://www.cnblogs.com/neaos/p/10790528.html
https://blog.csdn.net/weixin_43075247/article/details/105415572

上一篇:字符串转换注意编码


下一篇:ETL之PDI/Kettle培训实战教程-57个案例(数据迁移、抽取同步、转换加载)