spring 框架 总结

1,加入基础jar包

搭建spring需要一些基础的jar包,这些jar包当中有些是扩展的jar包,比如c3p0的jar包是配置spring datasource时所用到的:

在lib目录下加入这些基础jar包。加入的jar包不正确或这是不兼容,在项目启动时会有许多问题。

2,配置spring

web.xml文件

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  3.         xmlns="http://java.sun.com/xml/ns/javaee"   
  4.         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"   
  5.         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"   
  6.             id="WebApp_ID" version="3.0">  
  7.   <display-name>spring</display-name>  
  8.   <welcome-file-list>  
  9.     <welcome-file>index.html</welcome-file>  
  10.     <welcome-file>index.htm</welcome-file>  
  11.     <welcome-file>index.jsp</welcome-file>  
  12.     <welcome-file>default.html</welcome-file>  
  13.     <welcome-file>default.htm</welcome-file>  
  14.     <welcome-file>default.jsp</welcome-file>  
  15.   </welcome-file-list>  
  16.     
  17.     
  18.   <!-- spring mvc 核心servlet配置 -->  
  19.  <servlet>  
  20.     <servlet-name>applicationContext</servlet-name>  
  21.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  22.     <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下 -->  
  23.     <init-param>  
  24.         <param-name>contextConfigLocation</param-name>  
  25.         <param-value>classpath:/conf/applicationContext-servlet.xml</param-value>  
  26.     </init-param>  
  27.     <load-on-startup>1</load-on-startup>  
  28.  </servlet>  
  29.  <servlet-mapping>  
  30.     <servlet-name>applicationContext</servlet-name>  
  31.     <url-pattern>*.do</url-pattern>  
  32.  </servlet-mapping>  
  33.    
  34.  <!-- spring 配置 -->  
  35.  <listener>  
  36.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  37.  </listener>  
  38.    
  39.  <context-param>  
  40.     <param-name>contextConfigLocation</param-name>  
  41.     <param-value>classpath:/conf/applicationContext.xml</param-value>  
  42.  </context-param>  
  43.   
  44.     
  45. </web-app>  

解释一下classpath。classpath是指/WEB-INF/classes目录,classpath:/conf/applicationContext.xml 等同于 /WEB-INF/classes/conf/applicationContext.xml。
classes目录存放class文件 对应项目开发时src目录编译文件。有时web.xml中也会出现classpath*这样的配置,classpath与classpath*的区别在于: classpath 只会从class文件中查找;classpath*不仅从class文件中查找还会从jar包(class路径)中查找。
有关web.xml配置的详细说明可以参考这篇博文:http://twb.iteye.com/blog/196733

applicationContext.xml

[html]  view plain  copy
  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:aop="http://www.springframework.org/schema/aop"    
  5.     xmlns:tx="http://www.springframework.org/schema/tx"    
  6.     xmlns:context="http://www.springframework.org/schema/context"    
  7.     xsi:schemaLocation="     
  8.           http://www.springframework.org/schema/beans     
  9.           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     
  10.           http://www.springframework.org/schema/tx     
  11.           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    
  12.           http://www.springframework.org/schema/context     
  13.           http://www.springframework.org/schema/context/spring-context-3.0.xsd     
  14.           http://www.springframework.org/schema/aop     
  15.           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName">  
  16.            
  17.        
  18.      <context:property-placeholder location="classpath:application.properties"/>  
  19.        
  20.      <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">  
  21.         <property name="driverClass" value="${mysql.driverclass}"></property>  
  22.         <property name="jdbcUrl" value="${mysql.jdbcurl}"></property>  
  23.         <property name="user" value="${mysql.user}"></property>  
  24.         <property name="password" value="${mysql.password}"></property>  
  25.         <property name="acquireIncrement" value="5"></property>  <!-- 当连接池中的连接用完时,C3P0一次性创建新连接的数目2 -->  
  26.         <property name="initialPoolSize" value="10"></property>  <!-- 初始化时创建的连接数,必须在minPoolSize和maxPoolSize之间 -->  
  27.         <property name="minPoolSize" value="5"></property>  
  28.         <property name="maxPoolSize" value="20"></property>  
  29.         <!-- 最大空闲时间,超过空闲时间的连接将被丢弃  
  30.         [需要注意:mysql默认的连接时长为8小时(28800)【可在my.ini中添加 wait_timeout=30(单位秒)设置连接超时】,这里设置c3p0的超时必须<28800]   
  31.         -->  
  32.         <property name="maxIdleTime" value="300"></property>    
  33.         <property name="idleConnectionTestPeriod" value="60"></property> <!-- 每60秒检查连接池中的空闲连接 -->  
  34.         <property name="maxStatements" value="20"></property>  <!-- jdbc的标准参数  用以控制数据源内加载的PreparedStatement数量,但由于预缓存的Statement属 于单个Connection而不是整个连接 -->  
  35.      </bean>  
  36.        
  37.      <!-- 事务管理器配置 -->  
  38.      <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  39.         <property name="dataSource" ref="dataSource"></property>  
  40.      </bean>  
  41.      <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>  
  42.       
  43. </beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值