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

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-28 19:39   127   0

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

转载于:https://www.cnblogs.com/fanning/p/4720832.html

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP