1.
在spring的配置文件中,配置好需要读取的properties文件
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> <property name="fileEncoding" value="UTF-8" /> <property name="locations"> <list> <value>classpath:jdbc.properties</value> <value>classpath:config.properties</value> </list> </property> </bean>
2.
config.properties中的参数:
pay.url=http://www.test.com/pay.htm
3.
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class Configuration { @Value(value = "${pay.url}") private String payUrl; public String getPayUrl() { return payUrl; } public void setPayUrl(String payUrl) { this.payUrl = payUrl; } }
@Value(value = "${pay.url:123}") 代表pay.url默认值为123
时间: 2024-10-06 18:48:54