Spring切面二使用注解
package com.IC;
public interface PhoneBiz {
	public void buyPhone(int num);	//购买手机;
	public void salePhone(int num);	//销售手机
}
package com.bean;
import com.IC.*;
public class PhoneBizImpl implements PhoneBiz {
	public void buyPhone(int num) {
		System.out.println("购买手机"+num);
	}
	public void salePhone(int num) {
		System.out.println("销售手机"+num);
	}
}
package com.bean;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.jsp.tagext.TryCatchFinally;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class LogAspect {
	/*切入点*/
	@Pointcut("execution(void *Phone(int))")
	public void p1(){}
	@Before("p1()")
	public void before(JoinPoint jp)throws Throwable{
		Object[]args=jp.getArgs();	//目标方法所有参数;
		String methodName=jp.getSignature().getName();	//获得目标方法名称;
		if("buyPhone".equals(methodName)){
			System.out.println(currentTime()+"即将执行进货操作,数量为:"+args[0]);
		}
		if("salePhone".equals(methodName)){
			System.out.println(currentTime()+"即将执行销售操作,数量为:"+args[0]);
		}
	}
	@AfterReturning("p1()")
	public void afterReturing(JoinPoint jp)throws Throwable{
		String methodName=jp.getSignature().getName();
		if("buyPhone".equals(methodName)){
			System.out.println(currentTime()+"进货完毕");
		}
		if("salePhone".equals(methodName)){
			System.out.println(currentTime()+"销售完毕");
		}
	}
	/*环绕通知*/
	@Around("p1()")
	public Object after(ProceedingJoinPoint pjp)throws Throwable{
		String method=pjp.getSignature().getName();
		long begin=System.currentTimeMillis();
		System.out.println(currentTime()+":"+method+"方法开始执行,计时开始");
		try {
			return pjp.proceed();
		}finally{
			long end=System.currentTimeMillis();
			System.out.println(currentTime()+"耗时:"+(end-begin)+"毫秒");
		}
	}
	private String currentTime() {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
		return sdf.format(new Date());
	}
}
package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.IC.PhoneBiz;
import com.bean.PhoneBizImpl;
public class Test {
	public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		PhoneBiz pb=(PhoneBiz) ac.getBean("phoneBiz");
		pb.buyPhone(100);
		pb.salePhone(40);
	}
}
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 启用注解配置 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!-- 目标业务对象 --> <bean id="phoneBiz" class="com.bean.PhoneBizImpl"></bean> <!-- 日志管理切面 --> <bean class="com.bean.LogAspect"></bean> </beans>
Spring切面二使用注解的更多相关文章
- Spring Boot 二十个注解
		
Spring Boot 二十个注解 占据无力拥有的东西是一种悲哀. Cold on the outside passionate on the insede. 背景:Spring Boot 注解的强大 ...
 - spring boot(二):注解大全
		
spring boot注解 @Autowired 注解的意思就是,当Spring发现@Autowired注解时,将自动在代码上下文中找到和其匹配(默认是类型匹配)的Bean,并自动注入到相应的地方去. ...
 - spring入门(二) 使用注解代替xml配置
		
1.导包(略) 2.applicationContext.xml如下: <?xml version="1.0" encoding="UTF-8"?> ...
 - Spring AOP(二)--注解方式
		
本文介绍通过注解@AspectJ实现Spring AOP,这里要重点说明一下这种方式实现时所需的包,因为Aspect是第三方提供的,不包含在spring中,所以不能只导入spring-aop的包,为了 ...
 - spring aop使用,spring aop注解,Spring切面编程
		
================================ ©Copyright 蕃薯耀 2020-01-21 https://www.cnblogs.com/fanshuyao/ 一.第一步, ...
 - Spring 注解(二)注解工具类 AnnotationUtils 和 AnnotatedElementUtils
		
Spring 注解(二)注解工具类 AnnotationUtils 和 AnnotatedElementUtils Spring 系列目录(https://www.cnblogs.com/binary ...
 - Spring AOP之使用注解创建切面
		
上节中我们已经定义了Performance接口,他是切面中的切点的一个目标对象.那么现在就让我们使用AspectJ注解来定义切面吧. 1.定义切面 下面我们就来定义一场舞台剧中观众的切面类Audien ...
 - SpringInAction--面向切片的Spring以及如何使用注解创建切面
		
什么叫做切片..什么叫做AOP... 与大多数技术一样,AOP已经形成了自己的术语.描述切面的常用术语有通知(advice).切点(pointcut)和连接点(join point). (一大串书上的 ...
 - Spring AOP 面向切面编程相关注解
		
Aspect Oriented Programming 面向切面编程 在Spring中使用这些面向切面相关的注解可以结合使用aspectJ,aspectJ是专门搞动态代理技术的,所以比较专业. ...
 
随机推荐
- 详细讲解 A/B 测试关键步骤,快来检查下还有哪些疏漏的知识点
			
作为一种对照实验方法,A/B 测试通过比较两个 (或多个) 不同版本之间的差异来验证假设是否正确.该方法将特定测试组从实验其余部分中独立出来,从而得出可靠结果.在被测人不知情且测试场景真实的情况下,A ...
 - fastdfs+nginx+image_filter安装与生成缩略图
			
fastdfs简介 类似google FS的一个轻量级分布式文件系统,纯C实现,支持linux.FreeBSD等UNIX系统: 只能通过API访问,不支持POXIS: 文件不分块存储,上传的文件和OS ...
 - [转]Git 撤销操作
			
二. Git撤消操作 12.1 修改最后一次提交 git commit --amend 1.新建一个文件 2.提交一个之前的更改 3.跟踪这个文件 4.跟前一次一起提交 提示你是否重新编辑提交说明,如 ...
 - Github上的一些高分Qt开源项目【多图】
			
游戏2D地图编辑器: 著名的TileMap编辑器,做2D游戏开发的一定不会陌生. Go 语言的IDE: Go语言的集成开发环境. Clementine Music Player: 功能很完善且跨平台支 ...
 - ASP.NET MVC - 启动创建项目,未能加载错误
			
VS2012以常规方式创建一ASP.NET MVC internet 项目.创建后F5启动项目,遇一错误: 未能加载文件或程序集“MySql.Web.v20, Version=6.9.4.0, Cul ...
 - Web全景图的原理及实现
			
全景图的基本原理 全景图是一种广角图.通过全景播放器可以让观看者身临其境地进入到全景图所记录的场景中去.比如像是这个.这种看起来很高大上的效果其实背后的原理并不复杂. 通常标准的全景图是一张2:1的图 ...
 - KETTLE并行
			
1.转换的并行 转换的并行是改变复制的数量 上面的转换相当于下面的: 实际是把一个任务拆成三部分执行,相当于在一个数据库连接中做了三次查询,数据库连接的开销没有增加,但是有三个进程一起执行. 2.jo ...
 - 【Alpha】阶段第三次Scrum Meeting
			
[Alpha]阶段第三次Scrum Meeting 工作情况 团队成员 今日已完成任务 明日待完成任务 刘峻辰 更新评论接口 获取课程评论接口 赵智源 编写脚本实现持续集成 整合前端进行部署 肖萌威 ...
 - 关于jsp之间href传参(中文)乱码问题
			
在A.jsp中有href传值 <a href=\"6.jsp?param="+rs.getString(2)+"\">" 在B.jsp中使 ...
 - POJ2528的另一种解法(线段切割)
			
题目:Mayor's posters 原文地址 首先本题题意是:有一面墙,被等分为1QW份,一份的宽度为一个单位宽度.现在往墙上贴N张海报,每张海报的宽度是任意 的,但是必定是单位宽度的整数倍,且&l ...