繁体   English   中英

从Spring属性文件中读取值

[英]Reading value from Spring Property 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);
}

这是在我的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 

这是我遇到的错误

该声明:

TestToCheckProperties testToCheckProperties = 
    context.getBean(TestToCheckProperties);

是无效的。 它应该是:

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

暂无
暂无

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

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