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

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

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

    一葉笑天
    雄關漫道真如鐵, 而今邁步從頭越。 從頭越, 蒼山如海, 殘陽如血。
    posts - 73,comments - 7,trackbacks - 0
    原文地址:http://www.wajava.com/bbs/viewthread_thread,51

    1、在web.xml中加載Spring的配置文件
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:spring/*.xml</param-value>
    </context-param>

    <!--Spring ApplicationContext 載入 -->
    <listener>
    <listener-class>
    org.springframework.web.context.ContextLoaderListener
    </listener-class>
    </listener>


    2、配置屬性文件
    <!-- 屬性文件讀入 -->
    <bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
    <list>
    <value>classpath*:config/ct-db-c3p0.properties</value>
    <value>classpath*:config/mail.properties</value>
    </list>
    </property>
    </bean>

    3、配置數(shù)據(jù)源
    <!-- 數(shù)據(jù)源定義,使用Apache c3p0 連接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="${....common.dbutils.c3p0.driver}" />
    <property name="jdbcUrl" value="${....common.dbutils.c3p0.url}" />
    <property name="user" value="${....common.dbutils.c3p0.user}" />
    <property name="password" value="${....common.dbutils.c3p0.password}" />
    <property name="maxStatementsPerConnection" value="${....common.dbutils.c3p0.maxStatementsPerConnection}"/>
    <property name="checkoutTimeout" value="${....common.dbutils.c3p0.checkoutTimeout}"/>
    <property name="initialPoolSize" value="${....common.dbutils.c3p0.initialPoolSize}"/>
    <property name="minPoolSize" value="${....common.dbutils.c3p0.minPoolSize}"/>
    <property name="maxPoolSize" value="${....common.dbutils.c3p0.maxPoolSize}"/>
    <property name="maxStatements" value="${....common.dbutils.c3p0.maxStatements}"/>
    <property name="acquireIncrement" value="${....common.dbutils.c3p0.acquireIncrement}"/>
    <property name="maxIdleTime" value="${....common.dbutils.c3p0.maxIdleTime}"/>
    </bean>

    <!--Hibernate SessionFatory-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="annotatedClasses">
    <list>
    <!--添加bean -->
    <value>....bean.AddressBook</value>
    .......
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
    <prop key="hibernate.show_sql">false</prop>
    <prop key="hibernate.generate_statistics">false</prop>
    <prop key="hibernate.connection.release_mode">auto</prop>
    <prop key="hibernate.autoReconnect">true</prop>
    <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
    <prop key="hibernate.cache.use_query_cache">true</prop>
    </props>
    </property>
    </bean>

    <!--Hibernate TransactionManager-->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
    </bean>


    4、依賴注入配置
    <bean id="companyAction" class="....action.CompanyAction" scope="request"></bean>
    <bean name="companyBaseModel" class="....CompanyBaseModel"></bean>
    <bean class="....CompanyService" id="companyService"></bean>
    <bean class="....CompanyDAO" id="companyDAO"></bean>
    注:對beans的設置
    <beans default-autowire="byName" default-lazy-init="true">...</beans>



    5、Spring怎樣注入Class<T>(泛型類型對象)?
    在事務配置時,將proxy-target-class設置為false即可。不能為true,因為要針對接口代理。
    如:<aop:config proxy-target-class="false">

    6、Spring中采用構造方法注入注意要點:
    在配置文件中顯式書寫注入的參數(shù)。如:
    <bean class="....impl.JobService"
    id="retJobService">
    <constructor-arg ref="retJobDao" />
    </bean>
    多個參數(shù)的構造函數(shù)示例
    <bean class="....impl.TypeService"
    id="typeService">
    <constructor-arg index="0" ref="typeDAO" />
    <constructor-arg index="1" type="boolean">
    <value>false</value>
    </constructor-arg>
    </bean>


    7、Spring 2.0 結合AspectJ pointcut語法配置AOP詳解
    Spring參考文檔 7.3 chema-based AOP support 提供了aspect,advisor,advide三種組裝方法的解釋,其中aspect是aspectJ原裝,但稍復雜,
    <aop:config proxy-target-class="true">
    <aop:advisor pointcut="execution(* *..BookManager.save(..))||execution(* *..BookManager.remove(..))" advice-ref="lowStockBookFlushingAdvice"/>
    <aop:advisor pointcut="execution(* *..BookStockChecker.getLowStockBooks())" advice-ref="lowStockBookCachingAdvice"/>
    </aop:config>
    以上幾句定義使用cglib創(chuàng)建Proxy, 為BookManager的save()和remove()加上lowStockBookFlushingAdvice,為 BookStockChecker.getLowStockBooks加上lowStockBookCachingAdvice.

    execution(* *..BookManager.save(..))
    第一顆* 代表ret-type-pattern 返回值可任意,
    *..BookManager 代表任意Pacakge里的BookManager類。
    如果寫成com.xyz.service.* 則代表com.xyz.service下的任意類
    com.xyz.service..* com.xyz.service則代表com.xyz.service及其子package下的任意類
    save代表save方法,也可以寫save* 代表saveBook()等方法
    (..) 匹配0個參數(shù)或者多個參數(shù)的,任意類型
    (x,..) 第一個參數(shù)的類型必須是X
    (x,*,*,s,..) 匹配至少4個參數(shù),第一個參數(shù)必須是x類型,第二個和第三個參數(shù)可以任意,第四個必須是s類型。

    8、事務配置的不同形式
    事務配置一:
    <!-- 支持 @AspectJ 標記-->
    <aop:aspectj-autoproxy />

    <!-- 以AspectJ方式 定義 AOP -->
    <aop:config proxy-target-class="false">
    <aop:advisor
    pointcut="execution(* ....recruitment.service.impl..*.*(..)) || execution(* ....employeeinfo.service.impl..*Service.*(..))"
    advice-ref="txAdvice" />
    </aop:config>

    <!-- 基本事務定義,使用transactionManager作事務管理,默認get*方法的事務為readonly,其余方法按默認設置.
    默認的設置請參考Spring文檔事務一章. -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <tx:method name="get*" read-only="true" propagation="REQUIRED" />
    <tx:method name="load*" read-only="true" propagation="REQUIRED" />
    <tx:method name="find*" read-only="true" propagation="REQUIRED" />
    <tx:method name="*" propagation="REQUIRED" />
    </tx:attributes>
    </tx:advice>


    事務配置二:
    <!-- 支持 @AspectJ 標記-->
    <aop:aspectj-autoproxy />

    <!-- 以AspectJ方式 定義 AOP -->
    <aop:config proxy-target-class="false">
    <aop:advisor
    pointcut="execution(* ....employeeinfo.service.impl..*Service.*(..))"
    advice-ref="txAdvice" />

    <aop:advisor
    pointcut="execution(* ....recruitment.service.impl..*.*(..)) "
    advice-ref="txAdviceRet" />
    </aop:config>

    <!-- 基本事務定義,使用transactionManager作事務管理,默認get*方法的事務為readonly,其余方法按默認設置.
    默認的設置請參考Spring文檔事務一章. -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    ......
    </tx:attributes>
    </tx:advice>
    <tx:advice id="txAdviceRet"
    transaction-manager="transactionManager">
    <tx:attributes>
    .........
    </tx:attributes>
    </tx:advice>

    事務配置三:(在java文件中配置)
    需要單獨配置事務的類名前配置事務開啟
    @Transactional(propagation = Propagation.SUPPORTS)
    需要使用事務的方法前配置開啟事務的信息
    @Transactional(propagation = Propagation.REQUIRED)
    如:
    @Transactional(propagation = Propagation.SUPPORTS)
    public class ApplierService extends BaseService<Applier> implements
    IApplierService {
    @Transactional(propagation = Propagation.REQUIRED)
    public void addOrModify(Applier t) {
    applierDao.saveOrUpdate(toDBValue(t));
    }
    }

    9、正確配置重寫父類方法時事務
    參閱:http://aumy2008.javaeye.com/admin/blogs/152928

    10、spring中bean的作用域詳解
    bean屬性scope的選取(prototype、request、session、global session):
    <bean name="companyAction"
    class="....CompanyAction" scope="prototype"/>
    s:datetimepicker錄入框提交正常。
    prototype作用域部署的bean,每一次請求(將其注入到另一個bean中,或者以程序的方式調用容器的getBean()方法)
    都會產(chǎn)生一個新的bean實例,相當一個new的操作,對于prototype作用域的bean,有一點非常重要,
    那就是Spring不能對一個prototype bean的整個生命周期負責,
    容器在初始化、配置、裝飾或者是裝配完一個prototype實例后,將它交給客戶端,
    隨后就對該prototype實例不聞不問了。不管何種作用域,容器都會調用所有對象的初始化生命周期回調方法,
    而對prototype而言,任何配置好的析構生命周期回調方法都將不會被調用。
    清除prototype作用域的對象并釋放任何prototype bean所持有的昂貴資源,都是客戶端代碼的職責。
    (讓Spring容器釋放被singleton作用域bean占用資源的一種可行方式是,通過使用bean的后置處理器,
    該處理器持有要被清除的bean的引用。)

    request、session、global session使用的時候首先要在web.xml中做如下配置:
    如果你使用的是Servlet 2.4及以上的web容器,那么你僅需要在web應用的XML聲明文件web.xml中增加下述ContextListener即可:
    <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    <bean name="companyAction"
    class="....action.CompanyAction" scope="request"/>
    s:datetimepicker錄入框提交正常。

    <bean name="companyAction"
    class="....action.CompanyAction" scope="session"/>
    s:datetimepicker錄入框不能正常提交。

    <bean name="companyAction"
    class="....action.CompanyAction" scope="global session"/>
    java.lang.IllegalStateException: No Scope registered for scope 'global session'
    分析:global session作用域類似于標準的HTTP Session作用域,不過它僅僅在基于portlet的web應用中才有意義。

    Spring中bean的作用域相關網(wǎng)站:
    http://doc.javanb.com/spring-framework-reference-zh-2-0-5/ch03s04.html
    posted on 2008-12-26 13:45 一葉笑天 閱讀(1007) 評論(0)  編輯  收藏 所屬分類: 開源技術
    主站蜘蛛池模板: 国产国产人免费人成成免视频 | 免费无码成人AV片在线在线播放| 男女猛烈激情xx00免费视频| 成年网站免费视频A在线双飞| 亚洲综合图片小说区热久久| 5g影院5g天天爽永久免费影院| 最近2019中文免费字幕在线观看| 亚洲一区无码精品色| 日本一区二区三区免费高清在线| 免费一看一级毛片人| 久久精品国产亚洲AV高清热| 久久国产精品成人片免费| 亚洲成A人片在线观看中文| 78成人精品电影在线播放日韩精品电影一区亚洲 | 又粗又硬又黄又爽的免费视频 | 亚洲AV无码日韩AV无码导航| a成人毛片免费观看| 亚洲VA中文字幕无码一二三区 | 国内免费高清在线观看| 亚洲国产精品国自产拍AV| 欧洲亚洲国产精华液| 2021在线永久免费视频| 亚洲狠狠ady亚洲精品大秀| 啦啦啦中文在线观看电视剧免费版| 中文字幕亚洲男人的天堂网络| 在线免费视频你懂的| 亚洲一区二区女搞男| 性xxxxx大片免费视频| 亚洲av日韩av综合| 免费在线看黄的网站| 亚洲黄色免费网站| 国产老女人精品免费视频| 久久最新免费视频| 亚洲小说区图片区| 亚洲精品天堂成人片?V在线播放 | 6080午夜一级毛片免费看 | 久久福利青草精品资源站免费| 久久久久亚洲av无码专区| 成人毛片免费在线观看| 久久久久精品国产亚洲AV无码| 免费大黄网站在线观|