我想将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)
我现在还是刚开始琢磨,我也不知道是不是可以这样做,请各位给点意见!