




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、struts2+hibernate+springjavaee 企业级项目开发单元六 考勤管理任务2 考勤查询设计1任务简介任务简介2任务分析任务分析3相关支撑知识相关支撑知识4任务小结任务小结目录页第1页任务2 考勤查询设计过渡页第2页过渡页任务简介任务2 考勤查询设计任务简介本本任务以考勤查询功能设计与实现为依托,主要内容:p学习spring aop中的pointcut与advisor;p理解aop基于xml schema的配置实现;p应用xml schema配置设计并实现考勤查询子模块。第3页过渡页第4页过渡页任务分析任务2 考勤查询设计任务分析pspring中的pointcut:l定义了
2、advice的应用时机,相对任务1中的四种advice,pointcut可以定义更细致的植入时机。l分为静态pointcut、动态pointcut和用户自定义pointcut三种。p本任务重点:l分析静态pointcut与动态pointcut。l基于xml schema的配置实现方式可以简化aop的使用,无需编写代理类,可以重用pointcut。第5页任务2 考勤查询设计任务分析p考勤查询:l根据用户名、开始日期、结束日期查询符合条件的考勤记录。l考勤查询同时记录查询日志信息,查询日志信息包括:用户的id、ip地址、查询时间等。l查询日志应用基于xml schema配置,在spring 配置文
3、件中配置标签,配置文件中没有设置代理对象,spring自动设置代理对象并调用对应的方法是应用advice。第6页过渡页第7页过渡页相关支撑知识相关支撑知识ppointcut即切入点,用于配置切面的切入位置:lspring切入点的粒度是方法级,因此在spring aop中pointcut的作用是配置哪些类中哪些方法在定义的切入点之内、哪些方法应该被过滤排除。lspring的pointcut分为三种:静态pointcut:需要考虑类名、方法名动态pointcut:除此之外,还要考虑方法的参数,以便在运行时可以动态的确定切入点的位置; 用户自定义pointcut。第8页一pointcut、advis
4、ior任务2 考勤查询设计相关支撑知识pspring中一个advisor是一个通知对象和与之关联的切入点表达式的切面。除了引入这种特殊形式,任何advisor都可以和任何通知一起工作。p org.springframework.aop.support.defaultpointcutadvisor是最常用的advisor类。l例如,可以和 methodinterceptor,beforeadvice 或者 throwsadvice一起使用。第9页一pointcut、advisior任务2 考勤查询设计相关支撑知识p 静态pointcut:l 可以根据类和方法的签名,来判定那些类的哪些方法在定义的
5、切入点之内、哪些应该被过滤排除。p 静态pointcut的实现类:l namematchmethodpointcut:只能对方法名进行判别的静态pointcut实现类;l jdkregexpmethodpointcut:使用jdk中定义的正则表达式对方法名进行匹配的静态pointcut实现类;l staticmethodmatcherpointcut:抽象的静态pointcut,不能被实例化,可以扩展该类来实现自定义的切入点。第10页静态pointcut1 1一pointcut、advisior任务2 考勤查询设计相关支撑知识第11页静态pointcut1 1一pointcut、advisio
6、r任务2 考勤查询设计(1)namematchmethodpointcutl 只能对方法名进行判别的静态pointcut实现类 pos* start pos*表示包含所有以pos开始的方法(大小写敏感)。相关支撑知识第12页静态pointcut1 1一pointcut、advisior任务2 考勤查询设计(1)namematchmethodpointcutl 使用namematchmethodpointcut匹配foo(),foo(int)和bar()方法。【例6-5】namematchmethodpointcut使用实例接口:inamebean.javapublic interface in
7、amebean public void foo(); public void foo(int x);public void bar(); public void yup();接口实现类:namebean.javapublic class namebean implements inamebean public void foo() system.out.println(foo); public void foo(int x) system.out.println(foo + x); public void bar() system.out.println(bar); public void y
8、up() system.out.println(yup); 相关支撑知识第13页静态pointcut1 1一pointcut、advisior任务2 考勤查询设计(1)namematchmethodpointcutl 使用namematchmethodpointcut匹配foo(),foo(int)和bar()方法。【例6-5】namematchmethodpointcut使用实例修改配置文件 foo bar nameadvisor 相关支撑知识第14页静态pointcut1 1一pointcut、advisior任务2 考勤查询设计(1)namematchmethodpointcutl 使用
9、namematchmethodpointcut匹配foo(),foo(int)和bar()方法。【例6-5】namematchmethodpointcut使用实例测试类:namepointcutexample.javapublic class namepointcutexample public static void main(string args) applicationcontext context = new classpathxmlapplicationcontext(applicationcontext.xml);inamebean namebeanproxy=(inamebea
10、n)context.getbean(namebeanproxy);namebeanproxy.foo();namebeanproxy.foo(100);namebeanproxy.bar();namebeanproxy.yup(); 相关支撑知识第15页静态pointcut1 1一pointcut、advisior任务2 考勤查询设计(1)namematchmethodpointcutl 使用namematchmethodpointcut匹配foo(),foo(int)和bar()方法。【例6-5】namematchmethodpointcut使用实例运行结果相关支撑知识第16页静态point
11、cut1 1一pointcut、advisior任务2 考勤查询设计(2)jdkregexpmethodpointcutl 使用jdk中定义的正则表达式对方法名进行匹配的静态pointcut。 .*ost .*tart .*tart .*ost正则表达式,所有以ost结尾的方法都匹配。相关支撑知识第17页静态pointcut1 1一pointcut、advisior任务2 考勤查询设计(2)jdkregexpmethodpointcutp patterns属性:l 一个java.lang.sring类型的数组,表示数组中所有和定义的正则表达式匹配的方法都包含在定义的切入点内。p exclude
12、dpatterns属性:l 与patterns属性相反,表示被排除于切入点范围的方法。p 和namematchmethodpointcut一样,也可以用classfilter类型的classfilter属性来定义类过滤器。 相关支撑知识第18页静态pointcut1 1一pointcut、advisior任务2 考勤查询设计(2)jdkregexpmethodpointcutpublic class people public void speak() / 讲话 system.out.println(hello,我是people!); public void running() / 跑步 sy
13、stem.out.println(我在跑跑逃); public void loving() / 恋爱 system.out.println(我在恋爱别来打搅我!); public void died() / 死亡 system.out.println(完了,死了); 【例6-6】jdkregexpmethodpointcut使用实例 创建目标类:people.java相关支撑知识第19页静态pointcut1 1一pointcut、advisior任务2 考勤查询设计(2)jdkregexpmethodpointcutpublic class logerpeople implements me
14、thodbeforeadvice private static final log log = logfactory.getlog(logerpeople.class); public void before(method method, object args, object target) throws throwable system.out.println(target.getclass().getsimplename() + 正在 + method.getname()+ !); 【例6-6】jdkregexpmethodpointcut使用实例创建一个类名:logerpeople.j
15、ava实现methodbeforeadvice接口相关支撑知识第20页静态pointcut1 1一pointcut、advisior任务2 考勤查询设计(2)jdkregexpmethodpointcutpublic class testmain public static void main(string args) applicationcontext context = new filesystemxmlapplicationcontext( src/com/my/test/spring.xml); /通过proxyfactorybean获取icomputer接口实现类的实例 peopl
16、e people = (people) context.getbean(proxyfactorybean); people.speak(); people.running(); people.loving(); people.died(); 【例6-6】jdkregexpmethodpointcut使用实例创建含主方法的测试类testmain.java相关支撑知识第21页静态pointcut1 1一pointcut、advisior任务2 考勤查询设计(2)jdkregexpmethodpointcut .*spea.* .*ing .*di.* 【例6-6】jdkregexpmethodpo
17、intcut使用实例编写spring配置文件 defaultadvisor l“.*spea.*”表示所有名字以spea开头的方法,例子中是指speak方法;l“.*ing”表示所有名字以ing结束的方法,例子中是指running和loving方法;l“.*di.*”表示所有名字以di开头的方法,例子中是指died方法;l“.*run.*”表示所有名字以run开头的方法,例子中是指running方法。相关支撑知识第22页静态pointcut1 1一pointcut、advisior任务2 考勤查询设计(2)jdkregexpmethodpointcut【例6-6】jdkregexpmethod
18、pointcut使用实例运行结果l people类中的speak、loving、died方法已经被拦截;l 但running方法却没有拦截,因为在jdkregexpmethodpointcut中指定其excludedpattern属性把它排除在切入点之外的缘故。相关支撑知识p spring中提供了以下几种动态切入点的实现:l controlflowpointcut:控制流程切入点 如只有在某个特定的类或方法中调用某个连接点时,装备才会被触发,这时就可以使用controlflowpointcut; 它的系统开销很大,在追求高效的应用中,不推荐使用。l dynamicmethodmatcherpo
19、intcut:动态方法匹配器 是抽象类,扩展该类可以实现自己的动态pointcut。第23页动态pointcut2 2一pointcut、advisior任务2 考勤查询设计相关支撑知识第24页动态pointcut2 2一pointcut、advisior任务2 考勤查询设计(1)dynamicmethodmatcherpointcut【例6-7】dynamicmethodmatcherpointcut使用实例l 通知foo()方法并且仅仅希望当传递给方法int参数等于100时对方法进行通知 接口:isamplebean.javapublic interface isamplebean pub
20、lic void foo(int x); public void bar(); 接口实现类:samplebean.javapublic class samplebean implements isamplebean public void foo(int x) system.out.println(invoked foo() with: + x); public void bar() system.out.println(invoked bar(); 相关支撑知识第25页动态pointcut2 2一pointcut、advisior任务2 考勤查询设计(1)dynamicmethodmatch
21、erpointcut【例6-7】dynamicmethodmatcherpointcut使用实例l 通知foo()方法并且仅仅希望当传递给方法int参数等于100时对方法进行通知动态切入点类:simpledynamicpointcut.javapublic class simpledynamicpointcut extends dynamicmethodmatcherpointcut public boolean matches(method method, class cls) system.out.println(static check for + method.getname();re
22、turn (foo.equals(method.getname();public boolean matches(method method, class cls, object args) system.out.println(dynamic check for + method.getname();int x = (integer) args0;return (x = 100);public classfilter getclassfilter() return new classfilter() public boolean matches(class cls) return (cls
23、= samplebean.class);相关支撑知识第26页动态pointcut2 2一pointcut、advisior任务2 考勤查询设计(1)dynamicmethodmatcherpointcut【例6-7】dynamicmethodmatcherpointcut使用实例 修改配置文件 simplebeanadvisor 相关支撑知识第27页动态pointcut2 2一pointcut、advisior任务2 考勤查询设计(1)dynamicmethodmatcherpointcut【例6-7】dynamicmethodmatcherpointcut使用实例运行结果相关支撑知识第28页
24、二基于xml schema的设置任务2 考勤查询设计p spring 3.0中,除了传统的通过实现aop api的方式来实现advice之外,还提供了两种更加简便的方式来实现advice:l before advice:基于xml schemal after advice:基于xml sechmap 采用上述两种方式,advice不用实现特定的接口。相关支撑知识第29页brfore advice:基于xml schema1 1二基于xml schema的设置任务2 考勤查询设计(1)before advice【例6-8】基于xml schema的before advice实例l 当基于xml
25、schema实现before advice时, advice类不用实现methodbeforeadvice接口。 接口:ihello.javapublic interface ihello public void hello(string name); 接口实现类:hellospeaker.javapublic class hellospeaker implements ihello public void hello(string name) system.out.println(hello,+name);相关支撑知识第30页brfore advice:基于xml schema1 1二基于x
26、ml schema的设置任务2 考勤查询设计(1)before advice【例6-8】基于xml schema的before advice实例 logaspect .javapublic class logaspect private logger logger=logger.getlogger(this.getclass().getname(); public void before(joinpoint jointpoint) logger.log(level.info, method starts. + jointpoint.getsignature().getdeclaringtype
27、name() + . + jointpoint.getsignature().getname(); before方法:l是在目标对象上的方法被执行前要执行的方法;ljoinpoint参数是可选项,可以根据需要决定是否需要joinpoint参数,通过joinpoint对象,可以获得目标对象(gettarget())、目标方法上的参数(getargs())等信息。相关支撑知识第31页brfore advice:基于xml schema1 1二基于xml schema的设置任务2 考勤查询设计(1)before advice【例6-8】基于xml schema的before advice实例 配置文
28、件:beans-config.xml l 使用基于xml sechma声明aop,要在xml中加入aop的名称空间;l 所有的aop都是在标签中声明的;l 用于定义advice实例;l 表示当前实例用于实现before advice;l pointcut属性用于指定pointcut表示式;l method属性表示advice上要调用的方法。相关支撑知识第32页brfore advice:基于xml schema1 1二基于xml schema的设置任务2 考勤查询设计(1)before advice【例6-8】基于xml schema的before advice实例 测试类:testclass
29、.javapublic class testclass public static void main(string args) applicationcontext context = new classpathxmlapplicationcontext(beans-config.xml); try ihello hellospeaker=(ihello) context.getbean(hellospeaker); hellospeaker.hello(jack); catch(exception e) e.printstacktrace(); 相关支撑知识第33页brfore advic
30、e:基于xml schema1 1二基于xml schema的设置任务2 考勤查询设计(1)before advice【例6-8】基于xml schema的before advice实例运行结果相关支撑知识第34页after advice:基于xml schema2 2二基于xml schema的设置任务2 考勤查询设计(2)after advice【例6-9】基于xml schema的after advice实例l 与before advice一样,基于xml sechma实现after returning advice时,不再需要org.springframework.aop.afterreturningadvice接口。 修改logaspect类,在其中增加after方法public void after(joinpoint jointpoint) logger.log(level.info, method ends. + jointpoint.getsignature().getdeclaringtypename() + . + jointpoint.getsignature().getname(); 修改配置文件接口与接口实现类与例6-8相同相关支撑知识第35页after advice:基于xml schema2 2二基于xml sche
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2030年中国数字出版行业市场运营动态调研与发展建议咨询报告
- 电子商务师中级模拟考试题与参考答案
- 北京市一七一中学2025年高考适应性考试英语试卷含解析
- 验光员测试题(含答案)
- 车工高级工练习题(附参考答案)
- 职业技术学院2024级证券实务专业人才培养方案
- 2025年海南省海口九中等学校联考中考数学一模试题(原卷版+解析版)
- 院感爆发处置规范理论考核试题
- 游乐设施施工项目成本效益分析考核试卷
- 畜牧业养殖废弃物处理政策效果与优化建议考核试卷
- 牛津译林7A-Unit3、4单元复习
- 国家义务教育质量监测初中美术试题
- 超声波探伤作业指导书
- 课程思政视域下小学音乐教学策略初探 论文
- 智能高速铁路概论-课件-第一章-世界智能铁路发展-
- 群众性战伤救治技术知识考试题库-下(多选、判断题部分)
- 黑龙江佳木斯旅游介绍PPT模板
- 中国传统文化之中国古代科技PPT
- 心力衰竭护理业务查房
- 2023部编七年级下册语文生字词总汇
- 黑布林英语阅读(初一年级第1辑套装共6册)赤诚之心翻译
评论
0/150
提交评论