spring 注解 xml配置 事务 aop面向切面 的知识点与理解
记录项目的spring mvc 的配置文件,spring 整合shiro和mybatis的配置xml文件 ()
1.springMvc的配置xml如下(spring-mvc.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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 静态资源访问处理 --> <mvc:resources mapping="/js/**" location="/js/"/> <mvc:resources mapping="/css/**" location="/css/"/> <mvc:resources mapping="/datas/**" location="/datas/"/> <mvc:resources mapping="/build/**" location="/build/"/> <mvc:resources mapping="/plugins/**" location="/plugins/"/> <mvc:resources mapping="/src/**" location="/src/"/> <mvc:resources mapping="/components/**" location="/components/"/> <!--开启AOP--> <aop:aspectj-autoproxy proxy-target-class="true"/> <!-- 自动扫描该包 --> <context:component-scan base-package="com.gviiii.controller"/> <!-- 默认的注解映射的支持 --> <mvc:annotation-driven/> <!--解析返回JSON --> <!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter"/> </list> </property> </bean> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <!-- 配置文件上传 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!--默认编码--> <property name="defaultEncoding" value="utf-8" /> <!--文件大小最大值--> <property name="maxUploadSize" value="10485760000" /> <!--内存中的最大值--> <property name="maxInMemorySize" value="40960" /> </bean> <!-- freemarker config --> <bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="location" value="classpath:freemarker.properties"/> </bean> <!-- 配置freeMarker的模板路径 ,html路径在这里设置,视图解析里面配置无效--> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="freemarkerSettings" ref="freemarkerConfiguration"/> <property name="templateLoaderPath" value="/views/"/> </bean> <!-- freemarker视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="cache" value="true"/> <property name="suffix" value=".html"/> <!--只是这里后缀改成.html --> <property name="viewClass" value="com.gviiii.expand.BaseFreeMarkerView"/> <property name="exposeSpringMacroHelpers" value="true"/> <property name="exposeRequestAttributes" value="true"/> <property name="exposeSessionAttributes" value="true"/> <property name="requestContextAttribute" value="request"/> <property name="contentType" value="text/html;charset=UTF-8"/> </bean> </beans>
因为项目的表现层是 html 没有使用jsp 所以一部分页面是采用的freemarker来做后台与前端界面的交互,freemarker 整合中的BaseFreeMarkerView 类代码如下(BaseFreeMarkerView.java):
package com.gviiii.expand; import org.springframework.web.servlet.view.freemarker.FreeMarkerView; import javax.servlet.http.HttpServletRequest; import java.util.Map; /** * @version V1.0.0 * @classname BaseFreeMarkerView * @description description */ public class BaseFreeMarkerView extends FreeMarkerView { private static final String CONTEXT_PATH = "basePath"; private static final String WEB_PATH = "webPath"; @Override protected void exposeHelpers(Map<String, Object> model, HttpServletRequest request) throws Exception { String scheme = request.getScheme(); String serverName = request.getServerName(); int port = request.getServerPort(); String path = request.getContextPath(); String basePath = scheme + "://" + serverName + ":" + port + path; model.put(CONTEXT_PATH, basePath); model.put(WEB_PATH, path); super.exposeHelpers(model, request); } }
这里freemarker的配置如下(freemarker.properties)
classic_compatible=true
##如果变量为null,转化为空字符串,比如做比较的时候按照空字符串做比较
whitespace_stripping=true
##去掉多余的空格,非常有用
##模板更新事件,设置为1秒,正式环境设置为3600秒
template_update_delay=1
##模板更新时间,这里配置是1秒更新一次,正式环境,模板不会改变,可以降这个值很大,提高效率
locale=zh_CN
##中国
default_encoding=UTF-8
##编码utf8
url_escaping_charset=UTF-8
##url编码utf8
date_format=yyyy-MM-dd
##显示日期格式
time_format=HH:mm:ss
##显示时间格式
datetime_format=yyyy-MM-dd HH:mm:ss
##显示日期格式
number_format=\#0.\#\#\#\#
output_encoding=UTF-8