依旧是LogInterceptor的例子。下面是beans.xml:
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.bjsxt"/>
<aop:aspectj-autoproxy />
</beans>
注意添加aop的namespace和<aop:aspectj-autoproxy />这一句。LogInterceptor的代码如下:
package com.bjsxt.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LogInterceptor {
@Pointcut("execution(public * com.bjsxt.service..*.add(..))")
public void myMethod(){};
@Before("myMethod()")
public void before() {
System.out.println("method before");
}
@Around("myMethod()")
public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("method around start");
pjp.proceed();
System.out.println("method around end");
}
}
这里需要注意两点:
(1) 这里必须要有@Component,让Spring来new一个LoginInterceptor对象(我们定义了<context:component-scan base-package="com.bjsxt"/>来让Spring来扫描com.bjsxt包;扫描到@Aspect就知道要织入这个类了)。这样才能把LoginInterceptor对象织入UserDAOImpl
(2) execution(public void com.bjsxt.dao.UserDAOImpl.save(com.bjsxt.model.User)"),这是AspectJ语法,execution表示在方法执行时切入。另有其他的切入点,比如属性初始化时、类加载时
根据这个例子,我们来理解一些AOP的概念:
(1) Target: UserDAOImpl是织入的Target
(2) JoinPoint: "execution(public void save())"这里就是JoinPoint,即切入点
(3) Advice: before()方法是Advice,Advice is an action taken by an Aspect at a certain JoinPoint
(4) AdviceType: @Before是AdviceType,表示这是一个Advice that executes before a JoinPoint
(5) PointCut: 从通配符(@Pointcut("execution(public * com.bjsxt.service.*.add(.))"))可以看出,PointCut是JoinPoint的集合,但是注意PointCut必须依赖于一个方法
(6) Aspect: LogInterceptor这个类,或者说LogInterceptor这个类的逻辑(即记录日志)是一个Aspect
分享到:
相关推荐
Spring AOP 入门 作者:廖雪峰
NULL 博文链接:https://aoyi.iteye.com/blog/723571
Spring AOP面向方面编程原理:AOP概念,主要介绍面向对象的概念及原理,及作者的一些理解。
赠送jar包:spring-aop-5.0.10.RELEASE.jar; 赠送原API文档:spring-aop-5.0.10.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.10.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.10.RELEASE....
spring的aop简单例子spring的aop简单例子spring的aop简单例子spring的aop简单例子
NULL 博文链接:https://306963591.iteye.com/blog/1129837
Spring AOP 几个不同使用方法的完整例子,使用Junit4c测试, 在我的博客上有不同配置组合的说明,可以参考
Spring AOP 代理一、两种代理方式1. JDK动态代理:其代理对象必须是某个接口的实现,它是通过在运行期间创建一个接口的实现类来完成对目标对象的代理。二
Spring源码最难问题:当Spring AOP遇上循环依赖.docx
spring aop例子,这是spring aop例子哦
spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop
spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...
spring aop简单例子,入门学习的好资料
spring aop jar 包
spring aop 经典例子(原创),无论新手还是老手,可以快速对Spring AOP有个总体的认识。基于JDK1.6,Spring2.0.5,Eclipse IDE。
SPRING_AOP_概念解析以及例子示范
SpringAOP注解方式.zip,SpringAOP注解特棒例子
Spring_AOP_学习小结 Spring_AOP_学习小结 Spring_AOP_学习小结 Spring_AOP_学习小结
NULL 博文链接:https://microjava.iteye.com/blog/525796
eclipse的SpringAOP和动态代理的小例子