aop的execution(* com.example.service.*.*(..)) 是什么意思
时间: 2023-11-14 21:35:33 浏览: 40
aop的execution(* com.example.service.*.*(..)) 表示对 com.example.service 包下所有类的所有方法进行拦截,拦截的方法可以有任意的参数类型和数量,返回类型也可以是任意类型。其中 * 表示任意字符或任意数量的字符,(..) 表示任意参数类型和数量。
相关问题
[**38.Spring中如何设置AOP暴露?**](#38)
在 Spring 中,通过配置 `<aop:config>` 标签,可以设置 AOP 的暴露方式。具体来说,可以通过设置 `<aop:config>` 标签的 `expose-proxy` 属性来控制代理对象的暴露方式。`expose-proxy` 属性有以下两个取值:
1. false:表示不暴露代理对象,默认值为 false。
2. true:表示暴露代理对象。在使用暴露代理对象的方式时,需要将代理对象注入到 Spring 容器中,并且在需要使用代理对象的地方,通过 `AopContext.currentProxy()` 方法来获取当前的代理对象。
例如,以下是一个使用暴露代理对象的例子:
```xml
<aop:config proxy-target-class="true" expose-proxy="true">
<!-- 定义切面 -->
<aop:aspect ref="myAspect">
<!-- 定义切点 -->
<aop:pointcut id="myPointcut" expression="execution(* com.example.service.*.*(..))"/>
<!-- 定义通知 -->
<aop:before method="before" pointcut-ref="myPointcut"/>
</aop:aspect>
</aop:config>
<bean id="myService" class="com.example.service.MyServiceImpl">
<!-- 注入代理对象 -->
<aop:scoped-proxy/>
</bean>
<bean id="myAspect" class="com.example.aspect.MyAspect"/>
```
在上面的例子中,通过设置 `proxy-target-class="true"` 和 `expose-proxy="true"` 来启用代理对象的暴露。同时,使用 `<aop:scoped-proxy/>` 标签将代理对象注入到 Spring 容器中。在需要使用代理对象的地方,可以通过 `AopContext.currentProxy()` 方法来获取当前的代理对象,例如:
```java
@Service
public class MyServiceImpl implements MyService {
@Override
public void doSomething() {
MyService proxy = (MyService) AopContext.currentProxy();
// 使用代理对象执行业务逻辑
}
}
```
需要注意的是,使用暴露代理对象的方式可能会存在一些问题,例如线程安全性问题。因此,在使用暴露代理对象的方式时,需要仔细考虑其可能带来的风险并进行相应的处理。
pom.xml配置aop
在 pom.xml 中配置 AOP 需要引入 Spring AOP 相关的依赖,如下所示:
```xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
```
然后在 Spring 配置文件中开启 AOP,如下所示:
```xml
<aop:aspectj-autoproxy />
```
接着定义切面类和切点,如下所示:
```java
@Aspect
@Component
public class LogAspect {
@Pointcut("execution(* com.example.demo.service.*.*(..))")
public void pointcut() {}
@Before("pointcut()")
public void before(JoinPoint joinPoint) {
// 前置通知
}
@AfterReturning("pointcut()")
public void afterReturning(JoinPoint joinPoint) {
// 后置通知
}
@AfterThrowing("pointcut()")
public void afterThrowing(JoinPoint joinPoint) {
// 异常通知
}
@Around("pointcut()")
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
// 环绕通知
return proceedingJoinPoint.proceed();
}
}
```
其中,@Aspect 表示这是一个切面类,@Component 表示这是一个 Spring 组件,@Pointcut 定义切点,@Before、@AfterReturning、@AfterThrowing、@Around 分别表示前置通知、后置通知、异常通知和环绕通知。