第一步.创建maven工程
我的maven项目目录图如下
01 | < dependency > |
02 | < groupId >org.hibernate</ groupId > |
03 | < artifactId >hibernate-validator-annotation-processor</ artifactId > |
04 | < version >5.1.0.Alpha1</ version > |
05 | </ dependency > |
06 | < dependency > |
07 | < groupId >org.hibernate</ groupId > |
08 | < artifactId >hibernate-validator</ artifactId > |
09 | < version >5.0.1.Final</ version > |
10 | </ dependency > |
第二点要注意的是
1 | <!-- aspectjweaver --> |
2 | < dependency > |
3 | < groupId >org.aspectj</ groupId > |
4 | < artifactId >aspectjweaver</ artifactId > |
5 | < version >1.7.0</ version > |
6 | </ dependency > |
加入上述aop实现方式就无需加spring-aop的依赖了
第二步 web.xml配置相应的servlet filter过滤器
我的web.xml如下 下面我将一一解释
01 | < context-param > |
02 | < param-name >contextConfigLocation</ param-name > //这里我配置到classpath:是指类classpath( 编译后再classes下)该文件在maven项目src/main/resources文件夹下 多个用“,”号隔开 classpath:spring-jdbc.xml,classpath:spring-hibernate.xml |
03 | < param-value >classpath:spring.xml</ param-value > |
04 | </ context-param > |
05 | < filter > |
06 | < description >字符集过滤器</ description > |
07 | < filter-name >encodingFilter</ filter-name > |
08 | < filter-class >org.springframework.web.filter.CharacterEncodingFilter</ filter-class > |
09 | < init-param > |
10 | < description >字符集编码</ description > |
11 | < param-name >encoding</ param-name > |
12 | < param-value >UTF-8</ param-value > |
13 | </ init-param > |
14 | </ filter > |
15 | < filter-mapping > |
16 | < filter-name >encodingFilter</ filter-name > |
17 | < url-pattern >/*</ url-pattern > |
18 | </ filter-mapping > |
19 | < listener > |
20 | < description >spring监听器</ description > |
21 | < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class > |
22 | </ listener > |
23 | <!-- 防止spring内存溢出监听器 --> |
24 | < listener > |
25 | < listener-class >org.springframework.web.util.IntrospectorCleanupListener</ listener-class > |
26 | </ listener > |
27 |
28 | <!-- spring mvc servlet --> |
29 | < servlet > |
30 | < description >spring mvc servlet</ description > |
31 | < servlet-name >springMvc</ servlet-name > |
32 | < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class > |
33 | < init-param > |
34 | < description >spring mvc 配置文件</ description > |
35 | < param-name >contextConfigLocation</ param-name > |
36 | < param-value >classpath:spring-mvc.xml</ param-value > |
37 | </ init-param > |
38 | < load-on-startup >1</ load-on-startup > |
39 | </ servlet > |
40 |
41 |
42 | < servlet-mapping > |
43 | < servlet-name >springMvc</ servlet-name > |
44 | < url-pattern >/</ url-pattern > //这里注意 这里你如果想实现不能用/*不报错 但是无法映射到相应的url上 你如果要实现strut1 strut2相应风格 把他改成 *.action *.do即可 |
45 | </ servlet-mapping > |
46 | //这是欢迎页面可以配置多个 不过你可以直接用response.sendRedirect(request.getContextPath()+"/index.htm"); |
47 | < welcome-file-list > |
48 | < welcome-file >/index.jsp</ welcome-file > |
49 | </ welcome-file-list > |
50 |
51 | <!-- 配置session超时时间,单位分钟 --> |
52 | < session-config > |
53 | < session-timeout >15</ session-timeout > |
54 | </ session-config > |
第三部 我们来配置spring配置i文件 如上面web.xml的名称是spring.xml下面 我来解释我的配置
<!-- 引入属性文件这里可以这样加载 也可以用bean 类 还有util命名空间加载 可以百度了解下 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 自动扫描dao和service包(自动注入) 这里我排除掉controller注解 交给springMVC 的dispatchServlet启动时候 来扫描控制器-->
001 | < context:component-scan base-package = "com.cn21.talk.dao,com.cn21.talk.service" > |
002 | < context:exclude-filter type = "annotation" expression = "org.springframework.stereotype.Controller" /> |
003 | </ context:component-scan > |
004 | < import resource = "classpath:spring-jdbc.xml" /> |
005 | 我把数据库连接池相关配置到spring-jdbc.xml中 下面是其内容 |
006 |
007 | <? xml version = "1.0" encoding = "UTF-8" ?> |
008 | < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:tx = "http://www.springframework.org/schema/tx" xmlns:aop = "http://www.springframework.org/schema/aop" xsi:schemaLocation=" |
009 | http://www.springframework.org/schema/beans |
010 | http://www.springframework.org/schema/beans/spring-beans-3.0.xsd |
011 | http://www.springframework.org/schema/tx |
012 | http://www.springframework.org/schema/tx/spring-tx-3.0.xsd |
013 | http://www.springframework.org/schema/aop |
014 | http://www.springframework.org/schema/aop/spring-aop-3.0.xsd |
015 | "> |
016 |
017 |
018 | <!-- JNDI方式配置数据源 --> |
019 | <!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="${jndiName}"></property> </bean> --> |
020 |
021 |
022 | <!-- 配置数据源这里我 原来用阿里的连接池 但是多线程并发下会出现 获取链接为空 为此我改成boneCP --> |
023 | <!-- <bean name="dataSourcePersd" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> --> |
024 | <!-- <property name="url" value="${jdbc_url}" /> --> |
025 | <!-- <property name="username" value="${jdbc_username}" /> --> |
026 | <!-- <property name="password" value="${jdbc_password}" /> --> |
027 |
028 |
029 | <!-- 初始化连接大小 --> |
030 | <!-- <property name="initialSize" value="0" /> --> |
031 | <!-- 连接池最大使用连接数量 --> |
032 | <!-- <property name="maxActive" value="50" /> --> |
033 | <!-- 连接池最大空闲 --> |
034 | <!-- <property name="maxIdle" value="20" /> --> |
035 | <!-- 连接池最小空闲 --> |
036 | <!-- <property name="minIdle" value="0" /> --> |
037 | <!-- 获取连接最大等待时间 --> |
038 | <!-- <property name="maxWait" value="60000" /> --> |
039 |
040 |
041 | <!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> --> |
042 |
043 |
044 | <!-- <property name="validationQuery" value="${validationQuery}" /> --> |
045 | <!-- <property name="testOnBorrow" value="false" /> --> |
046 | <!-- <property name="testOnReturn" value="false" /> --> |
047 | <!-- <property name="testWhileIdle" value="true" /> --> |
048 |
049 |
050 | <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> |
051 | <!-- <property name="timeBetweenEvictionRunsMillis" value="60000" /> --> |
052 | <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> |
053 | <!-- <property name="minEvictableIdleTimeMillis" value="25200000" /> --> |
054 |
055 |
056 | <!-- 打开removeAbandoned功能 --> |
057 | <!-- <property name="removeAbandoned" value="true" /> --> |
058 | <!-- 1800秒,也就是30分钟 --> |
059 | <!-- <property name="removeAbandonedTimeout" value="1800" /> --> |
060 | <!-- 关闭abanded连接时输出错误日志 --> |
061 | <!-- <property name="logAbandoned" value="true" /> --> |
062 |
063 |
064 | <!-- 监控数据库 --> |
065 | <!-- <property name="filters" value="stat" /> --> |
066 | <!-- <property name="filters" value="mergeStat" /> --> |
067 | <!-- </bean> --> |
068 |
069 |
070 | <!-- 数据源配置, 使用BoneCP数据库连接池 --> |
071 | < bean id = "dataSourceBoncp" class = "com.jolbox.bonecp.BoneCPDataSource" destroy-method = "close" > |
072 | <!-- 数据库驱动 --> |
073 | < property name = "driverClass" value = "${driverClassName}" /> |
074 | <!-- 相应驱动的jdbcUrl,你懂的 --> |
075 | < property name = "jdbcUrl" value = "${jdbc_url}" /> |
076 | <!-- 数据库的用户名 --> |
077 | < property name = "username" value = "${jdbc_username}" /> |
078 | <!-- 数据库的密码 --> |
079 | < property name = "password" value = "${jdbc_password}" /> |
080 | <!-- 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0 |
081 | <property name="idleConnectionTestPeriod" value="240" />--> |
082 | <!-- 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0 |
083 | <property name="idleMaxAge" value="60" />--> |
084 | <!-- 每个分区最大的连接数 --> |
085 | < property name = "maxConnectionsPerPartition" value = "${pool.maxconn}" /> |
086 | <!-- 每个分区最小的连接数 --> |
087 | < property name = "minConnectionsPerPartition" value = "${pool.minconn}" /> |
088 | <!-- 分区数 ,默认值2,最小1,推荐3-4,视应用而定--> |
089 | < property name = "partitionCount" value = "2" /> |
090 | <!-- 每次去拿数据库连接的时候一次性要拿几个,默认值:2 --> |
091 | < property name = "acquireIncrement" value = "2" /> |
092 | <!-- 缓存prepared statements的大小,默认值:0 --> |
093 | < property name = "statementsCacheSize" value = "0" /> |
094 | <!-- 每个分区释放链接助理进程的数量,默认值:3,除非你的一个数据库连接的时间内做了很多工作,不然过多的助理进程会影响你的性能 --> |
095 | < property name = "releaseHelperThreads" value = "3" /> |
096 | < property name = "acquireRetryAttempts" value = "5" /> |
097 | <!-- 其他不常用的属性: |
098 | connectionTestStatement:在做keep-alive的时候的SQL语句。 |
099 | statementsCachedPerConnection:No of statements that can be cached per connection,反正源码中不推荐使用,就别用了. |
100 | initSQL:在每次到数据库取连接的时候执行的SQL语句,只执行一次。 |
101 | closeConnectionWatch:如果设置为true,则会增加一个线程监控关闭连接时的情况,如果关闭时出现异常,则打出错误日志,主要用于debug。上线后记得关掉。 |
102 | logStatementsEnabled:如果设置为true,就会打印执行的SQL语句,如果你用了其他能打印SQL语句的框架,那就不必了。 |
103 | acquireRetryDelay:在获取连接失败后,第二次参试前的延迟时间,默认为7000毫秒。 |
104 | acquireRetryAttempts:在获取连接失败后的重试次数,默认为5次。 |
105 | lazyInit:如果设置为true,那么连接池不会自动创建最小连接数的链接,而是保持为空,直到有需求要获取连接。 |
106 | transactionRecoveryEnabled:如果设置为true,则会保存该链接上的所有活动,以备下次重试的时候使用,这里指的活动是数据库操作。 |
107 | connectionHookClassName:Connection hook class name.没看懂… |
108 | poolName:上面特性中说到的自定义连接池名称。 |
109 | disableJMX:控制JMX的支持开关。 |
110 | connectionTimeout:获取连接的时候最大的等待时间,默认值为:Long.MAX_VALUE |
111 | --> |
112 | </ bean > |
113 |
114 | <!--这里这样配置是因为我用了log4jdbc在开发环境中输出相应的sql语句 便于调试发现错误 --> |
115 | < bean id = "dataSource" class = "net.sf.log4jdbc.Log4jdbcProxyDataSource" > |
116 | < constructor-arg ref = "dataSourceBoncp" /> |
117 | <!-- <property name="logFormatter"> --> |
118 | <!-- <bean class="net.sf.log4jdbc.tools.Log4JdbcCustomFormatter"> --> |
119 | <!-- <property name="loggingType" value="MULTI_LINE" /> --> |
120 | <!-- <property name="margin" value="15" > 这里margin会莫名错误 查看源代码也没发现有什么错误--> |
121 | <!-- <property name="sqlPrefix" value="SQL:::" /> --> |
122 | <!-- </bean> --> |
123 | <!-- </property> --> |
124 | </ bean > |
125 | < bean id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > |
126 | < property name = "dataSource" ref = "dataSource" /> |
127 | </ bean > |
128 |
129 | </ beans > |
第四步 我们来配置springMVC的配置文件spring-mvc.xml(我们 web.xml配置的)文件
文件如下
001 | <? xml version = "1.0" encoding = "UTF-8" ?> |
002 | < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:mvc = "http://www.springframework.org/schema/mvc" 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" xsi:schemaLocation="http://www.springframework.org/schema/beans |
003 | http://www.springframework.org/schema/beans/spring-beans-3.0.xsd |
004 | http://www.springframework.org/schema/context |
005 | http://www.springframework.org/schema/context/spring-context-3.0.xsd |
006 | http://www.springframework.org/schema/mvc |
007 | http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> |
008 |
009 |
010 | < mvc:annotation-driven /> |
011 | <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 --> |
012 | < context:component-scan base-package = "com.cn21.talk.view,com.cn21.admin.controller" /> |
013 | <!-- 处理静态资源 这里是最新版本的配置 也可以用mvc:resources 命名空间 如果你在web,xml配置.do不是"/" 这里可以不写 --> |
014 | < bean id = "simpleUrlHandlerMapping" class = "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" > |
015 | < property name = "urlMap" > |
016 | < map > |
017 | < entry key = "/resources/**" value-ref = "imageResource" /> |
018 | </ map > |
019 | </ property > |
020 | </ bean > |
021 | < bean id = "imageResource" class = "org.springframework.web.servlet.resource.ResourceHttpRequestHandler" > |
022 | < property name = "locations" > |
023 | < list > |
024 | < value >/resources/</ value > |
025 | </ list > |
026 | </ property > |
027 | </ bean > |
028 | < bean id = "httpHandlerAdapter" class = "org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" /> |
029 | <!-- <mvc:resources location="/resources/**" mapping="/resources/"/> --> |
030 | <!-- 由于可能要用到AJAX传送Json字符串 请调用我采用fastJson写的writeJson方法不要采用 原始的@responseBody性能不好--> |
031 | <!-- 避免IE执行AJAX时,返回JSON出现下载文件 --> |
032 |
033 | 这里注意要加json的依赖jackson-mapper-asl |
034 | < bean id = "mappingJacksonHttpMessageConverter" class = "org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" > |
035 | < property name = "supportedMediaTypes" > |
036 | < list > |
037 | < value >text/html;charset=UTF-8</ value > |
038 | </ list > |
039 | </ property > |
040 | </ bean > |
041 | <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> |
042 | < bean class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" > |
043 | < property name = "messageConverters" > |
044 | < list > |
045 | < ref bean = "mappingJacksonHttpMessageConverter" /> <!-- json转换器 --> |
046 | </ list > |
047 | </ property > |
048 | </ bean > |
049 |
050 | < bean id = "viewResolver" |
051 | class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > |
052 | < property name = "viewClass" |
053 | value = "org.springframework.web.servlet.view.JstlView" /> |
054 |
055 | < property name = "prefix" value = "/WEB-INF/jsp/" ></ property > |
056 | < property name = "suffix" value = ".jsp" ></ property > |
057 | </ bean > |
058 |
059 | //文件上传配置 |
060 | < bean id = "multipartResolver" class = "org.springframework.web.multipart.commons.CommonsMultipartResolver" > |
061 | < property name = "defaultEncoding" > |
062 | < value >UTF-8</ value > |
063 | </ property > |
064 | < property name = "maxUploadSize" > |
065 | < value >32505856</ value > <!-- 上传文件大小限制为31M,31*1024*1024 --> |
066 | </ property > |
067 | < property name = "maxInMemorySize" > |
068 | < value >4096</ value > |
069 | </ property > |
070 | </ bean > |
071 | 附上一段freemarker的配置 记得一定要有spring-context-support依赖 |
072 |
073 | <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> |
074 | < bean id = "freemarkerConfig" class = "org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer" > |
075 | < property name = "templateLoaderPath" value = "/WEB-INF/ftl/" /> |
076 | < property name = "defaultEncoding" value = "utf-8" /> |
077 | < property name = "freemarkerSettings" > <!-- 设置FreeMarker环境属性--> |
078 | < props > |
079 | < prop key = "template_update_delay" >60</ prop > <!--刷新模板的周期,单位为秒--> |
080 | < prop key = "default_encoding" >UTF-8</ prop > <!--模板的编码格式 --> |
081 | < prop key = "locale" >zh_CN</ prop > <!-- 本地化设置--> |
082 | </ props > |
083 | </ property > |
084 | </ bean > |
085 | < bean id = "freeResolver" class = "org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver" > |
086 | < property name = "cache" value = "true" /> |
087 | < property name = "suffix" value = ".ftl" /> |
088 | < property name = "contentType" value = "text/html; charset=UTF-8" ></ property > |
089 | < property name = "exposeRequestAttributes" value = "true" /> |
090 | < property name = "exposeSessionAttributes" value = "true" /> |
091 | < property name = "exposeSpringMacroHelpers" value = "true" /> |
092 | |
093 | < property name = "requestContextAttribute" value = "request" /> |
094 | <!-- <property name="viewClass" --> |
095 | <!-- value="com.zyp.view.servlet.MyFreeMarkerView" /> --> |
096 | < property name = "viewClass" |
097 | value = "org.springframework.web.servlet.view.freemarker.FreeMarkerView" /> |
098 | </ bean > |
099 | 注意顺序 freemarker视图处理器需要在 InternalResourceViewResolver |
100 |
101 | 源代码 里面是一个rootHanlder childHanlder 过滤 里面实现了comparor 接口 里面有个 order属性 排序 |
102 |
103 | 拦截器配置 网上有好多 我这里就不详细解释了 我发一段我以前配置的 |
104 |
105 | <!-- 添加springMVC拦截器 用来处理路径 以及 一些例如缓存设置 --> |
106 | <!-- <mvc:interceptors> --> |
107 | <!-- <mvc:interceptor> --> |
108 | <!-- <mvc:mapping path="/**" /> --> |
109 | <!-- <bean class="com.monitor.interceptor.CheckPrivilegeInterceptor"> --> |
110 | <!-- 不需要权限验证的地址 --> |
111 | <!-- <property name="excludeUrls"> --> |
112 | <!-- <list> --> |
113 | <!-- <value>/index.htm</value> --> |
114 | <!-- <value>/user/login.htm</value> --> |
115 | <!-- <value>/user/selfInfoUpdate.htm</value> --> |
116 | <!-- <value>/user/selfInfoList.htm</value> --> |
117 | <!-- <value>/logOut.htm</value> --> |
118 | <!-- <value>/user/selfPassUpdate.htm</value> --> |
119 | <!-- <value>/autoeveryday.htm</value> --> |
120 | <!-- </list> --> |
121 | <!-- </property> --> |
122 | <!-- </bean> --> |
123 | <!-- </mvc:interceptor> --> |
124 | <!-- </mvc:interceptors> --> |
125 | </ beans > |