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切面二使用注解的更多相关文章

  1. Spring Boot 二十个注解

    Spring Boot 二十个注解 占据无力拥有的东西是一种悲哀. Cold on the outside passionate on the insede. 背景:Spring Boot 注解的强大 ...

  2. spring boot(二):注解大全

    spring boot注解 @Autowired 注解的意思就是,当Spring发现@Autowired注解时,将自动在代码上下文中找到和其匹配(默认是类型匹配)的Bean,并自动注入到相应的地方去. ...

  3. spring入门(二) 使用注解代替xml配置

    1.导包(略) 2.applicationContext.xml如下: <?xml version="1.0" encoding="UTF-8"?> ...

  4. Spring AOP(二)--注解方式

    本文介绍通过注解@AspectJ实现Spring AOP,这里要重点说明一下这种方式实现时所需的包,因为Aspect是第三方提供的,不包含在spring中,所以不能只导入spring-aop的包,为了 ...

  5. spring aop使用,spring aop注解,Spring切面编程

    ================================ ©Copyright 蕃薯耀 2020-01-21 https://www.cnblogs.com/fanshuyao/ 一.第一步, ...

  6. Spring 注解(二)注解工具类 AnnotationUtils 和 AnnotatedElementUtils

    Spring 注解(二)注解工具类 AnnotationUtils 和 AnnotatedElementUtils Spring 系列目录(https://www.cnblogs.com/binary ...

  7. Spring AOP之使用注解创建切面

    上节中我们已经定义了Performance接口,他是切面中的切点的一个目标对象.那么现在就让我们使用AspectJ注解来定义切面吧. 1.定义切面 下面我们就来定义一场舞台剧中观众的切面类Audien ...

  8. SpringInAction--面向切片的Spring以及如何使用注解创建切面

    什么叫做切片..什么叫做AOP... 与大多数技术一样,AOP已经形成了自己的术语.描述切面的常用术语有通知(advice).切点(pointcut)和连接点(join point). (一大串书上的 ...

  9. Spring AOP 面向切面编程相关注解

    Aspect Oriented Programming 面向切面编程   在Spring中使用这些面向切面相关的注解可以结合使用aspectJ,aspectJ是专门搞动态代理技术的,所以比较专业.   ...

随机推荐

  1. Chrome 字体模糊解决

    新的电脑装了Chorm后发现字体很模糊,看起来比较累效果是这样的: 大多数都是说使用chrome://flags/中的DirectWrite开关来使其正常显示,我打开chrome://flags/没找 ...

  2. 用php做个简单的日历

    存档: index.php <html> <head> <title>日历</title> <style> table{border:1px ...

  3. Python中print函数中中逗号和加号的区别

    strip()方法,去除字符串开头或者结尾的空格 s = " a b c " new_s = s.strip() print("-------->%s<--- ...

  4. ubuntu18.04安装mongoDB 4.0

    STEP 1:  在终端输入GPK码 $  sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334B ...

  5. selenium自动化之切换iframe

    许多人在执行脚本的时候会发现,明明自己的元素路径没写错啊!怎么还是报元素未找到的异常呢?是的,没错,你可能是遇上iframe啦!下面将介绍关于iframe的相关操作. 例子:以163邮箱登录页面为例 ...

  6. NOIP2018出征策

    蒟蒻的风之旅人即将退役,现在分享一下退休前的故事 首先,经过这么多时间的划水训练,我成功从一个萌新变成了一个蒟蒻.我学会了各种奇怪玄学的算法,比如说昨天老师讲的NOIP第三题通用的算法,叫做XG算法, ...

  7. 袋鼠云研发手记 | 袋鼠云EasyManager的TypeScript重构纪要

    作为一家创新驱动的科技公司,袋鼠云每年研发投入达数千万,公司80%员工都是技术人员,袋鼠云产品家族包括企业级一站式数据中台PaaS数栈.交互式数据可视化大屏开发平台Easy[V]等产品也在迅速迭代.在 ...

  8. Machine Learning方法总结

    Kmeans——不断松弛(?我的理解)模拟,将点集分成几堆的算法(堆数需要自己定). 局部加权回归(LWR)——非参数学习算法,不用担心自变量幂次选择.(因此当二次欠拟合, 三次过拟合的时候不妨尝试这 ...

  9. 【Alpha】阶段第五次Scrum Meeting

    [Alpha]阶段第五次Scrum Meeting 工作情况 团队成员 今日已完成任务 明日待完成任务 刘峻辰 增加课程接口 增加教师接口 赵智源 整合前端进行部署 构建后端测试点测试框架 肖萌威 编 ...

  10. vue cli3 配置postcss

    1.安装postcss-import,postcss-cssnext 包 2.修改package.json 将postcss响应的内容替换为 "postcss": { " ...