spring aop 排除某个方法
时间: 2024-01-31 11:05:16 浏览: 40
在Spring AOP中,如果需要排除某个方法,可以使用`!`操作符来实现。在切面类的`@Before`或`@After`注解中,可以使用`!`操作符来排除某个方法的执行。例如,在给定的代码中,切面类`UserAspect`中的`@Before`和`@After`注解使用`!execution(* com.Operation..*.selectUser())`来排除`selectUser()`方法的执行。
相关问题
SpringAOP拦截方法
Spring AOP是Spring框架提供的一种面向切面编程的解决方案,它可以在方法执行前后、抛出异常时等特定的时刻拦截方法,并在方法执行前后加入自定义的逻辑。
以下是使用Spring AOP拦截方法的步骤:
1. 在Spring配置文件中配置AOP代理
```xml
<aop:aspectj-autoproxy />
```
2. 创建一个切面类,并在类上使用@Aspect注解声明该类是一个切面
```java
@Aspect
public class MyAspect {
...
}
```
3. 在切面类中声明一个或多个切点,切点用于定位到需要拦截的方法
```java
@Pointcut("execution(* com.example.service.*.*(..))")
public void myPointcut() {}
```
4. 在切面类中声明一个或多个通知,通知是在拦截到方法时需要执行的逻辑
```java
@Before("myPointcut()")
public void beforeAdvice() {
//执行前置通知的逻辑
}
```
5. 在需要拦截的方法上加入@Aspect注解
```java
@Service
public class MyService {
@MyAnnotation
public void myMethod() {
//需要拦截的方法
}
}
```
6. 在需要拦截的方法上加入自定义注解,以便在切点表达式中使用
```java
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {}
```
通过以上步骤,就可以使用Spring AOP拦截方法并在方法执行前后加入自定义的逻辑。
Spring AOP常用方法
Spring AOP常用方法有以下几种:
1. 使用注解方式实现AOP:通过在目标方法上添加注解,然后在配置文件中配置切面和通知,实现对目标方法的增强。
```java
@Aspect
@Component
public class MyAspect {
@Before("execution(* com.example.service.UserService.addUser(..))")
public void beforeAddUser(JoinPoint joinPoint) {
// 在目标方法执行之前执行的逻辑
System.out.println("Before adding user...");
}
}
```
2. 使用XML配置方式实现AOP:通过在配置文件中配置切面和通知,实现对目标方法的增强。
```xml
<bean id="myAspect" class="com.example.aspect.MyAspect" />
<aop:config>
<aop:aspect ref="myAspect">
<aop:before method="beforeAddUser" pointcut="execution(* com.example.service.UserService.addUser(..))" />
</aop:aspect>
</aop:config>
```
3. 使用@Around注解实现环绕通知:通过在切面方法上添加@Around注解,可以在目标方法执行前后执行自定义逻辑。
```java
@Aspect
@Component
public class MyAspect {
@Around("execution(* com.example.service.UserService.addUser(..))")
public Object aroundAddUser(ProceedingJoinPoint joinPoint) throws Throwable {
// 在目标方法执行之前执行的逻辑
System.out.println("Before adding user...");
// 执行目标方法
Object result = joinPoint.proceed();
// 在目标方法执行之后执行的逻辑
System.out.println("After adding user...");
return result;
}
}
```
4. 使用@AfterReturning注解实现后置通知:通过在切面方法上添加@AfterReturning注解,可以在目标方法执行后执行自定义逻辑。
```java
@Aspect
@Component
public class MyAspect {
@AfterReturning(pointcut = "execution(* com.example.service.UserService.addUser(..))", returning = "result")
public void afterReturningAddUser(JoinPoint joinPoint, Object result) {
// 在目标方法执行后执行的逻辑
System.out.println("After adding user...");
}
}
```
5. 使用@AfterThrowing注解实现异常通知:通过在切面方法上添加@AfterThrowing注解,可以在目标方法抛出异常时执行自定义逻辑。
```java
@Aspect
@Component
public class MyAspect {
@AfterThrowing(pointcut = "execution(* com.example.service.UserService.addUser(..))", throwing = "ex")
public void afterThrowingAddUser(JoinPoint joinPoint, Exception ex) {
// 在目标方法抛出异常时执行的逻辑
System.out.println("Exception occurred while adding user: " + ex.getMessage());
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)