PayModel
package com.yymt.communityservices.property.order.model;
import com.yymt.communityservices.common.config.YamlConfigFactory;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.io.Serializable;
@Data
@PropertySource(value = "classpath:payProperty.yml", factory = YamlConfigFactory.class)
@ConfigurationProperties(prefix = "alipay")
@Component("payModel")
public class PayModel implements Serializable {
private String serverUrl;
private String appId;
private String privateKey;
private String alipayPublicKey;
private String format;
private String charset;
private String signType;
private String productCode;
private long timeout;
private String returnUrl;
private String notifyUrl;
private String quitUrl;
}
YamlConfigFactory
package com.yymt.communityservices.common.config;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.DefaultPropertySourceFactory;
import org.springframework.core.io.support.EncodedResource;
import java.io.IOException;
import java.util.Properties;
public class YamlConfigFactory extends DefaultPropertySourceFactory {
@Override
public PropertySource> createPropertySource(String name, EncodedResource resource) throws IOException {
String sourceName = name != null ? name : resource.getResource().getFilename();
if (!resource.getResource().exists()) {
return new PropertiesPropertySource(sourceName, new Properties());
} else if (sourceName.endsWith(".yml") || sourceName.endsWith(".yaml")) {
Properties propertiesFromYaml = loadYml(resource);
return new PropertiesPropertySource(sourceName, propertiesFromYaml);
} else {
return super.createPropertySource(name, resource);
}
}
private Properties loadYml(EncodedResource resource) throws IOException {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
factory.afterPropertiesSet();
return factory.getObject();
}
}