Java 杂谈Springboot整合

@ConfigurationProperties注解的使用与@V

2018-10-11  本文已影响0人  垃圾简书_吃枣药丸
jianshu:
 id: futaosmile

# 1. 使用@ConfigurationProperties注解注入

@RunWith(SpringRunner.class)
@SpringBootTest
@ConfigurationProperties(prefix = "jianshu")
public class SpringmvcdemoApplicationTests {

    private String id;

    @Test
    public void test10() {
        System.out.println(id);
    }
}
package com.futao.springmvcdemo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
@ConfigurationProperties(prefix = "jianshu")
public class SpringmvcdemoApplicationTests {

    private String id;

    @Test
    public void test10() {
        System.out.println(id);
    }

    public void setId(String id) {
        this.id = id;
    }
}

# 2. 使用@Value注解

package com.futao.springmvcdemo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

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

    @Value("${jianshu.id}")
    private String id;

    @Test
    public void test10() {
        System.out.println(id);
    }

}

OK

上一篇下一篇

猜你喜欢

热点阅读