Encapsulate HibernateDaoSupport:
We inherit this class when using HibernateDaoSupport. However, we cannot rewrite the setSessionFactory and setHibernateTemplate methods in it. However, these two attributes are required by HibernateDaoSupport. This leads to the problem of how to inject these two attributes. Below we will use two methods to solve this problem to complete the use of hibernateDaoSupport:
1. inherit HibernateDaoSupport as the parent class and use the super set method to inject it during initialization:
A. Configure sessionFactory in the container:
[Html]
<! -- Inject the configured dataSource into SessionFactory -->
<Bean id = "sessionFactory" class = "org. springframework. orm. hibernate3.LocalSessionFactoryBean">
<Property name = "dataSource" ref = "dataSource"/>
<Property name = "mappingResources">
<List>
<Value> com/spring/model/user. hbm. xml </value>
<Value> com/spring/model/userlog. hbm. xml </value>
</List>
</Property>
<Property name = "hibernateProperties">
<Value>
Hibernate. dialect = org. hibernate. dialect. MySQLDialect
Hibernate. show_ SQL = true
Hibernate. hbm2ddl. auto = create
</Value>
</Property>
</Bean>
B. Call the set Method of super to inject sessionFactory.
[Java]
@ Component ("userLogDaoImpl ")
Public class UserLogDaoImpl extends HibernateDaoSupport implements UserLogDao {
// The super keyword calls the parent class method injection.
@ Resource
Public void setSuperSessionFactory (SessionFactory sessionFactory ){
Super. setSessionFactory (sessionFactory );
}
@ Override
Public void save (UserLog l ){
This. getHibernateTemplate (). save (l );
}
}
Disadvantages:
The above method writes injection-related code in every DaoImpl. This is a lot of trouble. The second type
2. do not inherit the HibernateDaoSupport and create a parent class to inject the hibernateTemplate as an attribute.
A. configure A hibernateTemplate in the container:
[Html]
<! -- Inject the configured dataSource into SessionFactory -->
<Bean id = "sessionFactory" class = "org. springframework. orm. hibernate3.LocalSessionFactoryBean">
<Property name = "dataSource" ref = "dataSource"/>
<Property name = "mappingResources">
<List>
<Value> com/spring/model/user. hbm. xml </value>
<Value> com/spring/model/userlog. hbm. xml </value>
</List>
</Property>
<Property name = "hibernateProperties">
<Value>
Hibernate. dialect = org. hibernate. dialect. MySQLDialect
Hibernate. show_ SQL = true
Hibernate. hbm2ddl. auto = create
</Value>
</Property>
</Bean>
<! -- Configure hibernateTemplate -->
<Bean id = "hibernateTemplate" class = "org. springframework. orm. hibernate3.HibernateTemplate">
<Property name = "sessionFactory" ref = "sessionFactory"> </property>
</Bean>
B. Create a parent class to inject hibernateTemplate
[Java]
Public class SuperDaoSupport {
@ Resource
Private HibernateTemplate hibernateTemplate;
Public HibernateTemplate getHibernateTemplate (){
Return hibernateTemplate;
}
Public void setHibernateTemplate (HibernateTemplate hibernateTemplate ){
This. hibernateTemplate = hibernateTemplate;
}
}
C. inherit the parent class.
[Java]
@ Component ("userLogDaoImpl ")
Public class UserLogDaoImpl extends SuperDaoSupport implements UserLogDao {
@ Override
Public void save (UserLog l ){
This. getHibernateTemplate (). save (l );
}
}
Author: zhang6622056