Spring切面二使用注解

举报
tea_year 发表于 2021/12/30 00:17:27 2021/12/30
【摘要】 package com.IC; public interface PhoneBiz { public void buyPhone(int num); //购买手机; public void salePhone(int num); //销售手机} JAVA就业套餐课:https://edu.csdn.net/combo/detail/1...

      package com.IC;
      public interface PhoneBiz {
     	public void buyPhone(int num);	//购买手机;
     	public void salePhone(int num);	//销售手机
      }
  
 

JAVA就业套餐课:https://edu.csdn.net/combo/detail/1230

 


      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>
  
 

 

 

 

 

 

文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。

原文链接:aaaedu.blog.csdn.net/article/details/53913514

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。