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

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

报告相同问题?

悬赏问题

  • ¥40 如果update 一个列名为参数的value
  • ¥15 基于51单片机的水位检测系统设计中LCD1602一直不显示
  • ¥15 OCS2安装出现问题,请大家给点意见
  • ¥15 ros小车启动launch文件报错
  • ¥15 vs2015到期想登陆但是登陆不上
  • ¥15 IPQ5018制作烧录固件,boot运行失败(操作系统-linux)(相关搜索:操作系统)(相关搜索:操作系统)
  • ¥20 icefall在librispeech基础上加入个人数据集
  • ¥30 keepalive高可用故障运维配置询问
  • ¥15 求帮助!国家电网内网u盘突然识别不出来了。
  • ¥15 matlab语音变速变调同时实现