編輯
applicationContext.xml和spring-mvc.xml。
一、配置applicationContext.xml
1)定義組件掃描
<!-- 使用annotation 自動註冊bean,並檢查@Required,@Autowired的屬性已被注入 -->
<context:component-scan base-package="com.qctc.pds">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>
2)定義資料庫參數資訊
<!-- 在不同開發階段可以切換資料庫參數資訊,不需要重複寫資料庫參數資訊 -->
<context:property-placeholder
ignore-resource-not-found="true" location="classpath*:/database.properties" />
在/src/main/resources目錄下建database.properties
3)配置資料來源資訊
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!-- 基本屬性 url、user、password -->
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="${druid.initialSize}" />
<property name="minIdle" value="${druid.minIdle}" />
<property name="maxActive" value="${druid.maxActive}" />
<!-- 配置擷取串連等待逾時的時間 -->
<property name="maxWait" value="${druid.maxWait}" />
<!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閑串連,單位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="${druid.timeBetweenEvictionRunsMillis}" />
<!-- 配置一個串連在池中最小生存的時間,單位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="${druid.minEvictableIdleTimeMillis}" />
<property name="validationQuery" value="${druid.validationQuery}" />
<property name="testWhileIdle" value="${druid.testWhileIdle}" />
<property name="testOnBorrow" value="${druid.testOnBorrow}" />
<property name="testOnReturn" value="${druid.testOnReturn}" />
<!-- 開啟PSCache,並且指定每個串連上PSCache的大小 -->
<property name="poolPreparedStatements" value="${druid.poolPreparedStatements}" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="${druid.maxPoolPreparedStatementPerConnectionSize}" />
<!-- 配置監控統計攔截的filters -->
<property name="filters" value="${druid.filters}" />
</bean>
4)配置內建交易管理
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
5)配置註解驅動
<tx:annotation-driven transaction-manager="transactionManager" />對@Transactional這個註解進行的驅動,這是基於註解的方式使用事務配置聲明,這樣在具體應用中可以指定對哪些方法使用事務
<tx:annotation-driven transaction-manager="txManager" />
6)配置jdbc模板
<!--配置一個JdbcTemplate執行個體,並將這個“共用的”,“安全的”執行個體注入到不同的DAO類中去-->
<bean id="defaultJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
二、配置spring-mvc.xml。
1)定義組件掃描
<!-- 使用annotation 自動註冊bean,並檢查@Required,@Autowired的屬性已被注入 -->
<context:component-scan base-package="com.qctc.pds">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>
2)在spring-mvc.xml中添加<mvc:annotation-driven>就能使用springmvc的註解驅動特性。
<mvc:annotation-driven
content-negotiation-manager="contentNegotiationManager">
<mvc:message-converters register-defaults="true">
<!-- 將StringHttpMessageConverter的預設編碼設為UTF-8 -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" />
</bean>
<!-- 將Jackson2HttpMessageConverter的預設格式化輸出設為true -->
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="prettyPrint" value="true" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
3)
<!-- REST中根據URL尾碼自動判定Content-Type及相應的View -->
<bean id="contentNegotiationManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="mediaTypes">
<value>
json=application/json
xml=application/xml
</value>
</property>
</bean>
4)
<!-- 定義JSP檔案的位置 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 容器預設的DefaultServletHandler處理 所有靜態內容與無RequestMapping處理的URL -->
<mvc:default-servlet-handler />
<!-- 將Controller拋出的異常轉到特定View, 保持SiteMesh的裝飾效果 -->
<bean
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="org.apache.shiro.authz.UnauthorizedException">error/403</prop>
<prop key="java.lang.Throwable">error/500</prop>
</props>
</property>
</bean>