<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、配置數據源
    <!-- 數據源定義,使用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中采用構造方法注入注意要點:
    在配置文件中顯式書寫注入的參數。如:
    <bean class="....impl.JobService"
    id="retJobService">
    <constructor-arg ref="retJobDao" />
    </bean>
    多個參數的構造函數示例
    <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創建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個參數或者多個參數的,任意類型
    (x,..) 第一個參數的類型必須是X
    (x,*,*,s,..) 匹配至少4個參數,第一個參數必須是x類型,第二個和第三個參數可以任意,第四個必須是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()方法)
    都會產生一個新的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的作用域相關網站:
    http://doc.javanb.com/spring-framework-reference-zh-2-0-5/ch03s04.html
    posted on 2008-12-26 13:45 一葉笑天 閱讀(1003) 評論(0)  編輯  收藏 所屬分類: 開源技術
    主站蜘蛛池模板: 久久亚洲av无码精品浪潮| 成人免费的性色视频| 免费人成大片在线观看播放| 五月天婷婷精品免费视频| 99精品视频在线视频免费观看| 成人黄软件网18免费下载成人黄18免费视频| 四虎永久在线免费观看| 亚洲不卡中文字幕无码| 亚洲AV永久无码精品放毛片| 免费萌白酱国产一区二区三区| 在线观看免费宅男视频| 亚洲精品tv久久久久久久久| 校园亚洲春色另类小说合集| 污污网站18禁在线永久免费观看| 国产黄色片在线免费观看| 久久综合日韩亚洲精品色| 黄色三级三级三级免费看| 91黑丝国产线观看免费| 亚洲综合av一区二区三区| 成人A片产无码免费视频在线观看| 亚洲性无码av在线| 美女扒开屁股让男人桶爽免费| 成人伊人亚洲人综合网站222| 亚洲高清在线观看| 亚洲喷奶水中文字幕电影 | 精品国产免费观看一区| 亚洲AV无码久久精品狠狠爱浪潮| 精品亚洲永久免费精品| 色噜噜亚洲精品中文字幕| 精品亚洲国产成人av| 亚洲精品偷拍视频免费观看 | 亚洲国产一区二区三区青草影视| 亚洲网站免费观看| 亚洲国产成人一区二区三区| 最近中文字幕无免费| 亚洲国产高清人在线| 最近2019中文字幕mv免费看| caoporn成人免费公开| 亚洲人AV永久一区二区三区久久| 美女视频黄的免费视频网页| 亚洲日韩乱码中文字幕|