以下是一个基于Spring XML配置的AspectJ AOP,请补充缺失的代码:Java切面类
Package com.example
Public class LogAspect {
public void beforeAdvice() {
System.out.println("前置通知:方法执行前记录日志");
}
public void afterAdvice() {
System.out.println("后置通知:方法执行后记录日志");
}
}
目标接口与实现类
Package com.example
Public interface UserService {
void addUser(String name);
}
Public class UserServiceImpl implements UserService {
public void addUser(String name) {
System.out.println("添加用户:" + name);
}
}
Spring XML配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
…
xsi:schemaLocation="
http://www.springframework.org/schema/beans
…">
<!-- 定义目标Bean -->
<bean id="userService" class="com.example.UserServiceImpl" />
<!-- 定义切面Bean -->
<① id="logAspect" class="com.example.LogAspect" />
<!-- 配置AOP -->
<aop:config>
<aop:aspect ref="logAspect">
<!-- 定义切入点表达式 -->
<aop: ② id="servicePointcut"
expression="execution(* com.example.UserService. ③ (..))" />
<!-- 前置通知 -->
<aop:_④_ method="beforeAdvice" pointcut-ref="servicePointcut" />
<!-- 后置通知 -->
<aop:_⑤__ method="afterAdvice" pointcut-ref="servicePointcut" />
</aop:aspect>
</aop:config>
</beans>