从Spring Property文件中读取值

时间:2014-01-29 14:44:55

标签: java spring properties

这是我的代码我试图从My.properties文件中读取“TestMe变量。

public class TestToCheckProperties {

    private int testMe;

      public void setTestMe(int testMe) {
    this.testMe = testMe;
        }

    public static void main(String args[]){
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/META-INF/spring/bean/dfp-web-beans.xml");
        TestToCheckProperties testToCheckProperties=context.getBean(TestToCheckProperties);
        System.out.println("testMe: "+testToCheckProperties.testMe);
}

这是我的Spring.XML 我为类TestToCheckProperties创建了一个bean:

<bean id='TestToCheckProperties' class="com.My.Code.TestToCheckProperties"  >
        <property name="testMe" value="${My.code.TestToCheckProperties}" />
    </bean>

这是在我的My.Property文件中,我正试图在我的班级中读取这个值:

#TestToCheckProperties
My.code.TestToCheckProperties=10 

这是我得到的错误

1 个答案:

答案 0 :(得分:0)

声明:

TestToCheckProperties testToCheckProperties = 
    context.getBean(TestToCheckProperties);

无效。它应该是:

TestToCheckProperties testToCheckProperties = 
    (TestToCheckProperties) context.getBean("TestToCheckProperties");