一葉笑天
          雄關漫道真如鐵, 而今邁步從頭越。 從頭越, 蒼山如海, 殘陽如血。
          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 一葉笑天 閱讀(1006) 評論(0)  編輯  收藏 所屬分類: 開源技術
          主站蜘蛛池模板: 安远县| 墨竹工卡县| 珲春市| 习水县| 安徽省| 都匀市| 峨边| 济宁市| 乡城县| 周至县| 西昌市| 博乐市| 丹寨县| 固始县| 浦东新区| 浑源县| 册亨县| 罗源县| 中阳县| 永善县| 海晏县| 扎囊县| 丰城市| 巍山| 大英县| 高淳县| 略阳县| 依安县| 曲松县| 天祝| 安吉县| 汤原县| 沙田区| 金堂县| 南康市| 长顺县| 墨脱县| 泸西县| 万州区| 广西| 芒康县|