溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Spring2.5常用的配置備份

發(fā)布時(shí)間:2021-07-14 10:40:01 來源:億速云 閱讀:115 作者:chen 欄目:編程語言

這篇文章主要講解了“Spring2.5常用的配置備份”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Spring2.5常用的配置備份”吧!

一、啟動Spring2.5監(jiān)聽,讓web項(xiàng)目整合spring

在web.xml中配置,代碼如下:

<context-param>  <param-name>contextConfigLocation</param-name>  <param-value>    classpath:beans.xml   </param-value> </context-param>  <listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>

注:如果在支持低版本的Servlet的web容器中,可以采用Servlet形式,把上面的<listener>換成下面的<servlet>,
優(yōu)點(diǎn)是可以設(shè)置自啟動順序,代碼如下:

<!-- 使用自動啟動的Servlet進(jìn)行初始化 -->   <servlet>   <servlet-name>contextLoaderServlet</servlet-name>   <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>   <load-on-startup>2</load-on-startup> </servlet>

二、關(guān)于配置日志文件Log4J

由于WebApplicationContext需要使用日志功能,用戶可以將Log4J的配置文件放到類路徑WEB-INF/classes下,這Log4J引擎可以順利啟動。如果將Log4J配置文件放在其他位置,用戶還需要在web.xml中指定Log4J的位置,spring也Log4J提供了兩種方式,跟上面類似,有監(jiān)聽模式和自啟動模式(Servlet模式)

監(jiān)聽模式:

<context-param>  <param-name>log4jConfigLocation</param-name>  <param-value>    /WEB-INF/log4j.properties   </param-value> </context-param>  <listener>  <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener>

而自啟動模式跟上面一樣把listen換成servlet,如:

<servlet>   <servlet-name>log4jConfigServlet</servlet-name>   <servlet-class>org.springframework.web.util.Log4jConfigServlet</servlet-class>   <load-on-startup>1</load-on-startup> </servlet>

下面是一個(gè)簡單的log4j.properties文件:

log4j.rootLogger=INFO,A1  log4j.appender.A1=org.apache.log4j.ConsoleAppender  log4j.appender.A1.layout=org.apache.log4j.PatternLayout  log4j.appender.A1.layout.ConversionPattern=%d %5p [%t] (%F:%L) - %m%n

注意:如果手動配置Log4J,則先要讓日志文件Log4J先啟動,再spring的監(jiān)聽或啟動。

三、使用外部屬性文件和配置數(shù)據(jù)源

a、使用外部屬性文件  

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    <property name="locations">     <list>      <value>classpath:com/baobaotao/place/jdbc.properties</value>     </list>    </property>    <property name="fileEncoding" value="utf-8" />   </bean>

而在spring2.5的版本中提供了一種更簡便的方式,如:

<context:property-placeholder location="classpath:config/jdbc.properties"/>

這樣以后要使用屬性文件中的資源時(shí),可以使用${屬性名}來獲得。

b、常用數(shù)據(jù)源的配置

***種是:DBCP數(shù)據(jù)源,(需要加入2個(gè)jar文件,在spring中的lib下jakarta-commons/commons-dbcp.jar和commons-pools.jar)主要配置如下:

<!-- 配置數(shù)據(jù)源  -->  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">   <property name="driverClassName" value="com.mysql.jdbc.Driver" />   <property name="url" value="jdbc:mysql://localhost:3309/sampledb" />   <property name="username" value="root" />   <property name="password" value="1234" />  </bean>

第二種是:c3p0數(shù)據(jù)源,跟***種一個(gè)類型,需加入c3p0.jar包。

第三種是:JNDI數(shù)據(jù)源,配置在高性能的應(yīng)用服務(wù)器(如WebLogic、WebSphere等)

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">       <property name="jndiName" value="java:comp/env/jdbc/bbt"/>   </bean>

從spring2.0開始提供jee命名空間,可以簡化配置如下:

<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/bbt"/>

四、事務(wù)管理配置

a、Spring JDBC 和 iBatis事務(wù)管理器的配置

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">       <property name="dataSource" ref="dataSource"/> </bean>

b、Hibernate3以上事務(wù)管理器的配置(先要集成hibernate,再配置事務(wù)管理器)

<!-- 集成hibernate -->  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">     <property name="dataSource" ref="dataSource"/>     <property name="mappingResources">       <list>         <value>classpath:product.hbm.xml</value>       </list>     </property>     <property name="hibernateProperties">       <props>        <prop key="hibernate.dialect">       </props>     </property>   </bean>  <!-- 配置Hibernate事務(wù)策略 -->  <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">         <property name="sessionFactory" ref="sessionFactory"></property>  </bean>  五、配置tx/aop聲明式事務(wù)   <!-- 配置tx/aop聲明式事務(wù) -->  <!-- 聲明一個(gè)切面 -->  <tx:advice id="txAdvice" transaction-manager="txManager">   <tx:attributes>    <tx:method name="find*" propagation="REQUIRED" read-only="true"/>    <tx:method name="save*" propagation="REQUIRED"/>    <tx:method name="update*" propagation="REQUIRED"/>    <tx:method name="*" propagation="SUPPORTS" read-only="true" />   </tx:attributes>  </tx:advice>  <!-- 把切面注入到業(yè)務(wù)中 -->   <aop:config>   <aop:pointcut id="productServiceMethods" expression="execution(* com.wzc.student.business.*.*(..))" />   <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />  </aop:config>

六、使用基于@Transactional注解形式聲明事務(wù)

在bean.xml通過一 行xml配置就可以,如

<tx:annotation-driven transaction-manager="txManager"/>

提示:如果你用 'transactionManager' 來定義 PlatformTransactionManager bean的名字的話,你就可以忽略

<tx:annotation-driven/> 標(biāo)簽里的 'transaction-manager' 屬性。

這樣在需要業(yè)務(wù)的接口或?qū)崿F(xiàn)類中加上@Transactional就可以了,詳細(xì)見文檔。

七、spring的字符過濾器的配置

在web.xml中增加如下過濾器

<filter>   <filter-name>characterEncodingFilter</filter-name>   <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>   <init-param>    <param-name>encoding</param-name>    <param-value>UTF-8</param-value>   </init-param>  </filter>     <filter-mapping>   <filter-name>characterEncodingFilter</filter-name>   <url-pattern>/*</url-pattern>  </filter-mapping>

感謝各位的閱讀,以上就是“Spring2.5常用的配置備份”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Spring2.5常用的配置備份這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI