【问题标题】:Reading value from Spring Property file从 Spring 属性文件中读取值
【发布时间】:2014-01-29 14:44:55
【问题描述】:

这是我试图从 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 

这是我遇到的错误

【问题讨论】:

  • 您在写完问题之前发帖了吗?你的错误在哪里?发布其余的上下文配置。

标签: java spring properties


【解决方案1】:

声明:

TestToCheckProperties testToCheckProperties = 
    context.getBean(TestToCheckProperties);

无效。应该是:

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

【讨论】:

    猜你喜欢
    • 2020-07-01
    • 2015-12-23
    • 2018-05-24
    • 2023-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    相关资源
    最近更新 更多