軟件藝術(shù)思考者  
          混沌,彷徨,立志,蓄勢(shì)...
          公告
          日歷
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導(dǎo)航

          隨筆分類(lèi)(86)

          隨筆檔案(85)

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

           

          Spring Framework最得以出名的是與Hibernate的無(wú)縫鏈接,基本上用Spring,就會(huì)用Hibernate??上У氖荢pring提供的HibernateTemplate功能顯得不夠,使用起來(lái)也不是很方便。我們編程序時(shí),一般先寫(xiě)B(tài)usinessService,由BusinessService調(diào)DAO來(lái)執(zhí)行存儲(chǔ),在這方面Spring沒(méi)有很好的例子,造成真正想用好它,并不容易。

          我們的思路是先寫(xiě)一個(gè)BaseDao,仿照HibernateTemplate,將基本功能全部實(shí)現(xiàn):

          public class BaseDao extends HibernateDaoSupport{

          ??? private Log log = LogFactory.getLog(getClass());

          ??? public Session openSession() {
          ??????? return SessionFactoryUtils.getSession(getSessionFactory(), false);
          ??? }

          ??? public Object get(Class entityClass, Serializable id) throws DataAccessException {
          ??????? Session session = openSession();
          ??????? try {
          ??????????? return session.get(entityClass, id);
          ??????? }
          ??????? catch (HibernateException ex) {
          ??????????? throw SessionFactoryUtils.convertHibernateAccessException(ex);
          ??????? }
          ??? }

          ??? public Serializable create(Object entity) throws DataAccessException {
          ??????? Session session = openSession();
          ??????? try {
          ??????????? return session.save(entity);
          ??????? }
          ??????? catch (HibernateException ex) {
          ??????????? throw SessionFactoryUtils.convertHibernateAccessException(ex);
          ??????? }
          ??? }

          ...

          其它的DAO,從BaseDao繼承出來(lái),這樣寫(xiě)其他的DAO,代碼就會(huì)很少。

          從BaseDao繼承出來(lái)EntityDao,專(zhuān)門(mén)負(fù)責(zé)一般實(shí)體的基本操作,會(huì)更方便。

          public interface EntityDao {

          ??? public Object get(Class entityClass, Serializable id) throws DataAccessException;

          ??? public Object load(Class entityClass, Serializable id) throws DataAccessException;

          ??? public Serializable create(Object entity) throws DataAccessException;
          ...}

          /**
          ?* Base class for Hibernate DAOs.? This class defines common CRUD methods for
          ?* child classes to inherit. User Sping AOP Inteceptor
          ?*/
          public class EntityDaoImpl extends BaseDao implements EntityDao{

          }

          為了Transaction的控制,采用AOP的方式:

          public interface EntityManager {

          ??? public Object get(Class entityClass, Serializable id);

          ??? public Object load(Class entityClass, Serializable id);

          ??? public Serializable create(Object entity);
          ...

          }

          /**
          ?* Base class for Entity Service. User Sping AOP Inteceptor
          ?*/
          public class EntityManagerImpl implements EntityManager {

          ??? private EntityDao entityDao;

          ??? public void setEntityDao(EntityDao entityDao) {
          ??????? this.entityDao = entityDao;
          ??? }

          ??? public Object get(Class entityClass, Serializable id) {
          ??????? return entityDao.get(entityClass, id);
          ??? }

          ??? public Object load(Class entityClass, Serializable id) {
          ??????? return entityDao.load(entityClass, id);
          ??? }
          ...

          }

          這樣我們就有了一個(gè)通用的Hibernate實(shí)體引擎,可以對(duì)任何Hibernate實(shí)體實(shí)現(xiàn)基本的增加、修改、刪除、查詢(xún)等。

          其它的BusinessService就可以繼承EntityManager,快速實(shí)現(xiàn)業(yè)務(wù)邏輯。

          具體XML配置如下:

          ?<!-- Oracle JNDI DataSource for J2EE environments -->
          ?<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
          ??<property name="jndiName"><value>java:comp/env/jdbc/testPool</value></property>
          ?</bean>

          ?<!-- Hibernate SessionFactory for Oracle -->
          ?<!-- Choose the dialect that matches your "dataSource" definition -->
          ?<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
          ??<property name="dataSource"><ref local="dataSource"/></property>
          ??<property name="mappingResources">
          ???<value>user-hbm.xml</value>
          ??</property>
          ??<property name="hibernateProperties">
          ???<props>
          ????<prop key="hibernate.dialect">net.sf.hibernate.dialect.OracleDialect</prop>
          ????<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.Provider</prop>
          ????<prop key="hibernate.cache.use_query_cache">true</prop>
          ????????????????? <prop key="hibernate.show_sql">false</prop>
          ???</props>
          ??</property>
          ?</bean>

          ?<!-- AOP DAO Intecepter -->
          ??????? <bean id="hibernateInterceptor" class="org.springframework.orm.hibernate.HibernateInterceptor">
          ????????? <property name="sessionFactory">
          ??????????? <ref bean="sessionFactory"/>
          ????????? </property>
          ??????? </bean>

          ??????? <bean id="entityDaoTarget" class="com.gpower.services.entity.dao.EntityDaoImpl">
          ????????? <property name="sessionFactory">
          ??????????? <ref bean="sessionFactory"/>
          ????????? </property>
          ??????? </bean>

          ??????? <bean id="entityDao" class="org.springframework.aop.framework.ProxyFactoryBean">
          ????????? <property name="proxyInterfaces">
          ??????????? <value>com.gpower.services.entity.dao.EntityDao</value>
          ????????? </property>
          ????????? <property name="interceptorNames">
          ??????????? <list>
          ????????????? <value>hibernateInterceptor</value>
          ????????????? <value>entityDaoTarget</value>
          ??????????? </list>
          ????????? </property>
          ??????? </bean>

          ?<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
          ?<bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
          ??<property name="sessionFactory"><ref local="sessionFactory"/></property>
          ?</bean>

          ?<!-- Transaction manager that delegates to JTA (for a transactional JNDI DataSource) -->
          ?<!--
          ?<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>
          ?-->

          ?<!-- Transactional proxy for the Application primary business object -->
          ??????? <bean id="entityManagerTarget" class="com.gpower.services.entity.EntityManagerImpl">
          ????????? <property name="entityDao">
          ??????????? <ref bean="entityDao"/>
          ????????? </property>
          ??????? </bean>

          ??????? <bean id="entityManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
          ????????? <property name="transactionManager">
          ??????????? <ref bean="transactionManager"/>
          ????????? </property>
          ????????? <property name="target">
          ??????????? <ref bean="entityManagerTarget"/>
          ????????? </property>
          ????????? <property name="transactionAttributes">
          ???? <props>
          ?????? <prop key="get*">PROPAGATION_SUPPORTS</prop>
          ?????? <prop key="*">PROPAGATION_REQUIRED</prop>
          ???? </props>
          ????????? </property>
          ??????? </bean>


          Spring Framework之最佳實(shí)踐二
          posted on 2006-07-05 16:38 智者無(wú)疆 閱讀(1653) 評(píng)論(2)  編輯  收藏 所屬分類(lèi): about spring
          評(píng)論:
          • # re: Spring Framework之最佳實(shí)踐二   白白 Posted @ 2006-07-05 16:54
            我發(fā)現(xiàn)javascript源碼大全挺好,里面的代碼都是正確的,我試了幾個(gè)都可以,呵呵,老公,你真聰明??!  回復(fù)  更多評(píng)論   

          • # re: Spring Framework之最佳實(shí)踐二   白白 Posted @ 2006-07-10 15:12
            我時(shí)刻關(guān)注著你的博客,點(diǎn)擊率還蠻高的嘛。比我們公司的網(wǎng)站還要厲害?。?!呵呵,我喜歡啦。  回復(fù)  更多評(píng)論   

           
          Copyright © 智者無(wú)疆 Powered by: 博客園 模板提供:滬江博客


             觀(guān)音菩薩贊

          主站蜘蛛池模板: 铜陵市| 清镇市| 千阳县| 合山市| 房山区| 大厂| 昂仁县| 南郑县| 陆河县| 安溪县| 察隅县| 东丽区| 湖南省| 玛纳斯县| 蓝山县| 措美县| 宝山区| 工布江达县| 建德市| 寻乌县| 湖口县| 嘉定区| 辉南县| 栖霞市| 湖南省| 昆山市| 前郭尔| 张北县| 阜平县| 手机| 大邑县| 仙居县| 江川县| 萝北县| 青神县| 中西区| 甘洛县| 淮安市| 衡东县| 弥渡县| 阿尔山市|