接口
package cn.itheima.springxmlaop;
public interface UserService {
void save(int a, int b);
void update();
void find();
void delete();
}
接口实现类
package cn.itheima.springxmlaop;
public interface UserService {
void save(int a, int b);
void update();
void find();
void delete();
}
通知/增强
package cn.itheima.springxmlaop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
//这就是一个切面
public class MyAdvice {
//前置通知
public void before(JoinPoint point) {
System.out.println(point.getTarget());//cn.itheima.springxmlaop.UserServiceImpl@1fe47411
System.out.println(point.getTarget().getClass());//class cn.itheima.springxmlaop.UserServiceImpl
System.out.println(point.getTarget().getClass().getName());//cn.itheima.springxmlaop.UserServiceImpl
System.out.println(point.getTarget().getClass().getSimpleName());//UserServiceImpl
System.out.println(point.getSignature().getName());//save,or,delete,or,update
//System.out.println(point.getArgs()[0]);//[Ljava.lang.Object;@1165477e数组对象//2
System.out.println(point.getArgs());//[Ljava.lang.Object;@1165477e数组对象//2
System.out.println("前置通知");
}
//后置通知
public void after() {
System.out.println("后置通知");
}
//环绕通知
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕通知之前");
Object proceed = pjp.proceed();
System.out.println("环绕通知之后");
return proceed;
}
//异常拦截通知
public void exception() {
System.out.println("出现异常了");
}
//后置异常通知
public void afterException() {
System.out.println("出现异常了,后置通知");
}
}
测试:
package cn.itheima.springxmlaop;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:cn/itheima/springxmlaop/applicationContext.xml")
public class Demo {
@Resource(name="userServiceImpl")
private UserService userServiceImpl;
@Test
public void fun() {
userServiceImpl.save(2,3);
userServiceImpl.delete();
}
}
配置文件applicationContext.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">
<bean name="userServiceImpl" class="cn.itheima.springxmlaop.UserServiceImpl"></bean>
<bean name="myAdvice" class="cn.itheima.springxmlaop.MyAdvice"></bean>
<aop:config>
<!-- public void cn.itheima.service.UserServiceImpl.save()
void cn.itheima.service.UserServiceImpl.save()
* cn.itheima.service.UserServiceImpl.save()
* cn.itheima.service.UserServiceImpl.*()
* cn.itheima.service.UserServiceImpl.*(..)
* cn.itheima.service.*ServiceImpl.*(..)
* cn.itheima.service.*ServiceImpl.*(..)
-->
<aop:pointcut expression="execution(* cn.itheima.springxmlaop.*ServiceImpl.*(..))" id="pc"/>
<aop:aspect ref="myAdvice">
<aop:after method="afterException" pointcut-ref="pc"/>
<aop:after-returning method="after" pointcut-ref="pc"/>
<aop:after-throwing method="exception" pointcut-ref="pc"/>
<aop:around method="around" pointcut-ref="pc"/>
<aop:before method="before" pointcut-ref="pc"/>
</aop:aspect>
</aop:config>
</beans>