近几日搭建框架,遇到sessionFactory报错,在Spring配置文件中向dao层中注入时报的错:
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'sessionFactory' of bean class [com.first.web.dao.LoginDaoImpl]: Bean property 'sessionFactory' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1064)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:924)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:76)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1393)
... 27 more
起初各种百度,有的说是Spring配置文件中扫描包配置出现的问题,有的说是把Spring配置文件中注入sessionFactory那句话去掉(可以尝试一下,去掉,然后用注释的方法注入SessionFactory),都试了试,发现是自己在dao层实现类中,SessionFactory的命名规范问题,写成了:
private SessionFactroy sf
没有与Spring配置文件中的名称保持一致。
总结:
1,注解时,必须配置好扫描包的设置,只有Spring扫描到了 类似@resource @component的时候才会去创建bean。
2,细心。