1,加入基础jar包
搭建spring需要一些基础的jar包,这些jar包当中有些是扩展的jar包,比如c3p0的jar包是配置spring datasource时所用到的:
在lib目录下加入这些基础jar包。加入的jar包不正确或这是不兼容,在项目启动时会有许多问题。
2,配置spring
web.xml文件
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
- id="WebApp_ID" version="3.0">
- <display-name>spring</display-name>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- <welcome-file>index.htm</welcome-file>
- <welcome-file>index.jsp</welcome-file>
- <welcome-file>default.html</welcome-file>
- <welcome-file>default.htm</welcome-file>
- <welcome-file>default.jsp</welcome-file>
- </welcome-file-list>
- <!-- spring mvc 核心servlet配置 -->
- <servlet>
- <servlet-name>applicationContext</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下 -->
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:/conf/applicationContext-servlet.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>applicationContext</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- <!-- spring 配置 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:/conf/applicationContext.xml</param-value>
- </context-param>
- </web-app>
classes目录存放class文件 对应项目开发时src目录编译文件。有时web.xml中也会出现classpath*这样的配置,classpath与classpath*的区别在于: classpath 只会从class文件中查找;classpath*不仅从class文件中查找还会从jar包(class路径)中查找。
有关web.xml配置的详细说明可以参考这篇博文:http://twb.iteye.com/blog/196733
applicationContext.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:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName">
- <context:property-placeholder location="classpath:application.properties"/>
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
- <property name="driverClass" value="${mysql.driverclass}"></property>
- <property name="jdbcUrl" value="${mysql.jdbcurl}"></property>
- <property name="user" value="${mysql.user}"></property>
- <property name="password" value="${mysql.password}"></property>
- <property name="acquireIncrement" value="5"></property> <!-- 当连接池中的连接用完时,C3P0一次性创建新连接的数目2 -->
- <property name="initialPoolSize" value="10"></property> <!-- 初始化时创建的连接数,必须在minPoolSize和maxPoolSize之间 -->
- <property name="minPoolSize" value="5"></property>
- <property name="maxPoolSize" value="20"></property>
- <!-- 最大空闲时间,超过空闲时间的连接将被丢弃
- [需要注意:mysql默认的连接时长为8小时(28800)【可在my.ini中添加 wait_timeout=30(单位秒)设置连接超时】,这里设置c3p0的超时必须<28800]
- -->
- <property name="maxIdleTime" value="300"></property>
- <property name="idleConnectionTestPeriod" value="60"></property> <!-- 每60秒检查连接池中的空闲连接 -->
- <property name="maxStatements" value="20"></property> <!-- jdbc的标准参数 用以控制数据源内加载的PreparedStatement数量,但由于预缓存的Statement属 于单个Connection而不是整个连接 -->
- </bean>
- <!-- 事务管理器配置 -->
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"></property>
- </bean>
- <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
- </beans>