简体   繁体   English

从Spring属性文件中读取值

[英]Reading value from Spring Property file

Here is my code I'm trying to read " TestMe variable from My.properties file. 这是我试图从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);
}

This is in my Spring.XML I've created a bean for class TestToCheckProperties: 这是在我的Spring.XML中,我为类TestToCheckProperties创建了一个bean:

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

This is in my My.Property file and I'm trying to read this value in my class: 这在我的My.Property文件中,并且我正在尝试在我的课程中读取以下值:

#TestToCheckProperties
My.code.TestToCheckProperties=10 

Here is the error I'm getting 这是我遇到的错误

The statement: 该声明:

TestToCheckProperties testToCheckProperties = 
    context.getBean(TestToCheckProperties);

is invalid. 是无效的。 It should rather be: 它应该是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM