spring3.0配置搭建

來源:互聯網
上載者:User

原帖地址:http://apps.hi.baidu.com/share/detail/20255244

整了一天終於把spring3.0.2+hibernate3+tiles2.1.3 結合起來了,搭建了一個項目環境,下面我把我的設定檔放上來,希望對spring3感興趣的同志們有協助。

applicationContext-mvc-controller.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="lylt.mvc.action" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
   <ref bean="openSessionInViewInterceptor" />
   <ref bean="userRightInterceptor" />
</list>
</property>
</bean>

<!-- spring 事物的管理 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean id="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory">
   <ref bean="sessionFactory" />
</property>
</bean>

<!-- spring 攔截器的配置 -->
<bean id="userRightInterceptor" class="lylt.interceptor.UserRightInterceptor"></bean>

<!-- spring 錯誤和異常頁面的定義 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
   <props>
    <prop key="org.springframework.dao.DataAccessException">error</prop>
    <prop key="org.springframework.transaction.TransactionException">error</prop>
   </props>
</property>
</bean>

<!-- tiles的定義 -->
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
   <list>
    <value>/WEB-INF/tiles/*.xml</value>
   </list>
</property>
<property name="preparerFactoryClass" value="org.springframework.web.servlet.view.tiles2.SpringBeanPreparerFactory" />
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="messages" />
</beans>
applicationContext-hibernate.xml 的配置 這裡採用的是dbcp 來串連資料庫

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
   http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
   http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:annotation-config />
<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
   <list>
    <value>classpath:init.properties</value>
   </list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
   <value>${datasource.driverClassName}</value>
</property>
<property name="url">
   <value>${datasource.url}</value>
</property>
<property name="username">
   <value>${datasource.username}</value>
</property>
<property name="password">
   <value>${datasource.password}</value>
</property>
<property name="maxActive">
   <value>${jdbc.maxActive}</value>
</property>
<property name="maxIdle">
   <value>${jdbc.maxIdle}</value>
</property>
<property name="maxWait">
   <value>${jdbc.maxWait}</value>
</property>
<property name="defaultAutoCommit">
   <value>${jdbc.defaultAutoCommit}</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
   <ref local="dataSource" />
</property>
<property name="mappingDirectoryLocations">
   <list>
    <value>classpath:lylt/hibernate/data/</value>
   </list>
</property>
<property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">
     ${hibernate.dialect}
    </prop>
    <prop key="hibernate.show_sql">
     ${hibernate.show_sql}
    </prop>
    <prop key="hibernate.jdbc.fetch_size">
     ${hibernate.jdbc.fetch_size}
    </prop>
    <prop key="hibernate.jdbc.batch_size">
     ${hibernate.jdbc.batch_size}
    </prop>
    <prop key="hibernate.cache.provider_class">
     org.hibernate.cache.EhCacheProvider
    </prop>
    <prop key="hibernate.cache.use_query_cache">true</prop>
   </props>
</property>
</bean>
</beans>

資料庫採用的書postgres 下面是一些設定檔

datasource.type=postgres
datasource.driverClassName=org.postgresql.Driver
datasource.url=jdbc:postgresql://192.168.1.203:5432/dbname
datasource.username=postgres
datasource.password=postgres

datasource.maxActive=10
datasource.maxIdle=2
datasource.maxWait=120000
datasource.whenExhaustedAction=1
datasource.validationQuery=select 1 from dual
datasource.testOnBorrow=true
datasource.testOnReturn=false

#dbcp
jdbc.maxActive=100
jdbc.maxIdle=20
jdbc.maxWait=5
jdbc.defaultAutoCommit=true

hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

hibernate.jdbc.batch_size=25
hibernate.jdbc.fetch_size=50
hibernate.show_sql=true
hibernate.connection.release_mode=after_transaction

applicationContext-service.xml 的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
   http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
   http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:annotation-config />

<!--這裡是spring的annotation的配置-->
<context:component-scan
base-package="lylt.hibernate.dao,lylt.service,lylt.mvc" />
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" />
</beans>

在web.xml加入如下配置即可

<servlet>
<servlet-name>mvcaction</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/applicationContext-mvc-controller.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvcaction</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>

基本上就是這些,其他有些地方都是些基本的就不需要多說了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.