在java代码中读取file.properties值

在java代码中读取file.properties值,java,spring,properties,Java,Spring,Properties,我有一个属性文件名documentum,其中包含以下内容 #test dfs.repositoryName = CUDO dfs.contextRoot = http://13.209.9.28:9080/services dfs.username = 1234 dfs.password = fx8888 dfs.moduleName = core 我已经在my beans.xml中配置了这个文件 <bean id="propertyConfigurer" class="

我有一个属性文件名documentum,其中包含以下内容

#test
dfs.repositoryName = CUDO
dfs.contextRoot = http://13.209.9.28:9080/services
dfs.username = 1234
dfs.password = fx8888
dfs.moduleName = core
我已经在my beans.xml中配置了这个文件

<bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="false" />
        <property name="locations">
            <list>
                <value>file:${app.home}/documentum.properties</value>
            </list>
        </property>
    </bean>
如果我在同一个类中使用@value注释,那么它可以正常工作,但是如果我像这样作为一个单独的类使用

package au.com.fxa.sfdc.custdocs.util;

import org.springframework.beans.factory.annotation.Value;

public class DocumentumConfigUtil {


    @Value("${dfs.repositoryName}")
    private String repositoryName;

    @Value("${dfs.contextRoot}")
    private String contextRoot;

    @Value("${dfs.username}")
    private String username;

    @Value("${dfs.password}")
    private String password;

    @Value("${dfs.module}")
    private String module;

    public String getRepositoryName() {
        return repositoryName;
    }

    public String getContextRoot() {
        return contextRoot;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String getModule() {
        return module;
    }

}

然后创建该类的对象,并尝试通过getter方法访问它,因为它给我空值。

使用
@value
注释

定义一个类属性,如

@Value("${dfs.repositoryName}")
String repository

正如我所看到的,您谈论的是spring,而不是Web服务。如果我是对的,请查找@Value注释。是的,很抱歉使用apache cxf和spring框架创建web服务。
@Value
类似于
@Autowired
将仅在spring托管bean上工作。如果您自己创建一个实例,spring将对该bean一无所知,从而忽略
@Value
。谢谢Jens。如果我在同一个类中定义,那么它是有效的。但是,如果我在另一个类中定义属性,并通过getter尝试访问这些属性,则返回null。@Adeel编辑您的问题,添加标题更新并在那里共享,或者您是否有github repo?@Adeel此类不是spring bean,因此属性的注入不起作用。将
@Configuration
注释添加到classI中我添加了@Configuration并得到异常,表示org.springframework.beans.factory.BeanCreationException:无法自动关联字段:(@Adeel哪个字段?
@Value("${dfs.repositoryName}")
String repository