weixin_42358817 2010-03-26 10:42
浏览 245
已采纳

Hibernate+spring

我想将hibernateDaoSupport抽象类写成接口的形式,这样我用它时就可以采用注入的方式,下面是我改写的代码:
[color=red]public interface HispUtil[/color] {

public void afterPropertiesSet();

public void checkDaoConfig();

public void initDao() throws Exception ;

public void setSessionFactory(SessionFactory sessionFactory) ;

public HibernateTemplate createHibernateTemplate(SessionFactory sessionFactory);

public SessionFactory getSessionFactory() ;

public Session getSession();

public Session getSession(boolean allowCreate);

public DataAccessException convertHibernateAccessException(HibernateException ex);

public void releaseSession(Session session);
public HibernateTemplate getHibernateTemplate() ;

public void setHibernateTemplate(HibernateTemplate hibernateTemplate) ;
}

[color=red]public class HispUtilImpl implements HispUtil[/color]{
private final Log logger = LogFactory.getLog(getClass());
private HibernateTemplate hibernateTemplate;
@Override
[color=brown]public void afterPropertiesSet() [/color]throws IllegalArgumentException, BeanInitializationException{
checkDaoConfig();
try {
// Let concrete implementations initialize themselves.
initDao();
}
catch (Exception ex) {
throw new BeanInitializationException("Initialization of DAO failed", ex);
}

}

@Override
[color=brown]public void checkDaoConfig() throws IllegalArgumentException[/color] {
if (this.hibernateTemplate == null) {
throw new IllegalArgumentException("'sessionFactory' or 'hibernateTemplate' is required");
}

}

@Override
public DataAccessException convertHibernateAccessException(
        HibernateException ex) {
    return this.hibernateTemplate.convertHibernateAccessException(ex);
}

@Override
[color=brown]public HibernateTemplate createHibernateTemplate/color {
return new HibernateTemplate(sessionFactory);
}
@Override
[color=orange]public Session getSession() throws DataAccessResourceFailureException, IllegalStateException [/color]{

return getSession(this.hibernateTemplate.isAllowCreate());
}

@Override
[color=brown]public Session getSession(boolean allowCreate) throws DataAccessResourceFailureException, IllegalStateException[/color] {
return (!allowCreate ?
SessionFactoryUtils.getSession(getSessionFactory(), false) :
SessionFactoryUtils.getSession(
getSessionFactory(),
this.hibernateTemplate.getEntityInterceptor(), this.hibernateTemplate.getJdbcExceptionTranslator()));
}
@Override
[color=brown]public SessionFactory getSessionFactory() [/color]{
return (this.hibernateTemplate != null ? this.hibernateTemplate.getSessionFactory() : null);
}

@Override
[color=brown]public void initDao() throws Exception[/color]{

}

@Override
[color=brown]public void releaseSession(Session session)[/color] {
SessionFactoryUtils.releaseSession(session, getSessionFactory());

}

@Override
[color=brown]public void setSessionFactory(SessionFactory sessionFactory)[/color] {
if (this.hibernateTemplate == null || sessionFactory != this.hibernateTemplate.getSessionFactory()) {
this.hibernateTemplate = createHibernateTemplate(sessionFactory);
}

}

[color=brown]public HibernateTemplate getHibernateTemplate()[/color] {
return hibernateTemplate;
[color=brown]}
public void setHibernateTemplate(HibernateTemplate hibernateTemplate)[/color] {
this.hibernateTemplate = hibernateTemplate;
}

}

持久层:
public class StudentDAOImpl implements StudentDAO {
private HispUtil hispUtil;
@Override
public void saveStudent(StudentInfor studentInfor) {
System.out.println("测试4"+this.hispUtil);
this.hispUtil.getHibernateTemplate().save(studentInfor);
}
public HispUtil getHispUtil() {
return hispUtil;
}
public void setHispUtil(HispUtil hispUtil) {
this.hispUtil = hispUtil;
}
}

我的配置文件:











com.sphitwo.bean.StudentInfor




org.hibernate.dialect.MySQLDialect
true
update


















我的测试方法:
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentController studentCtrl=(StudentController) ctx.getBean("strudentCtrl");
StudentInfor student=new StudentInfor();
student.setStudentName("jinwan");
student.setStudentNo("6789990");
studentCtrl.saveStudent(student);

}

报错:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentService' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'studentDAO' of bean class [com.sphitwo.service.impl.StudentInforServiceImpl]: Bean property 'studentDAO' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'studentDAO' of bean class [com.sphitwo.service.impl.StudentInforServiceImpl]: Bean property 'studentDAO' 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:793)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:645)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:78)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:59)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1127)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:862)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:423)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:249)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:155)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:246)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:291)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:122)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:66)
at com.hibernatespring.main.MainTest.main(MainTest.java:15)

我现在还是刚开始琢磨,我也不知道是不是可以这样做,请各位给点意见!

  • 写回答

1条回答 默认 最新

  • wanghaolovezlq 2010-03-26 10:46
    关注

    Bean property 'studentDAO' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

    不可写,或没有相应的set方法



    这个bean的问题

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。