sping AOP 使用
来源:互联网 发布:淘宝禁止出售的药品 编辑:程序博客网 时间:2024/05/16 03:32
定义接口
public interface Performer {
void perform(List ls);
}
实现接口
@Service
public class Drama implements Performer {
public void perform(List ls) {
// TODO Auto-generated method stub
for (int i = 0; i < 1000; i++) {
System.out.println("话剧正在进行中——"+ls.get(2).toString());
}
}
}
切面拦截类
@Service
public class Audience {
public void takeSeats() throws Throwable {
// 节目开始之前
System.out.println("演出前——观众开始入座");
}
public void turnOffCellPhones() {
// 节目开始之前
System.out.println("演出前——观众关机或静音");
}
public void applaud() {
// 节目成功结束之后
System.out.println("成功演出很成功——观众鼓掌:啪啪啪");
}
public void demandRefund() {
// 节目表演失败之后
System.out.println("节目演出很失败——切!一点都不好看,我们要求退钱!");
}
public Object watchPerformance(ProceedingJoinPoint point) {
try {
Object[] args = point.getArgs();
if (args != null && args.length > 0 && args[0].getClass() == ArrayList.class) {
List lt= (List)args[0];
lt.add("panshuyao");//向参数LIst中增加一个值
args[0]=lt;
}
Object returnValue = point.proceed(args);//执行方法,必须写此方法否则将会不运行代码
//System.out.println(args[0]+"********");
//用改变后的参数执行目标方法
return returnValue;
} catch (Throwable e) {
e.printStackTrace();
System.out.println("节目演出很失败——切!一点都不好看,我们要求退钱!");
return -1;
}
}
}
spring 配置
pointcut 设置需要进行切面的类
aspect 拦截类
before 运行目标方法前
after-returning 成功后
after-throwing 失败后
around 引介增强 可以修改传递的参数和返回的结果
<aop:config>
<aop:pointcut expression="execution(* study.com.domeaop.Performer.perform(..))" id="pointcut"></aop:pointcut>
<aop:aspect ref="audience">
<aop:before pointcut-ref="pointcut" method="takeSeats"/>
<aop:before pointcut-ref="pointcut" method="turnOffCellPhones"/>
<aop:after-returning pointcut-ref="pointcut" method="applaud"/>
<aop:after-throwing pointcut-ref="pointcut" method="demandRefund"/>
<aop:around pointcut-ref="pointcut" method="watchPerformance"/>
</aop:aspect>
</aop:config>
- sping AOP 使用
- Sping-AOP
- sping aop
- 使用sping AOP 操作日志管理
- Sping使用 AOP时 无法依赖注入问题
- sping aop pointcut定义
- sping AOP Demo
- Sping AOP详解
- day43 Sping AOP @AspectJ
- sping AOP 代理详解
- sping AOP模拟实现
- sping IOC 和 AOP
- 深入理解sping AOP
- 使用java5的注解和Sping/AspectJ的AOP 来实现Memcached的缓存
- 使用java5的注解和Sping/AspectJ的AOP 来实现Memcached的缓存
- 使用java5的注解和Sping/AspectJ的AOP 来实现Memcached的缓存
- sping 一些使用
- sping batch 使用笔记
- git flow 工作流
- 计算机网络Introduction
- UVALive 2678 子序列
- 有史以来写的最low的接口
- Good Numbers
- sping AOP 使用
- 最短路径—Dijkstra算法和Floyd算法
- UVA.679 Dropping Balls (二叉树 思维题)
- Django基本命令与初始用法
- 【zzuli】九进制转化为十进制
- Hibernate 单向N-1关联
- linux 邮件服务器
- 朴素贝叶斯分类器——机器学习算法(二)
- 线段树Lazy-tag