当前位置: 代码迷 >> J2EE >> Caused by: org.springframework.beans.InvalidPropertyException:
  详细解决方案

Caused by: org.springframework.beans.InvalidPropertyException:

热度:304   发布时间:2016-04-22 03:16:41.0
一个超级简单的Spring注入的问题,为什么说找不到property? 内详...
我有一个类:
Java code
public class HPServiceImpl implements HPService{    private HPDao dao;        public void setHPDao(HPDao dao)    {        this.dao = dao;    }        @Override    public int getHp()    {        return dao.getHp();    }}


它的Bean定义:
XML code
    <bean id="hpService" class="org.mycompany.spring.aop.service.impl.HPServiceImpl">              <property name="dao">            <ref bean="hpDao" />        </property>    </bean>


// -----------------------------------------------------------

引用了hpDao的Bean:
XML code
     <bean id="hpDao" class="org.mycompany.spring.aop.dao.impl.HPDaoImpl"/>


hpDao的实际类是:
Java code
public class HPDaoImpl implements HPDao{    @Override    public int getHp()    {        return (int)(Math.random() * 100);    }    }


// -----------------------------------------------------------

我用一个main去调用它:
Java code
public class Main{    public static void main(String[] args)    {        ApplicationContext context= new ClassPathXmlApplicationContext("classpath*:spring/application-*.xml");                    (HPService)context.getBean("hpService");    }}


运行抛出了异常:
Java code
Caused by: org.springframework.beans.InvalidPropertyException: Invalid property 'dao' of bean class [org.mycompany.spring.aop.service.impl.HPServiceImpl]: No property 'dao' found


晕...说找不到'dao'. 我不是定义了吗: private HPDao dao;


------解决方案--------------------
public void setHPDao(HPDao dao)
{
this.dao = dao;
}
<property name="hPDao">
<ref bean="hpDao" />
</property>


setter方法名,去掉前面三个字符,然后第四个字符转成小写。
  相关解决方案