tomcat 8、Spring 4.X、JDK1.8
需要jar包:
1)日志组件:log4j
1 # debug < info < warn < error 2 log4j.rootLogger=warn, console, file 3 4 ###################console#################### 5 log4j.appender.console=org.apache.log4j.ConsoleAppender 6 log4j.appender.console.layout=org.apache.log4j.PatternLayout 7 log4j.appender.console.layout.ConversionPattern=%d{yyy-MMM-dd HH:mm:ss} %p %m %L %n 8 9 ###################file###################### 10 log4j.appender.file=org.apache.log4j.RollingFileAppender 11 log4j.appender.file.File=../logs/SpuerPPP.log 12 log4j.appender.file.MaxFileSize=5kb 13 log4j.appender.file.MaxBackupIndex=100 14 log4j.appender.file.layout=org.apache.log4j.PatternLayout 15 log4j.appender.file.layout.ConversionPattern=%d %c.%M()-%m%n
2)web.xml:注册spring组件为项目启动时加载
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> 3 <welcome-file-list> 4 <welcome-file>index.jsp</welcome-file> 5 </welcome-file-list> 6 <servlet> 7 <servlet-name>springMVC</servlet-name> 8 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 9 <init-param> 10 <param-name>contextConfigLocation</param-name> 11 <param-value>classpath:spring.xml</param-value> 12 </init-param> 13 <load-on-startup>1</load-on-startup> 14 </servlet> 15 <servlet-mapping> 16 <servlet-name>springMVC</servlet-name> 17 <url-pattern>*.do</url-pattern> 18 </servlet-mapping> 19 <filter> 20 <filter-name>CharacterEncodingFilter</filter-name> 21 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 22 <init-param> 23 <param-name>encoding</param-name> 24 <param-value>UTF-8</param-value> 25 </init-param> 26 </filter> 27 <filter-mapping> 28 <filter-name>CharacterEncodingFilter</filter-name> 29 <url-pattern>/*</url-pattern> 30 </filter-mapping> 31 </web-app>
3)spring.xml:整合
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:p="http://www.springframework.org/schema/p" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:aop="http://www.springframework.org/schema/aop" 7 xmlns:tx="http://www.springframework.org/schema/tx" 8 xmlns:mvc="http://www.springframework.org/schema/mvc" 9 10 xsi:schemaLocation=" 11 http://www.springframework.org/schema/beans 12 http://www.springframework.org/schema/beans/spring-beans.xsd 13 http://www.springframework.org/schema/context 14 http://www.springframework.org/schema/context/spring-context.xsd 15 http://www.springframework.org/schema/aop 16 http://www.springframework.org/schema/aop/spring-aop.xsd 17 http://www.springframework.org/schema/tx 18 http://www.springframework.org/schema/tx/spring-tx.xsd 19 http://www.springframework.org/schema/mvc 20 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 21 22 <!-- 配置数据源信息-c3p0 --> 23 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 24 <property name="driverClass" value="com.mysql.jdbc.Driver"></property> 25 <property name="jdbcUrl" value="jdbc:mysql://47.93.197.247:3306/SuperPPP"></property> 26 <property name="user" value="yyq"></property> 27 <property name="password" value="yyq"></property> 28 <property name="initialPoolSize" value="5"></property> 29 <property name="maxPoolSize" value="10"></property> 30 <property name="maxStatements" value="100"></property> 31 <property name="acquireIncrement" value="2"></property> 32 </bean> 33 34 <!-- DBUtils的queryRunner注入 --> 35 <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype"> 36 <constructor-arg name="ds" ref="dataSource"/> 37 </bean> 38 39 <!-- 组件扫描 --> 40 <context:component-scan base-package="henu.superppp" /> 41 42 <!-- 事务管理器 --> 43 <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 44 <property name="dataSource" ref="dataSource"/> 45 </bean> 46 <!-- 注解式事务管理 --> 47 <tx:annotation-driven transaction-manager="txManager"/> 48 49 <!-- aop注解方式 --> 50 <aop:aspectj-autoproxy/> 51 52 <!-- MVC相关配置 --> 53 <!-- 拦截器,进行轻量级访问控制 --> 54 55 <!-- 文件上传 --> 56 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 57 <property name="defaultEncoding" value="UTF-8"/> 58 <property name="maxUploadSize" value="10000000"/> 59 </bean> 60 61 <!-- 处理请求返回json字符串的中文乱码问题 以及映射器、适配器和试图解析器的自动配置-->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven> 86 87 <!-- 解析器 --> 88 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 89 <property name="prefix" value="/WEB-INF/jsps/"/> 90 <property name="suffix" value=".jsp"/> 91 </bean> 95 </beans>