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

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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料