配置文件中设置信息,格式如下
wechat:
appId: abcdefg
appSecret: 12345678
多级
project:
url:
get: http://localhost
获取配置文件信息
1、@ConfigurationProperties
package com.xiong.sell.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "wechat")
public class WechatAccountConfig {
private String appId;
private String appSecret;
}
2、@Value
package com.xiong.sell.config;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@Data
public class ProjectUrlConfig {
@Value("${project.url.get}")
private String sell;
}
3、Environment
package com.xiong.sell.config;
import lombok.extern.slf4j.Slf4j;
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.core.env.Environment;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Slf4j
public class ConfigTest {
@Autowired
private Environment environment;
@Test
public void test2(){
String get = environment.getProperty("project.url.get");
log.info("project.url.get = {}",get);
}
}