Spring切面二使用注解

来源:互联网 发布:linux 下载安装命令 编辑:程序博客网 时间:2024/05/25 20:01
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;@Aspectpublic 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.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/aophttp://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>



0 0
原创粉丝点击