Spring中如何从属性文件中获取值

时间:2017-09-16 07:18:09

标签: spring

我想获取属性文件的值,如下所示: - 属性文件的名称是 application.properties ,值如下: -

config.name=abcd
config.pwd=efgh

我将检索值的代码如下: -

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@PropertySource("classpath:application.properties")

    public class CustomerService {
    @Value("${config.name}")
    private String name;

    @Value("$(config.pwd)")
    private String data;

    System.out.println("name is ::" +name);
    System.out.println("data is ::" +data);
    }

但是当代码返回时,输出将以

的形式出现
name is ::ankita
data is ::$(config.pwd)

任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

尝试使用:@Value("${config.pwd}")