<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/contesxt/spring-context-4.2.xsd ">
<!--2.引入切面类 -->
<bean id="myAspect" class="com.uc.aop_aspectj.MyAspect"></bean>
<!-- 3.aop配置 -->
<aop:config>
<aop:aspect ref="myAspect">
<aop:pointcut expression="eaecution(com.uc.aop_aspectj.*.*(..))"
id="mypoincut" />
<!--3.1 前置通知: <aop:before method="" pointcut-ref=""/>
method:通知,方法名
poincut:切入点表达式,此表达式只能当前通知有效
pointcut-ref:切入点引用,可以与其他通知共享
通知方法格式:public void myBefore(JoinPoint joinPoint){
参数1.import org.aspectj.lang.JoinPoint;用于描述连接点(目标方法),获取目标方法的方法名
<aop:before method="mybefore" pointcut-ref="mypoincut"/> -->
<!-- 3.2后置通知:
<aop:after-returning method="" pointcut-ref="" returning="" />
returning:通知方法的第二个参数名称,告诉aop这是个返回值
通知方法格式:public void myafterreturning(JoinPoint joinpoint,Object ret){
参数1.连接点描述
参数2.类型Object,参数名是returning="ret"配置的
<aop:after-returning method="myafterreturning" pointcut-ref="mypoincut" returning="ret" />
-->
<!-- 3.3环绕通知
<aop:around method="" pointcut-ref=""/>
通知方法格式:public Object myaround(ProceedingJoinPoint joinpoint) throws Throwable{
返回值类型:Object
方法名:任意
参数:import org.aspectj.lang.ProceedingJoinPoint;
执行目标方法:Object proceed = joinpoint.proceed();
例如:
<aop:around method="myaround" pointcut-ref="mypoincut"/>
-->
<!-- 3.4抛出异常通知
<aop:after-throwing method="" pointcut-ref="" throwing=""/>
throwing:通知方法的第二个参数名称
通知方法的格式: public void afterThrowing(JoinPoint joinpoint,Throwable mc) throws Throwable{
参数1.连接点的描述对象
参数2.获得异常信息,类型Throwable,参数名有throwing="mc"配置
例如:
<aop:after-throwing method="afterThrowing" pointcut-ref="mypoincut" throwing="mc"/>
-->
<!-- 3.5最终通知: -->
</aop:aspect>
</aop:config>
</beans>
package com.uc.aop_aspectj;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
/**
* AOP联盟为通知定义了通知类型,具有特定接口,必须实现,从而确定方法名称
*
* aspectj 通知类型:只定义类型名称、方法格式
* 个数:6个,知道5种,掌握1种
* before:前置通知(应用:各种校验)
* afterReturning:后置通知(应用:常规的数据处理)
* 方法正常返回后执行,如果方法中抛出异常,通知无法执行
* 必须在方法执行后才执行,所以可以获得方法的返回值
* around:环绕通知 (应用:十分强大,可以做任何事情)
* afterThrowing:抛出异常通知(应用:包装异常信息)
* 方法抛出异常后执行,如果方法没有抛出异常,无法执行
* after:最终通知(应用:清理现场)
* 方法执行完毕后,无论方法中是否出现异常
* @author
*
*/
public class MyAspect {
public void myBefore(JoinPoint joinpoint){
System.out.println("前置通知"+joinpoint.getSignature().getName());
}
public void myafterreturning(JoinPoint joinpoint,Object ret){
System.out.println("后置通知:"+joinpoint.getSignature().getName()+"---->"+ret);
}
public Object myaround(ProceedingJoinPoint joinpoint) throws Throwable{
System.out.println("环绕前:");
Object proceed = joinpoint.proceed();
System.out.println("环绕后:");
return proceed;
}
public void afterThrowing(JoinPoint joinpoint,Throwable mc) throws Throwable{
System.out.println("抛出异常通知"+mc.getMessage());
}
public void after(JoinPoint joinpoint) throws Throwable{
System.out.println("最终通知");
}
}
package com.uc.aop_aspectj;
public interface UserService {
public void addUser();
public void subUser();
public void divUser();
public String multUser();
public void adddept();
}
package com.uc.aop_aspectj;
import org.springframework.stereotype.Service;
@Service
public class UserServiceimpl implements UserService{
public void addUser() {
System.out.println("增加");
}
public void subUser() {
System.out.println("减少");
}
public void divUser() {
System.out.println("除");
int i=1/0;
}
public String multUser() {
System.out.println("乘法");
return "哈哈";
}
public void adddept() {
System.out.println("添加部门");
}
}
package com.uc.aop_aspectj;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyAopTest {
@Test
public void testaop(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationcontext.xml");
UserService userService = (UserService) applicationContext.getBean("UserServiceimpl");
userService.addUser();
userService.subUser();
userService.divUser();
userService.multUser();
userService.adddept();
}
}