Spring实现AOP的三种方法
第一种方式:使用原生的Spring API接口
- 创建测试接口
public interface UserService {
public void add();
public int delete();
public void update();
public void select();
}
- 实现测试接口
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("执行了add 方法");
}
public int delete() {
System.out.println("执行了delete方法");
return 10;
}
public void update() {
System.out.println("执行了更新方法");
}
public void select() {
System.out.println("执行了查询操作");
}
}
- 配置application.xml
<!--注册bean到Spring容器中-->
<bean id="userService" class="com.wangjw.service.UserServiceImpl"/>
<bean id="log" class="com.wangjw.log.Log"/>
<bean id="afterLog" class="com.wangjw.log.AfterLog"/>
<aop:config>
<!--切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.wangjw.service.UserServiceImpl.*(..))"/>
<!--执行环绕增强-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
- 实现AOP接口
根据需要,实现相关的接口的方法即可,此处只列举AfterReturningAdvice和MethodBeforeAdvice
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("执行了"+method.getName()+"返回结果:0"+o);
}
}
/**各参数含义
* Callback after a given method successfully returned.
* @param returnValue the value returned by the method, if any
* @param method the method being invoked
* @param args the arguments to the method
* @param target the target of the method invocation. May be {@code null}.
* @throws Throwable if this object wishes to abort the call.
* Any exception thrown will be returned to the caller if it's
* allowed by the method signature. Otherwise the exception
* will be wrapped as a runtime exception.
*/
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Before implements MethodBeforeAdvice {
public void before(Method method, Object[] target, Object o) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
/** 各参数含义
* Callback before a given method is invoked.
* @param method the method being invoked
* @param args the arguments to the method
* @param target the target of the method invocation. May be {@code null}.
* @throws Throwable if this object wishes to abort the call.
* Any exception thrown will be returned to the caller if it's
* allowed by the method signature. Otherwise the exception
* will be wrapped as a runtime exception.
*/
- 测试
public class MyTest {
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.add();
System.out.println("*********************");
userService.delete();
}
}
/** 结果输出
[Ljava.lang.Object;的add被执行了
执行了add 方法
执行了add返回结果:0null
*********************
[Ljava.lang.Object;的delete被执行了
执行了delete方法
执行了delete返回结果:010
**/
第二种方式:自定义类实现AOP
- 自定义类
public class Diy {
public void before(){
System.out.println("方法执行前");
}
public void after(){
System.out.println("方法执行后");
}
}
- 配置application.xml
<!--注册bean到Spring容器中-->
<bean id="diy" class="com.wangjw.diy.Diy"/>
<aop:config>
<!--配置切面-->
<aop:aspect id="" ref="diy">
<!--切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.wangjw.service.UserServiceImpl.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
- 测试
public class MyTest {
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.add();
System.out.println("*********************");
userService.delete();
}
}
/** 结果输出
方法执行前
执行了add 方法
方法执行后
*********************
方法执行前
执行了delete方法
方法执行后
**/
第三种方式:基于注解实现AOP
- 自定义切面
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class AnnotationAop {
@Before("execution(* com.wangjw.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("使用注解实现AOP之方法执行前");
}
@After("execution(* com.wangjw.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("使用注解实现AOP之方法执行后");
}
}
- 配置application.xml
<!--注册切面到Spring容器中-->
<bean class="com.wangjw.diy.AnnotationAop" id="annotationAop"/>
<!--开启aop注解的支持-->
<aop:aspectj-autoproxy/>
- 测试
public class MyTest {
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.add();
System.out.println("*********************");
userService.delete();
}
}
/** 结果输出
使用注解实现AOP之方法执行前
执行了add 方法
使用注解实现AOP之方法执行后
*********************
使用注解实现AOP之方法执行前
执行了delete方法
使用注解实现AOP之方法执行后
**/