四种常用通知类型
前置通知,在切入点执行之前执行
后置通知,在切入点执行之后执行
异常通知,在切入点执行方法产生之后执行
最终通知,无论切入点执行方法是否正常执行都会在其后面执行
1.业务层接口
public interface AccountService {
//模拟保存账户
void saveAccount();
//模拟更新账户
void updateAccount(int i);
//模拟删除账户
int deleteAccount();
}
2.业务层实现类
public class AccountServiceImpl implements AccountService {
public void saveAccount() {
System.out.println("执行了保存");
}
public void updateAccount(int i) {
System.out.println("执行了更新");
}
public int deleteAccount() {
System.out.println("执行了删除");
return 0;
}
}
3.公共类Logger
public class Logger {
//前置通知
public void beforePrintLog(){
System.out.println("前置通知Logger类中的beforePrintLog方法开始记录日志");
}
//后置通知
public void afterReturnPrintLog(){
System.out.println("后置通知Logger类中的afterReturnPrintLog方法开始记录日志");
}
//异常通知
public void afterThrowingPrintLog(){
System.out.println("异常通知Logger类中的afterThrowingPrintLog方法开始记录日志");
}
//最终通知
public void afterPrintLog(){
System.out.println("最终通知Logger类中的afterPrintLog方法开始记录日志");
}
}
4.配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--配置Spring的IOC-->
<bean id="accountService" class="com.hh.service.AccountServiceImpl"/>
<bean id="logger" class="com.hh.utils.Logger"/>
<!--配置AOP-->
<aop:config>
<aop:aspect id="logAdvice" ref="logger">
<!--配置通知的类型,并且建立通知方法和切入点方法的关联-->
<!--printLog为通知方法-->
<!--pointcut代表的切入点方法-->
<!--配置前置通知,在切入点执行之前执行-->
<aop:before method="beforePrintLog"
pointcut="execution(* com.hh.service.*.*(..))"/>
<!--配置后置通知,在切入点执行之后执行-->
<aop:after-returning method="afterReturnPrintLog"
pointcut="execution(* com.hh.service.*.*(..))"/>
<!--配置异常通知,在切入点执行方法产生之后执行-->
<aop:after-throwing method="afterThrowingPrintLog"
pointcut="execution(* com.hh.service.*.*(..))"/>
<!--配置最终通知,无论切入点执行方法是否正常执行都会在其后面执行
<aop:after method="afterPrintLog"
pointcut="execution(* com.hh.service.*.*(..))"/>
</aop:aspect>
</aop:config>
</beans>
5.测试类
public class AOPTest {
public static void main(String[] args) {
//获取容器
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
//获取对象
AccountService accountService = context.getBean("accountService", AccountService.class);
//执行方法
accountService.saveAccount();
}
}
我们发现异常通知没有执行,现在在业务层制造一个异常
通用切入点表达式
<aop:pointcut id="pt" expression="execution(* com.hh.service.*.*(..))"/>
现在使用通用切入点表达式简化:
id属性用于指定表达式的唯一标识
expression属性用于指定表达式内容
此标签写在aop:aspect内部只能当前切面使用
此标签写在aop:aspect外部则所有切面可用
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--配置Spring的IOC-->
<bean id="accountService" class="com.hh.service.AccountServiceImpl"/>
<bean id="logger" class="com.hh.utils.Logger"/>
<!--配置AOP-->
<aop:config>
<aop:aspect id="logAdvice" ref="logger">
<!--配置通知的类型,并且建立通知方法和切入点方法的关联-->
<!--printLog为通知方法-->
<!--pointcut代表的切入点方法-->
<!--配置前置通知,在切入点执行之前执行-->
<aop:before method="beforePrintLog" pointcut-ref="pt"/>
<!--配置后置通知,在切入点执行之后执行-->
<aop:after-returning method="afterReturnPrintLog" pointcut-ref="pt"/>
<!--配置异常通知,在切入点执行方法产生之后执行-->
<aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pt"/>
<!--配置最终通知,无论切入点执行方法是否正常执行都会在其后面执行-->
<aop:after method="afterPrintLog" pointcut-ref="pt"/>
<!--配置切入点表达式:
id属性用于指定表达式的唯一标识。
expression属性用于指定表达式内容
此标签写在aop:aspect内部只能当前切面使用
此标签写在aop:aspect外部则所有切面可用
-->
<aop:pointcut id="pt" expression="execution(* com.hh.service.*.*(..))"/>
</aop:aspect>
</aop:config>
</beans>