简体   繁体   中英

Reading value from Spring Property file

Here is my code I'm trying to read " TestMe variable from My.properties file.

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:

<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:

#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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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