最新的三大框架整合之配置檔案詳解
一:spring.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:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 掃描bean包 -->
<context:component-scan base-package="com.yc">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 引入外部屬性檔案 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 資料來源 -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
p:username="${jdbc.username}" p:password="${jdbc.password}" p:minIdle="${jdbc.minIdle}"
p:maxTotal="${jdbc.maxTotal}" p:initialSize="${jdbc.initialSize}"
p:removeAbandonedOnBorrow="true" p:removeAbandonedTimeout="180"
p:connectionProperties="clientEncoding=UTF-8"
/>
<!-- 配製mybatis的會話工廠物件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis.xml"></property>
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage"
value="com.yc.weixin.entity,com.yc.privilege.entity,com.yc.weixin.bean,com.yc.weixin.bean.events,com.yc.weixin.bean.menu,com.yc.weixin.bean.requestMessage,com.yc.weixin.bean.responseMessage"></property>
<property name="mapperLocations" value="classpath:*mapper/*Mapper.xml"></property>
</bean>
<!-- 配製對映介面 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
p:sqlSessionFactoryBeanName="sqlSessionFactory" p:basePackage="com.yc.*.mapper" />
<!-- 限制檔案為200M,resolveLazily是為了推遲檔案解析,已捕獲檔案大小異常 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="209715200" />
<property name="defaultEncoding" value="UTF-8" />
<property name="resolveLazily" value="true" />
</bean>
</beans>
二:springmvc檔案詳解
<?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:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/oxm
http://www.springframework.org/schema/oxm/spring-oxm.xsd">
<!-- 掃描bean包 ,use-default-filters取消預設設定-->
<context:component-scan base-package="com.yc" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 引入外部屬性檔案 -->
<!-- <context:property-placeholder location="classpath:db.properties" /> -->
<!-- 配置檢視解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- prefix:字首;suffix:字尾 -->
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 控制器處理異常 -->
<bean id="exceptionResolver"
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.Exception">
404
</prop>
</props>
</property>
</bean>
<!-- 限制檔案為200M,resolveLazily是為了推遲檔案解析,已捕獲檔案大小異常 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="209715200" />
<property name="defaultEncoding" value="UTF-8" />
<property name="resolveLazily" value="true" />
</bean>
<!-- 返回xml格式 -->
<!-- <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.yc.weixin.bean.responseMessage.ResponseArticle</value>
</list>
</property>
</bean>
</constructor-arg>
</bean> -->
<!-- default-servlet-handler 將在 SpringMVC 上下文中定義一個 DefaultServletHttpRequestHandler,
它會對進入 DispatcherServlet 的請求進行篩查, 如果發現是沒有經過對映的請求, 就將該請求交由 WEB 應用伺服器預設的 Servlet
處理. 如果不是靜態資源的請求,才由 DispatcherServlet 繼續處理 一般 WEB 應用伺服器預設的 Servlet 的名稱都是 default.
若所使用的 WEB 伺服器的預設 Servlet 名稱不是 default,則需要通過 default-servlet-name 屬性顯式指定 -->
<mvc:interceptors>
<!--配置自定義的攔截器 -->
<mvc:interceptor>
<mvc:mapping path="/**" />
<mvc:exclude-mapping path="/front/login.do" />
<mvc:exclude-mapping path="/weixin/**" />
<mvc:exclude-mapping path="/weixinfront/**" />
<mvc:exclude-mapping path="/article/*/findInit.do"/>
<mvc:exclude-mapping path="/article/*/findArticleBypage.do"/>
<mvc:exclude-mapping path="/article/findarticleByAid.do"/>
<mvc:exclude-mapping path="/article/*/showmore.do"/>
<mvc:exclude-mapping path="/article/findFirstArticleByTypeId.do"/>
<bean class="com.yc.privilege.interceptors.LoginInterceptors" />
</mvc:interceptor>
</mvc:interceptors>
<bean id="utf8Charset" class="java.nio.charset.Charset" factory-method="forName">
<constructor-arg value="UTF-8" />
</bean>
<!-- 預設靜態資源處理請求 --><!-- 添加註解驅動:裡面預設註冊了XMl的轉換器還有JSON的轉換器… -->
<mvc:default-servlet-handler />
<mvc:annotation-driven >
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg ref="utf8Charset"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
</beans>
三:mybatis.xml詳解
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="LOG4J"></setting>
</settings>
<!--
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/mybatis/example/BlogMapper.xml" />
</mappers>
-->
</configuration>
四:log4j.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p %C{1} (%F:%L) - %m%n"/>
</Console>
</Appenders>
<Loggers>
<!--自定義包中類的日誌級別-->
<Logger name="com.yc" level="error"/>
<!-- 日誌級別總開頭 -->
<Root level="error">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
五:log4j.proprities
log4j.rootLogger=debug,stdout,file
log4j.logger.com.yc.weixin.mapper=ERROR
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %m%n
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=yc.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %l %m%n
六:還有一個就是連結dataSources的配置了