转载自http://blog.csdn.net/qq_27093465/article/details/53381527
导入类扫描的注解解析器
命名空间:xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
引号添加这2个url
xml配置文件如下配置:<context:component-scan base-package="com.lxk.spring.aop.annotation"/>
导入springAOP的注解解析器
命名空间:xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
引号添加这2个url
xml配置文件如下配置:<aop:aspectj-autoproxy/>
上面的2个解析器的前后顺序不分先后。
接口类,目标接口
xml配置文件
测试和model文件
@Component(value="transaction")
解释:就是spring注解里面的注解,泛指,只是没有像@Service、@Controller 一样,具有特殊意义。
这个注解就是把切面类,纳入到spring容器管理,相当于。
@Component=bean id="transaction" class="..Transaction",这个写法。
@Aspect
此注解的作用:标识这个类就是切面。
@Aspect相当于xml文件中的aop配置标签:<aop:config> </aop:config>
然后切面里面,就有对应的切入点,通知等等。
@Pointcut
此注解的作用:标注一个切入点
此注解不能单独存在,必须依附在切面
一个切入点声明有两个部分:一个包含名字和任意参数的签名,还有一个切入点表达式,该表达式决定了我们关注那个方法的执行。
作为切入点签名的方法必须返回void 类型
@Pointcut("execution(* com.lxk.spring.aop.annotation.PersonDaoImpl.*(..))")
private void aa(){}//切入点签名
相当于如下:在xml配置切入点。
<aop:pointcut expression="execution(* cn.itcast.spring0401.aop.annotation.PersonDaoImpl.*(..))" id="aa()"/>
然后现在切入点就是这个aa(),方法啦。
看这个实现原理,spring容器有如下几次扫描过程。
第一次,解析xml配置文件解析到类扫描的注解解析器,会在base-package指定的包及子包中扫描所有的类看类上是否有@Compontent,@Service,@Controller,@Repository注解,如果有,则spring容器创建该类的实例
第二次,解析到aop的注解解析器,会在纳入spring管理的bean中,看哪个类上面是否有@Aspect注解
第三次,在有@Aspect注解的类的所有方法中查找@Pointcut,就会找到切入点表达式,根据切入点表达式,在纳入spring范围的bean内查找,看哪个bean符合切入点表达式,如果符合则创建代理对象
当客户端访问某一个bean时,如果该bean有代理对象,则返回代理对象,否则返回该bean的对象
最后总结: