1、bean.xml文件
<!-- 扫包 -->
<context:component-scan base-package="com.aop.aspect"></context:component-scan>
<!-- 注解织入开启aspectaop的注解模式 -->
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
2、新建接口IPerson,实现类XiaoBaiImpl
package com.aop.aspect;
public interface IPerson {
public void addPerson();
public void addPersonNum(int num,String name);
public void updatePerson();
}
package com.aop.aspect;
import org.springframework.stereotype.Component;
@Component
public class XiaoBaiImpl implements IPerson{
@Override
public void addPerson() {
System.out.println("添加小白!");
}
@Override
public void addPersonNum(int num, String name) {
System.out.println("添加小白数量!");
}
@Override
public void updatePerson() {
System.out.println("更新小白!");
}
}
3、前置通知BeforePersonAdvisor。环绕通知AroundPersonAdvisor
package com.aop.aspect;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class BeforePersonAdvisor {
// @Before:前置通知注解
// @AfterReturning:后置通知
// @AfterThrowing 异常通知
// @Around:环绕型通知,execution within args target 等怎么去找到我"中意的方法"
@Before("execution(public void addPerson())")
// @AfterReturning("execution(* add*(..))")
public void beforeAdvisor() {
System.out.println("======AOP织入类!=====");
}
}
package com.aop.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class AroundPersonAdvisor {
@Around("execution(* update*(..))")
public void aroundAdvisor(ProceedingJoinPoint joinPoint) throws Throwable {
Object[] params = joinPoint.getArgs();
System.out.println("参数=="+params);
System.out.println("方法名="+joinPoint.getSignature().getName());
System.out.println("类名=="+joinPoint.getThis().getClass().getName());
System.out.println("更新开始了。。。");
joinPoint.proceed();
System.out.println("更新结束了。。。");
}
}
package com.aop.aspect;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:com/aop/aspect/bean.xml");
// String[] names = context.getBeanDefinitionNames();
// for (String string : names) {
// System.out.println(string);
// }
//proxy-target-class="true" 使用CGLIB模式
XiaoBaiImpl xiaoBaiImpl = (XiaoBaiImpl) context.getBean("xiaoBaiImpl");
//<aop:aspectj-autoproxy></aop:aspectj-autoproxy>默认是JDK方式,需要使用接口
// IPerson xiaoBaiImpl = (IPerson) context.getBean("xiaoBaiImpl");
xiaoBaiImpl.addPerson();
System.out.println("-----------------------");
xiaoBaiImpl.updatePerson();
}
}
结果:
一月 04, 2017 5:41:54 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4ccabbaa: startup date [Wed Jan 04 17:41:54 CST 2017]; root of context hierarchy
一月 04, 2017 5:41:54 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/aop/aspect/bean.xml]
======AOP织入类!=====
添加小白!
-----------------------
参数==[Ljava.lang.Object;@662ac478
方法名=updatePerson
类名==com.aop.aspect.XiaoBaiImpl$$EnhancerBySpringCGLIB$$1d9a1514
更新开始了。。。
更新小白!
更新结束了。。。
注解函数:
@aspectJ支持三种通配符:
*:匹配任意字符
..:匹配任意字符,一般联合*使用,在参数列表中,代表参数的全匹配(空方法和有参数的)
+:匹配类制定的所有的类。如果是接口或者父类。会把下面所有扩展的子类和本身都会匹配出来
execution()是最常用的一种过滤方法的函数.
公式:public void com.tz.aop.UserServerImpl.save(int,int)throws exception;
格式:exection(<访问修饰符>?[返回值类型][方法的名称](<参数>)<异常模式>)
1:匹配所有的方法:execution(* *(..))
第一个*代表是返回值
第二个*代表是方法名
..的代表参数
2:execution(* com.aop.aspect.XiaoBaiImpl.add*(..))
3:execution(* com.aop.aspect.IPerson.add*(..)) 会拦截接口的实现类的add*方法
4:execution(* com.aop.*.*(..))
5:execution(* com.aop.*.add*(..))找到aop下面的类
6:execution(* com.aop..*.add*(..))找到当前aop和aop所有的子孙包下面的所有的类
7:execution(* com.aop..*.add*(int,..))找到当前aop和aop所有的子孙包下面的所有的类的add*方法,带int开头参数的
exection(* com.aop.aspect..*.before*(int,..,string))
before(int,string)
beforeadd(int,int,string)
beforeadd(int,int,string,string)
args(匹配参数的)
within:(匹配类。(不能是接口)不能匹配方法)