spring AOP
1.spring AOP 前置通知
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.zixue.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/emp") public class EmpController { /** * 查询员工 * */ @RequestMapping("/findEmp.do") public String find(){ //模拟查询员数据 System.out.println("查询员工数据,发送至列表页面"); return "emp/emp_list.jsp"; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.zixue.aspect; /** * 用于记录日志的方面组件,演示Spring AOP的各种通知类型 * * */ public class OperateLogger { /** * 前置通知 后置通知 最终通知使用的方法 * */ public void log1(){ //记录日志 System.out.println("-->记录用户操作信息"); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 开启注解扫描 --> <context:component-scan base-package="com.zixue"/> <!-- 开启RequestMapping注解 --> <mvc:annotation-driven/> <!-- 处理请求转发 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 声明方面组件 --> <bean id="operateLogger" class="com.zixue.aspect.OperateLogger"/> <!-- 配置AOP --> <aop:config> <aop:aspect ref="operateLogger"> <aop:before method="log1" pointcut="within(com.zixue.controller..*)"/> </aop:aspect> </aop:config> </beans>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.zixue.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.zixue.controller.EmpController; public class TestEmpController { @Test public void test1(){ ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml"); EmpController ctl =ac.getBean(EmpController.class); System.out.println(ctl.find()); } }
2.spring AOP 环绕通知
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.zixue.aspect; import java.text.SimpleDateFormat; import java.util.Date; import org.aspectj.lang.ProceedingJoinPoint; /** * 用于记录日志的方面组件,演示Spring AOP的各种通知类型 * * */ public class OperateLogger { /** * 前置通知 后置通知 最终通知使用的方法 * */ public void log1(){ //记录日志 System.out.println("-->记录用户操作信息"); } /** * 环绕通知使用方法 * @throws Throwable * */ public Object log2(ProceedingJoinPoint p) throws Throwable{ //目标组件的类名 String className =p.getTarget().getClass().getName(); //调用的方法名 String method=p.getSignature().getName(); //当前系统时间 String date =new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()); //拼日志信息 String msg="-->用户在"+date+",执行了"+className+"."+method+"()"; //记录日志 System.out.println(msg); //执行目标组件的方法 Object obj =p.proceed(); //在调用目标组件业务方法后也可以做一些业务处理 System.out.println("--->调用目标组件业务方法后..."); return obj; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 开启注解扫描 --> <context:component-scan base-package="com.zixue"/> <!-- 开启RequestMapping注解 --> <mvc:annotation-driven/> <!-- 处理请求转发 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 声明方面组件 --> <bean id="operateLogger" class="com.zixue.aspect.OperateLogger"/> <!-- 配置AOP --> <aop:config> <aop:aspect ref="operateLogger"> <aop:before method="log1" pointcut="within(com.zixue.controller..*)"/> </aop:aspect> <aop:aspect ref="operateLogger"> <aop:around method="log2" pointcut="within(com.zixue.controller..*)"/> </aop:aspect> </aop:config> </beans>
3.spring AOP 异常通知
/** * 异常通知使用的方法 * */ public void log3(Exception e){ StackTraceElement[] elems =e.getStackTrace(); //将异常信息记录 System.out.println("-->"+elems[0].toString()); }
<!-- 声明方面组件 --> <bean id="operateLogger" class="com.zixue.aspect.OperateLogger"/> <!-- 配置AOP --> <aop:config> <aop:aspect ref="operateLogger"> <aop:before method="log1" pointcut="within(com.zixue.controller..*)"/> </aop:aspect> <aop:aspect ref="operateLogger"> <aop:around method="log2" pointcut="within(com.zixue.controller..*)"/> </aop:aspect> <aop:aspect ref ="operateLogger"> <aop:after-throwing method="log3" throwing="e" pointcut="within(com.zixue.controller..*)"/> </aop:aspect> </aop:config>
4.sping AOP 注解使用
--@Aspect:用于声明方面组件
@Before:用于声明前置通知
@AfterReturning:用于声明后置通知
@After:用于声明最终通知
@Around:用于声明环绕通知
@AfterThrowing:用于声明异常通知