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>

最新文章

  1. android 图像处理系列合集
  2. 用Python写一个简单的Web框架
  3. JS开发HTML5游戏《神奇的六边形》(一)
  4. Java 项目JDBC 链接数据库中会出现的错误
  5. Android组件Spinner使用
  6. HDU 3491 最小点权割集
  7. Android Bundle的使用
  8. mvn多模块开发消除重复依赖造成的打包失败
  9. android 项目中设置背景图片
  10. ASM上的备份集如何转移到文件系统中
  11. Redis笔记3-redis事务
  12. 用ECMAScript4 ( ActionScript3) 实现Unity的热更新 -- 使用FairyGUI (一)
  13. EditText搜索关键字,返回结果匹配关键字改变颜色
  14. django学习:一些疑惑
  15. Python mysql 创建连接
  16. 洛谷题解 P1138 【第k小整数】
  17. promql查询表达式
  18. Linux 启动过程详解
  19. 如何将U盘转化成NTFS格式
  20. GitLab 502问题的解决

热门文章

  1. 在Notepad++中为Python配置编译环境
  2. scrapy 爬取知乎问题、答案 ,并异步写入数据库(mysql)
  3. CsvHelper文档-6类型转换
  4. MySQL双主复制
  5. JAVA学习笔记--简介几个常见关键字static、final、this、super
  6. Fast R-CNN学习总结
  7. Hexo博客 云服务器搭建
  8. scrapy-redis+selenium+webdriver解决动态代理ip和user-agent的问题(全网唯一完整代码解决方案)
  9. ES6对数组的扩展
  10. three的初步探索之表象篇