读取prop文件bean错误

时间:2015-02-09 10:17:15

标签: java xml spring

Hy先生,我正在尝试阅读一个属性文件并在屏幕上显示它,但是我的bean filE有问题

这是我的bean文件:

<context:property-placeholder location="classpath:fisier.properties." /> 

<bean id="fisier.properties" class="projectt.proiect.App">
    <property name="mesaj" value="${Hello}" />
    </bean>

</beans>

我的课程是这样的:

public class SecondClass {

    private String mesaj;

     public String getMesaj() {
            return mesaj;
        }

     public void setMesaj(String Mesaj) {
            this.mesaj = mesaj;
        }

     @Override
        public String toString(){
            return mesaj;
        }
}

主要:

public class App 
{
    public static void main( String[] args )
    {

        String confFile = "bean.xml";

          ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(confFile);

          SecondClass secClass = (SecondClass) context.getBean("mesaj");
          System.out.println(secClass.toString());
          context.close();

}
}

我在bean文件中收到以下错误: 属性:名称 遵循JavaBean命名约定的属性名称。

数据类型:字符串

有人可以帮我这个吗?

2 个答案:

答案 0 :(得分:0)

设置属性时,必须使用该属性放置类:

<bean id="fisier.properties" class="SecondClass">
    <property name="mesaj" value="${Hello}" />
</bean>

答案 1 :(得分:0)

首先,您的bean类型不正确:

<context:property-placeholder location="classpath:fisier.properties." /> 

<bean id="fisier.properties" class="projectt.proiect.SecondClass">
    <property name="mesaj" value="${Hello}" />
    </bean>

</beans>

其次,getBean()在上下文中,获取bean的id或类型:

    public class App 
    {
        public static void main( String[] args )
        {

            String confFile = "bean.xml";

              ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(confFile);

              SecondClass secClass = (SecondClass) context.getBean(projectt.proiect.SecondClass.class);
//or               SecondClass secClass = (SecondClass) context.getBean("fisier.properties");

              System.out.println(secClass.toString());
              context.close();

    }
    }

Thrid,您的setMesaj()不正确:

public class SecondClass
{
  private String mesaj;

  public String getMesaj()
  {
    return mesaj;
  }

  public void setMesaj(String mesaj)
  {
    this.mesaj = mesaj;
  }

}

你想用que值$ {Hello}做什么? 你想将mesaj属性设置为&#34; Hello&#34;?