Файл пользовательских свойств Java Читайте целочисленные значения весной - PullRequest
0 голосов
/ 02 мая 2019

Привет, у меня есть свой собственный файл properties, но когда я хочу получить значение order, в это время он возвращает 0, как я могу прочитать целочисленное значение в классе пользовательских свойств пружины

@Component 
@PropertySource("classpath:my.properties")
@ConfigurationProperties 
public class MyProperties {

    private Integer order;
* 1006?*my.properties такой файл
enableAll=true
order=1
myvalue=ABC

1 Ответ

0 голосов
/ 02 мая 2019

Я думаю, что вам не хватает геттеров и сеттеров.

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

@Component
@PropertySource("classpath:my.properties")
@ConfigurationProperties
public class MyProperties {
    private boolean enableAll;
    private Integer order;
    private String myvalue;


    public boolean isEnableAll() {
        return enableAll;
    }

    public void setEnableAll(boolean enableAll) {
        this.enableAll = enableAll;
    }

    public Integer getOrder() {
        return order;
    }

    public void setOrder(Integer order) {
        this.order = order;
    }

    public String getMyvalue() {
        return myvalue;
    }

    public void setMyvalue(String myvalue) {
        this.myvalue = myvalue;
    }
}

При этой настройке следующий тест успешно выполняется

import com.example.demo.gq.MyProperties;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MyPropertiesTests {

    @Autowired
    MyProperties myProperties;

    @Test
    public void myProps() {
        assertThat(myProperties.getOrder()).isEqualTo(1);
        assertThat(myProperties.isEnableAll()).isTrue();
        assertThat(myProperties.getMyvalue()).isEqualTo("ABC");
    }

}
...