最新讲义Spring3.docx
- 文档编号:4770408
- 上传时间:2022-12-08
- 格式:DOCX
- 页数:19
- 大小:19.40KB
最新讲义Spring3.docx
《最新讲义Spring3.docx》由会员分享,可在线阅读,更多相关《最新讲义Spring3.docx(19页珍藏版)》请在冰豆网上搜索。
最新讲义Spring3
Spring持久层的封装
通过xml实现DataSource数据源的注入有3种方式:
1:
使用spring自带的DriverManagerDataSource
mysql:
//localhost:
3306/newdb
--
创建jdbc.properties属性文件放在src下:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:
mysql:
//localhost:
3306/bbb
jdbc.username=root
jdbc.password=
jdbc.properties
-->
--声明式事务处理-->
--对create方法进行事务管理,PROPAGATION_REQUIRED表示如果没有事务就新建一个事务-->
2:
使用DBCP连接池commons-pool-1.2.jarcommons-collections.jar
mysql:
//localhost:
3306/newdb
--声明式事务处理-->
--对create方法进行事务管理,PROPAGATION_REQUIRED表示如果没有事务就新建一个事务-->
3:
使用Tomcat提供的JNDI(部署到服务器上通过页面访问)
name="jdbc/oracle" type="javax.sql.DataSource" driverClassName="oracle.jdbc.driver.OracleDriver" password="ascent" maxIdle="2" maxWait="-1" username="ascent" url="jdbc: oracle: thin: @localhost: 1521: orcl" maxActive="4"/> comp/env/jdbc/oracle --声明式事务处理--> --对create方法进行事务管理,PROPAGATION_REQUIRED表示如果没有事务就新建一个事务--> --------------------------------------------------------------------------------------------------------- Spring定时器 定时器的两种实现方式: 1: Java的java.util.Timer类 2: OpenSymphony的Quartz 步骤: 1: 创建定时任务,2: 运行定时任务。 而运行定时任务分为,一是程序直接启动,二是web监听 一、是程序直接启动 /**通过jsp页面来启动下面方法的执行在jsp页面中嵌入 <% Timertimer=newTimer(); timer.schedule(newMyTask(),10000,1000); %>*/ publicclassMyTaskextendsTimerTask{ publicvoidrun(){ System.out.print("Iamruning"); } } publicclassMain{ publicstaticvoidmain(String[]args){ Timertimer=newTimer(); timer.schedule(newMyTask(),10000,1000); } } 二、是web监听 /**只要启动Tomcat就可以执行其功能*/ publicclassMyTaskextendsTimerTask{ publicvoidrun(){ System.out.print("Iamruning"); } } publicclassBindLoaderimplementsServletContextListener{ Timertimer=newTimer(); publicvoidcontextInitialized(ServletContextEventarg0){ timer.schedule(newMyTask(),10000,1000); } publicvoidcontextDestroyed(ServletContextEventarg0){ timer.cancel(); } } web.xml: BindLoader --------------------------------------------------------------------------------- Spring定时任务开发: publicclassMyTaskextendsTimerTask{ publicvoidrun(){ System.out.print("Iamruning"); } } applicationContext.xml: --注册定时实体--> --注册定时信息--> --指定推迟时间--> --指定重复时间--> --执行具体任务--> --配置调度器,这个任务我们只能规定每隔24小时运行一次,无法精确到某时启动--> --指定定时器列表--> web.xml: applicationContext.xml org.springframework.web.context.ContextLoaderListener ============================================================================== 2: OpenSymphony的Quartz Spring对Quartz提供的支持 Quartz为任务调度提供了大量的功能,比如在何时执行何种任务。 Quartz是OpenSymphony的OpenSource项目,供开发任务调度应用使用。 开发者能够在J2EE或单独的J2SE应用中使用它。 驱动工作流: 比如,如果新创建的工作流程任务需要在2个小时内处理完,则在2小时后Quartz会检查订单是否处理完成。 如果没有处理,则Quartz会依据工作流定义的规则来对订单处理。 系统维护工作: 比如,在每个工作日的固定时间将RDBMS中的数据导出XML格式文件。 Spring对Quartz提供的了一流的集成支持,提供如下类: QuartzJobBean: 实现了Job接口,同TimerTask类似,用于定义任务本身。 executeInternal()方法定义了执行任务内容。 JobDetailBean: Quartz中JobDetail的子类,为了简化开发。 SimpleTriggerBean: Quartz中SimpleTrigger类的子类,同Timer中ScheduledTimerTask类似。 CronTirggerBean: Quartz中CronTrigger的子类,比SimpleTriggerBean功能强大,能够控制任务执行的精确时间。 MethodInvokingJobDetailFactoryBean: 同Timer中的MethodInvokingTimerFactoryBean类似。 SchedulerFactoryBean: 同TImerFactoryBean等效,借于它启动定时器。 实例一: packagecom.openv.spring; importjava.util.logging.Logger; importorg.quartz.JobExecutionContext; importorg.quartz.JobExecutionException; importorg.springframework.scheduling.quartz.QuartzJobBean; publicclassLogJobBeanextendsQuartzJobBean{ Loggerlogger=Logger.getLogger(LogJobMethodBeanTest.class.getName()); privateinttimeout; /** *设置超时时间 *@paramtimeoutthetimeouttoset */ publicvoidsetTimeout(inttimeout){ logger.info("setTimeout()......"); this.timeout=timeout; } @Override protectedvoidexecuteInternal(JobExecutionContextarg0) throwsJobExecutionException{ logger.info("CreateLogJobBean......"); } } packagecom.openv.spring; importorg.quartz.Scheduler; importorg.quartz.SchedulerException; importorg.springframework.beans.factory.BeanFactory; importorg.springframework.beans.factory.xml.XmlBeanFactory; importorg.springframework.core.io.ClassPathResource; importorg.springframework.core.io.Resource; publicclassLogJobBeanTest{ publicstaticvoidmain(String[]args){ Resourceresource=newClassPathResource("applicationContext.xml"); BeanFactoryfactory=newXmlBeanFactory(resource); Schedulersfb=(Scheduler)factory.getBean("sfb"); try{ Thread.sleep(20000); }catch(InterruptedExceptione){ } try{ sfb.shutdown(); }catch(SchedulerExceptione){ } } } xmlversion="1.0"encoding="UTF-8"? > xmlns="http: //www.springframework.org/schema/beans" xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" xmlns: p="http: //www.springframework.org/schema/p" xsi: schemaLocation="http: //www.springframework.org/schema/beanshttp: //www.springframework.org/schema/beans/spring-beans-2.0.xsd">
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 最新 讲义 Spring3
![提示](https://static.bdocx.com/images/bang_tan.gif)