<beans xmlns:aop="http://www.springframework.org/schema/aop"...> <!--启动支持--> <aop:aspectj-autoproxy /> </beans>
<beans...> <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" /> </beans>
@Aspect
public class LogAspect {
//...
}
@Aspect
public class LogAspect {
@Before("execution(* *.*.*(..))")
public void authority() {
System.out.println("执行目标方法前模拟权限检查") ;
}
}
@Aspect
public class LogAspect {
@Around("execution(* *.*.*(..))")
public Object aroundAdvice(ProceedingJoinPoint jp) {
System.out.println("执行Around增强处理") ;
//获取目标方法的原始参数
Object[] args = jp.getArgs() ;
//执行目标方法获取返回值
Object result = jp.proceed(args) ;
System.out.priontln("Around增强处理执行完毕") ;
}
}
1>Object[] getArgs():返回执行目标方法时的参数 2>Signature getSignature():返回被增强的方法的相关信息 3>Object getTarget():返回被织入增处理的目标对象 4>Object getThis():返回AOP框架为目标对象生成的代理对象
@Aspect
public class LogAspect {
@Before("execution(* *.*.*(..))")
public void beforeAdvice(JoinPoint jp) {
//获取目标方法的参数
Object[] args = jp.getArgs() ;
System.out.println("目标方法的参数列表为:" + Array.toString(args)) ;
//获取目标方法的方法名
String methodName = jp.getSignature().getName() ;
System.out.println("目标方法的方法名为:" + methodNamme) ;
//获取被织入增强处理的目标对象LogAspect
System.out.println("被织入增强处理的目标对象为:" + jp.getTarget()) ;
}
}
1>Aspect类实现org.springframework.core.Ordered接口,并且实现其抽象方法:int getOrder();该方法的返回值越小,优先级就越高 2>直接使用@Order注解来修饰Aspect类,该注解需要指定一个int型的value属性值
@Pointcut("execution(* *.*.*(..))")
public void simplepointcut() {
//...
}
//定义了Pointcut之后,就可以在其他的增强处理中使用其方法名作为pointcut属性值了:
@Before(pointcut="simplepointcut()")
//等同于pointcut="execution(* *.*.*(..))"
public void beforeAspect() {
//...
}
//如果使用的pointcut切入点不是同一类,就需要使用类来修饰如:
@Before(pointcut="Simple.simplepointcut()")
...
1>execution:用于匹配执行方法的joinpoint
2>within:用于限定匹配特定类型的joinpoint 如:
within(com.cheng.joinpoint.*)
3>this:用于限定AOP代理必须是指定类型的实例,匹配该对象的所有连接点 如:
this(com.cheng.aop.aopService)
4>target:用于限定目标独享必须是指定类型的实例
5>args:用于对连接点的参数类型进行限制,要求参数类型必须是指定类型的实例,多个参数类型使用逗号隔开
6>bean:用于限定只匹配指定Bean的实例内的连接点,需要传入Bean的id/name
<beans...>
<!--指定自动搜索Bean组件、自动搜索切面类-->
<context:component-scan base-package="com.cheng">
<context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect" />
</context:component-scan>
</beans>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有