aspect advice pointcut :annotation 方式

@Aspect
@Component
public class testAop {
	@Pointcut("execution(* com.sl.service.*.*(..))")
	public void method(){}
	
	@Around("execution(public void com.sl.service.StudentService.add(com.sl.entity.User))")
	public void around(ProceedingJoinPoint p) throws Throwable {
		System.out.println("around begin");
		p.proceed();
		System.out.println("around over");
	}
	@Before("execution(* com.sl.service.*.*(..))")
	public void begin(){
		System.out.println("before");
	}
	@After("method()")
	public void after(){
		System.out.println("after");
	}
	
}

<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	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
	">
	<context:component-scan base-package="com.sl"></context:component-scan>
	<aop:aspectj-autoproxy/>
	
	
	
</beans>

<aop:aspectj-autoproxy/>:让spring自动在所有类里找到标有@aspect的类
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: @annotationAspectJ中的一个表达式,用于匹配被注解标记的方法。具体来说,它可以用于定义一个点,该点会匹配所有被指定注解标记的方法。这个注解可以用于AOP编程中,用于在方法执行前、执行后或抛出异常时执行一些额外的逻辑。例如,可以使用@annotation来定义一个点,该点会匹配所有被@MyAnnotation标记的方法,并在这些方法执行前或执行后执行一些额外的逻辑。 ### 回答2: AspectJ是一种面编程语言,它使用pointcut和advice来定义和实现面。pointcut是一组匹配连接点的规则,而advice则是在这些连接点上执行的行为。 在AspectJ中,可以使用@annotation来定义一个基于注解的pointcut。该pointcut将匹配所有被指定注解标注的方法。例如,以下AspectJ代码定义了一个基于@MyAnnotation注解的pointcut: ``` @Pointcut("@annotation(MyAnnotation)") public void myAnnotationPointcut() {} ``` 在这个例子中,@Pointcut注解用于定义一个点,而@annotation(MyAnnotation)参数指定了一个基于MyAnnotation注解的pointcut。可以在匹配的连接点上执行advice。 例如,以下代码展示了如何使用上述pointcut来实现一个日志面: ``` @Aspect public class LoggingAspect { @Before("myAnnotationPointcut()") public void logMethodCall(JoinPoint joinPoint) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); MyAnnotation myAnnotation = signature.getMethod().getAnnotation(MyAnnotation.class); String message = myAnnotation.value(); System.out.println("Logging message: " + message); } } ``` 在此面中,@Before注解用于指定advice将在匹配的连接点之前执行。在logMethodCall方法中,我们获取了匹配方法的注解信息,并且使用该信息进行日志记录。 总之,通过使用@annotation,我们可以定义基于注解的点来匹配符合指定注解的方法,并执行针对该点的advice。这在实现像日志记录和安全认证等面时非常有用。 ### 回答3: pointcut是AOP中的一个概念,用于定义入点,即在应用程序中哪些方法需要被织入到面中。点入点通常由表达式表示,在该表达式中,可以使用多种语法来匹配目标对象中的方法。点入点也可以使用注解来表示。 @annotation是一个在点入点中常用的语法,在其中,入点将匹配带有特定注解的方法。注解可以用来标识目标方法的特殊行为或属性,例如,某个方法需要特殊的安全权限,或者某个方法需要记录其调用时间等。 具体来说,使用@annotationpointcut中的语法如下: @Pointcut("@annotation(com.example.CustomAnnotation)") 其中,注解类名为com.example.CustomAnnotation。这意味着,只有带有这个注解的方法才会被匹配,然后被织入到面中。 需要注意的是,在使用@annotation之前,需要先定义自定义注解。例如,可以定义一个名为CustomAnnotation的注解类: @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface CustomAnnotation { } 在这个例子中,注解类被标记为@Retention(RetentionPolicy.RUNTIME),这意味着注解将在运行时保留,并能够通过Java反射来访问它。注解类还用@Target(ElementType.METHOD)标记,这表示注解只能用于方法上。 综上所述,@annotationpointcut中的使用方法如上所示。它可以用来匹配带有特定注解的方法,然后将这些方法织入到面中。在使用之前,需要先定义自定义注解类,并标记其@Retention(RetentionPolicy.RUNTIME)和@Target(ElementType.METHOD)。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值