<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Spring Framework之最佳實踐二

    轉載自(http://www.gpowersoft.com/tech/Spring/46.htm)

    Spring Framework最得以出名的是與Hibernate的無縫鏈接,基本上用Spring,就會用Hibernate。可惜的是Spring提供的HibernateTemplate功能顯得不夠,使用起來也不是很方便。我們編程序時,一般先寫BusinessService,由BusinessService調DAO來執行存儲,在這方面Spring沒有很好的例子,造成真正想用好它,并不容易。

    我們的思路是先寫一個BaseDao,仿照HibernateTemplate,將基本功能全部實現:

    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繼承出來,這樣寫其他的DAO,代碼就會很少。

    從BaseDao繼承出來EntityDao,專門負責一般實體的基本操作,會更方便。

    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);
    ??? }
    ...

    }

    這樣我們就有了一個通用的Hibernate實體引擎,可以對任何Hibernate實體實現基本的增加、修改、刪除、查詢等。

    其它的BusinessService就可以繼承EntityManager,快速實現業務邏輯。

    具體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>



    posted on 2006-07-26 17:12 nbt 閱讀(350) 評論(0)  編輯  收藏 所屬分類: Spring框架

    <2006年7月>
    2526272829301
    2345678
    9101112131415
    16171819202122
    23242526272829
    303112345

    導航

    統計

    常用鏈接

    留言簿(3)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    相冊

    收藏夾

    Java技術網站

    友情鏈接

    國內一些開源網站

    最新隨筆

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲人精品午夜射精日韩 | 国产青草亚洲香蕉精品久久| 99精品国产成人a∨免费看| 国产亚洲精品不卡在线| 成人嫩草影院免费观看| 亚洲日韩在线观看| selaoban在线视频免费精品| 亚洲日韩在线第一页| 本免费AV无码专区一区| 国精无码欧精品亚洲一区| 免费无码一区二区三区蜜桃 | 国产在线a免费观看| 亚洲a∨无码男人的天堂| 成人激情免费视频| 色在线亚洲视频www| 韩国免费三片在线视频| 免费人成网上在线观看| 亚洲日本va午夜中文字幕久久| 国产午夜无码片免费| 亚洲国产成人久久精品动漫| 亚洲精品视频在线免费| 亚洲中文字幕久久精品无码A| 国产v精品成人免费视频400条| 中文字幕在线观看亚洲日韩| 毛片a级毛片免费播放100| 美女视频黄频a免费| 亚洲中文字幕无码日韩| 最近2019免费中文字幕视频三| 亚洲一区二区三区在线观看蜜桃| 好男人视频在线观看免费看片| 国产AV无码专区亚洲AV琪琪| 国产成人麻豆亚洲综合无码精品| 曰批全过程免费视频网址| 亚洲国产精品无码第一区二区三区 | 亚洲精品夜夜夜妓女网| 中文字幕免费在线看线人| 无码一区二区三区亚洲人妻| 亚洲最大av无码网址| 30岁的女人韩剧免费观看| 亚洲成a人片在线不卡一二三区 | 久久亚洲中文字幕精品一区|