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的问题

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

报告相同问题?

悬赏问题

  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA