一、yml配置文件
在yam文件中配置自定义的标签
1.在yml配置文件中加入
through:
url: http://10.4.2.140:49003/IBSThrough
2.测试类进行测试
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
//这样就可以引用配置文件中的
@Configuration
@EnableConfigurationProperties(HttpTransportProperties.class)//这个是自己写的配置类,如果是多个,就可以自己写个配置类进行配置
pulic class Test(){
@Value("${through.url}")
private String newTransportUrl;
//再写个get()set()方法
public String getNewTransportURL() {
return newTransportURL;
}
public void setNewTransportURL(String newTransportURL) {
this.newTransportURL = newTransportURL;
}
}
二、读取properties中的值
1.读取properties配置文件中的值
放到Resource目录下
config.properties
sendUrl=http//:www.wiliam.com
2.测试类进行测试
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.io.InputStream;
import java.util.Properties;
pulic class Test(){
InputStream resourceAsStream = HttpESBPacketformerTransportConfiguration.class.getClassLoader().getResourceAsStream("config.properties");
Properties properties = new Properties();
properties.load(resourceAsStream);
String throughUrl = properties.getProperty("sendUrl");
resourceAsStream.close();
}