spring入门2
来源:互联网 发布:电脑无线mac地址查询 编辑:程序博客网 时间:2024/06/03 10:33
1. aop : 分为 JDK动态代理 无耦合 cglib动态代理; 是一个开源项目 ,code生成类库. 不需要前提条件 有耦合
2. spring 自动选择 使用哪个动态机制, 有接口用动态代理, 无接口用cglib;
3. spring整合aspectj 框架实现的aop *
4.五种通知, 前置 ,后置 ,环绕 ,异常抛出通知, 引介通知
5.spring 配置文件拆分, <import resource="导入其他部分的配置文件">
6. aspectj 切点 传统开发, 可以使用<aop:xxx> 来简化操作.
1.导入名称空间. 两个bean, 目标和通知
2.<aop:config>自动代理 <aop: pointcut expression="excution()" id=""> 声明切点
<aop: advisor adivce-ref="" pointcut-ref=""> 切面, 只能包含该一个切点和一个增强,
<aop:aspect> 定义aspectj 框架的切面. 他可以包含多个切点和多个通知.
7.1.execution() 用于描述方法 【掌握】
语法:execution(修饰符 返回值 包.类.方法名(参数) throws异常)
修饰符,一般省略
public 公共方法
* 任意
返回值,不能省略
void 返回没有值
String 返回值字符串
* 任意
包,[省略]
com.itheima.crm 固定包
com.itheima.crm.*.service crm包下面子包任意 (例如:com.itheima.crm.staff.service
com.itheima.crm.. crm包下面的所有子包(含自己)
com.itheima.crm.*.service.. crm包下面任意子包,固定目录service,service目录任意包
类,[省略]
UserServiceImpl 指定类
*Impl 以Impl结尾
User* 以User开头
* 任意
方法名,不能省略
addUser 固定方法
add* 以add开头
*Do 以Do结尾
* 任意
(参数)
() 无参
(int) 一个整型
(int ,int) 两个
(..) 参数任意
throws ,可省略,一般不写。
综合1
execution(* com.itheima.crm.*.service..*.*(..))
综合2
<aop:pointcut expression="execution(* com.itheima.*WithCommit.*(..)) ||
execution(* com.itheima.*Service.*(..))" id="myPointCut"/>
7. aspectj 框架实现 aop aspect: 切面=切点+通知(多个切点和多个通知的组合) aspectj: 第三方框架,可以使用部分语法
定义通知类型6种, 前置,后置,环绕,抛出,引介,最终通知(不过是否有异常,都会执行.)
8. 在 aspectj 中它的增强可以不实现任何接口,只需要定义出增强功能
xml文件中: <aop:config> <aop:aspect ref="通知"> <aop:before method="">
<aop:after-throwing method="afterThrowing" pointcut-ref="delPointCut" throwing="ex"/>
环绕通知方法 public object around(proceedingjoinpoint pjp){ pjp.proceed(); }
异常抛出通知 public void afterThrowing(JoinPoint jp,Throwable ex) {
System.out.println("发现了异常。。。。"+ex)
9. 关于各个通知的参数
前置通知:参数 joinpoint jp, jp.getSignature().getDeclaringTypeName() 获取目标类名
后置通知: 参数 joinpoint jp, object val 要在xml中配置 returning="val" 注意参数名要一致
环绕通知 : 参数 proceedingjoinpoint pjp, Object obj = pjp.proceed() 执行目标行为
异常抛出通知: 参数 JoinPoint jp ,Throwable ex 在xml中 throwing="ex"
最终通知: after(JoinPoint jp ) jp.getSignature().getName() 方法名;
10.环绕通知:日志操作,权限校验, 性能监控,事物管理 最终通知: 资源释放.
11. 关于代理方式的选择: 在xml中 <aop:cofig proxy-target-class的值默认为false,代表有接口使用proxy >
12 : 注解开发: <context:component-scan base-pakeage> 扫描指定包下的注解;
开启aspectj注解自动代理功能: <aop:aspectj-autoproxy/>
通知中: 类上面: @component @aspect 声明当前bean是一个切面 方法上面: @before("execution(表达式)")
后置通知
@AfterReturning(value = "execution(* *.update(..))", returning = "value")
环绕通知: @around("execution(* *.update(..))")
异常通知: @afterthrowing(value = "execution(* *.update(..))", throwing = "ex")
最终通知: @after("execution(* *.update(..))")
13. 创建无参数方法: 在参数上面: @pointcut("execution(* *.update(..)")
最终通知: @after("方法名() || 方法名2()")
14. 选择代理方式: 在xml中 <aop: aspe...... proxy-target-class=false>
阅读全文
0 0
- Spring-----2、Spring入门
- Spring AOP 入门(2)
- (2)Spring 核心技术入门
- Spring MVC入门2
- spring boot 入门 (2)
- spring入门2
- spring boot入门-2
- 2、spring入门—Spring IOC容器
- Spring入门实例讲解(2)
- Struts2 + Spring + Hibernate 入门 2
- Spring入门2--aware接口
- spring MVC 2-helloword 入门
- spring MVC 2-helloword 入门
- Spring基础:快速入门spring boot(2):SPRING INITIALIZR
- Spring 入门
- Spring 入门
- Spring 入门
- Spring入门
- spring入门1
- 20170701总结
- 搭建CI感想——一周的时间搞:maven——Jenkins——Sonar——Jacoco
- 一.Flask-基本环境安装配置
- centos6.7安装hadoop准备工作-----1
- spring入门2
- 暑假第一周 J hdu 5702 (结构体排序)
- JS基础知识二
- leetcode难度及面试频率
- spring入门3
- java多线程下载
- linux查看进程所有子进程和线程
- spring入门4
- [leetcode]81. Search in Rotated Sorted Array II(Java)