eclipse 搭建spring框架总结

总结一下自己搭建spring环境的过程(工作中用到的spring环境都是老大搭的自己没有动手搭建过),以及中间出现的问题,和需要清楚的一些知识。

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>

解释一下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

<?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>

context:property-placeholder配置当前application.xml所需<property></propery>配置的properties文件加载路径。
有关datasource配置的方法可以参考这篇博文:http://blog.csdn.net/yangyz_love/article/details/8199207
有关spring事务管理,可以学习这篇博文,很优秀!http://blog.csdn.net/java_min/article/details/4427523,同时也推荐这一篇博文:http://blog.csdn.net/edward0830ly/article/details/8703123









  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值