zoukankan      html  css  js  c++  java
  • spring3.0学习之环境搭建

    最近搞定了项目,工作终于松了下来,回头整理一下spring学习的资料,记录于此,不足之处,希望大家不吝赐教。关于这一块的讨论,希望大家关注qq讨论群(Spring源码研究群:64606455,综合技术讨论群:178547149)
    言归正传,首先需要下载spring源码包,大家可以自己去spring官网( http://www.springsource.org/)下载最新版本的源码包,我这里直接给出下载地址,直接用迅雷下载即可.
    http://s3.amazonaws.com/dist.springframework.org/release/SPR/spring-framework-3.0.6.RELEASE-with-docs.zip
    http://s3.amazonaws.com/dist.springframework.org/release/SPR/spring-framework-3.0.2.RELEASE-dependencies.zip
    下载以上两个压缩包就可以了,也许有人会问,为什么会有两个zip?spring 3.0.6 是最新发布的版本,但是spring有些依赖包在这个zip中不存在,所以我们需要下载包含依赖包的zip,所以我们下载第二个zip主要是它包含一些依赖包。
    下载完成之后,我们就可以进行环境的搭建了。首先打开myeclipse,新建一个Web工程,首先修改web.xml,在其中加入spring的监听以及配置文件加载信息:
    <context-param>
     
    <param-name>contextConfigLocation</param-name>
     
    <param-value>/WEB-INF/spring*.xml</param-value>
     
    </context-param>
     
    <listener>
     
    <listener-class>
     
    org.springframework.web.context.ContextLoaderListener
     
    </listener-class>
     
    </listener>
    接下来我们来分析一下spring源码包里面的文件,在spring-framework-3.0.6.RELEASE下有四个文件夹:
    dist —— spring的jar包,spring 3.0 的jar按照功能分成多个,我们只需将我们需要的jar拷入到工程即可
    docs —— spring的api以及使用指南,我们在使用中有不懂的地方可以查看相应的文档
    projects —— spring的源码,我们在需要的时候可以查看相应的源码
    src —— 针对于每一个jar文件的源码包,我们可以在myeclipse导入查看jar中的源码
    简单起见,大家将dist下的jar全部拷入工程WEB-INF下的lib下(将来大家熟悉了,再根据相应的功能导入相应的jar即可),下面我们来配置spring的配置文件,在WEB-INF下新建一个名字为spring-config.xml的XML文件,内容如下:
    <?xml version=\"1.0\" encoding=\"UTF-8\"?>
     
     <beans xmlns=\"http://www.springframework.org/schema/beans\"
     
      xmlns:security=\"http://www.springframework.org/schema/security\"
     
      xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
     
      xsi:schemaLocation=\"http://www.springframework.org/schema/beans
     
              http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     
              http://www.springframework.org/schema/security
     
              http://www.springframework.org/schema/security/spring-security-3.0.3.xsd\">
     
     
     
    <
    </beans>
    接下来我们加入数据库连接池,目前常用的连接池有DBCP和 Proxool,这里就简单介绍一下 Proxool连接池的配置,首先下载jar包,这里给出下载地址(http://sourceforge.net/settings/mirror_choices?projectname=proxool&filename=proxool/0.9.1/proxool-0.9.1.zip ),下载之后解压,将lib下的两个jar拷入工程的lib文件夹下。接下来我们在Web-INF下新建一个jdbc.properties文件
    jdbc.driverClassName=com.mysql.jdbc.Driver
    j
    jdbc.url=jdbc:mysql://127.0.0.1:3306/demo
    j
    jdbc.username=root
    j
    jdbc.password=root
    然后我们配置一下spring-config.xml,加入关于连接池的配置
    <bean class=\"org.springframework.beans.factory.config.PropertyPlaceholderConfigurer\">
     
      <property name=\"locations\" value=\"/WEB-INF/jdbc.properties\"/>
     
     </bean>
     
     <bean id=\"dataSource\" class=\"org.logicalcobwebs.proxool.ProxoolDataSource\">
     
      <!--数据源的alias.html' target='_blank'>别名-->
     
      <property name=\"alias\" value=\"demo\"/> 
     
      <!--驱动类-->
     
      <property name=\"driver\" value=\"${jdbc.driverClassName}\"/>
     
      <!--url连接串-->
     
      <property name=\"driverUrl\" value=\"${jdbc.url}\"/>
     
      <!--用户名-->
     
      <property name=\"user\" value=\"${jdbc.username}\"/>
     
      <!--密码-->
     
      <property name=\"password\" value=\"${jdbc.password}\"/>
     
      <!--proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回收,超时的销毁 默认30秒-->
     
      <!--  <property name=\"houseKeepingSleepTime\" value=\"9000\"/>-->
     
      <property name=\"prototypeCount\" value=\"10\"/>
     
      <!--最大连接数(默认5个),超过了这个连接数,再有请求时,就排在队列中等候,最大的等待请求数由maximum-new-connections决定-->
     
      <property name=\"maximumConnectionCount\" value=\"100\"/>
     
      <!--最小连接数(默认2个)-->
     
      <property name=\"minimumConnectionCount\" value=\"10\"/>
     
      <!-- 在Consonl中显示sql -->
     
      <property name=\"trace\" value=\"true\"/>
     
      <property name=\"verbose\" value=\"true\"/>
     
     </bean>
    这些配置好了,现在我们是如何调用呢,我们写个简单的例子,首先新建一个接口
    public interface GenericDAO {
     
     
     
    /**
     
     * 查找指定sql对应的结果集,如果为空,则返回长度为0的List
     
     * 
     
     * @param sql
     
     * @return
     
     */
     
    public List<Map> qureyForList(String sql);
    }
    }
    然后写一个实现类
    public class GenericSpringDAO extends JdbcDaoSupport implements GenericDAO {
     
     
    @Override
     
    public List qureyForList(String sql) {
     
    // TODO Auto-generated method stub
     
    //log.debug(\"选择使用SQL(不带参数):\" + sql);
     
    return getJdbcTemplate().queryForList(sql);
     
    }
    }
    接着我们在spring-config.xml加入bean的配置:
    <bean id=\"genericDAO\" class=\"com.isoft.dao.impl.GenericSpringDAO\">
     
         <property name=\"dataSource\" ref=\"dataSource\" /> 
     
     </bean>
    我们写个测试类:
    public class Test {
     
        public static void main(String[] args) {
     
    ApplicationContext ctx = new ClassPathXmlApplicationContext(\"spring-config.xml\");
     
    GenericDAO genericDAO = (GenericDAO) ctx.getBean(\"genericDAO\");
     
    List list = genericDAO.qureyForList(\"select * from demo\");
     
    System.out.println(list==null);
     
    }
    }
    }
    这样spring jdbc就已经集成进来了,下一次我们在这个基础上加入sturts框架,十分感谢之前大家对我的支持!
  • 相关阅读:
    TCP/IP 协议简介
    制作html的旋转太极图
    Redis高可用
    RFID固定资产管理系统解决方案
    为什么要用三维数据可视化?
    Java单元测试技巧之PowerMock
    技术干货 | 如何在 Library 中使用/依赖 mPaaS?
    云原生的进一步具象化
    【阿里云EMR实战篇】以EMR测试集群版本为例,详解 Flink SQL Client 集成 Hive 使用步骤
    【实践案例】Databricks 数据洞察 Delta Lake 在基智科技(STEPONE)的应用实践
  • 原文地址:https://www.cnblogs.com/huapox/p/3251491.html
Copyright © 2011-2022 走看看