Spring使用注解读取properties文件内的参数

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

posted @ 2015-08-11 13:58  fanning  阅读(591)  评论(0编辑  收藏  举报