spring--day02笔记

发布时间 : 星期一 文章spring--day02笔记更新完毕开始阅读

1.5.4 xml配置(重要)

1.5.5 测试

@Test public void demo01(){ //从spring容器获得就是之前目标类,但目标类自动spring进行增强。 String xmlPath = \; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); TeacherService studentService = applicationContext.getBean(\,TeacherService.class); studentService.addTeacher(); studentService.updateTeacher(); } 1.6 aop aspectj 基于xml(重要) 1.6.1 介绍

? ? ? ? ?

AspectJ是一个基于Java语言的AOP框架

Spring2.0以后新增了对AspectJ切点表达式支持

@Aspect 是AspectJ1.5新增功能,通过JDK5注解技术,允许直接在Bean类中定义切面 新版本Spring框架,建议使用AspectJ方式来开发AOP jar介绍

AspectJ是aop框架,支持aop,AspectJ第三方提供规范,spring进行整合。并对规范做出实现。 aop联盟:com.springsource.org.aopalliance-1.0.0.jar ,定义aop规范 spring aop实现:spring-aop-3.2.0.RELEASE.jar

aspectj 规范:com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar spring aspectj实现:spring-aspects-3.2.0.RELEASE.jar

1.6.2 切入点表达式

? 位置:

1.2 修饰服:方法前修饰符。例如:public、private等。可以省略 1.3 返回值:确定方法的返回值

java.lang.String , 要求方法必须返回字符串 void,要求方法没有返回值 * ,返回值任意。

1.4 包,方法所在的包结构。例如:cn.itcast.servcie cn.itcast.servcie , 确定具体的包。 cn.itcast.service.. ,表示cn.itcast.servcie包,及所有子包 cn.itcast.*.servcie ,表示cn.itcast之后存放很多子目录。例如:cn.itcast.user.service /cn.itcast.book.service

综合:cn.itcast.crm.*.service..

1.5 类名,方法所在类 UserService , 具体的类 *Service,表示以Service结尾 User*,表示以User开头 UserService+,表示类必须实现UserService接口

* ,表示任意

1.6 方法,需要被表达式匹配方法名称 saveUser,表示具体方法 save*,表示以save开头方法 *Do,表示以Do结尾方法

* ,表示方法名任意

1.7 参数列表,方法的具体参数 (),表示没有参数 (int) ,表示一个int参数 (java.lang.String) ,表示一个字符串参数 (int , java.lang.String) ,表示两个参数

(..) ,表示参数个数任意

1.8 异常,确定方法抛出的异常。例如:java.lang.RuntimeException 表示允许时异常。可以省略

综合案例:execution(* cn.itcast.crm.*.service..*.*(..))

返回值任意 cn.itcast.crm.包任意.service 当前以及子包.类名任意.方法名称任意(参数任意)

2.within 匹配包, within(cn.itcast..*) 3.this 匹配代理对象 4.target 匹配目标对象

5.args 匹配参数列表。例如:args(int,int)

6.bean 匹配指定的bean。例如:bean(userServiceId) 只匹配指定的bean

1.6.3 AspectJ规定通知类型

?

AspectJ规定6种通知类型:

before:前置通知,目标方法前执行。

afterReturning:后置通知,目标方法后执行。 around:环绕通知,目标方法前后执行。【】 afterThrowing:抛出通知,目标方法发生异常。 after:最终通知,无论什么情况都会执行。

环绕 try{ //前操作 -- before //目标方法 //后操作 -- afterReturning } catch(e){ //异常 -- afterThrowing } finally{ //最终通知 }

联系合同范文客服:xxxxx#qq.com(#替换为@)