(10) 使用Spring的注解方式实现AOP入门 以及 细节

来源:互联网 发布:网络优化课程 编辑:程序博客网 时间:2024/05/01 16:38

1:AOP的概念

Aspect(切面):指横切性关注点的抽象即为切面,它与类相似,只是两者的关注点不一样,类是对物体特征的抽象,而切面横切性关注点的抽象.

joinpoint(连接点):所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点,实际上joinpoint还可以是field或类构造器)

Pointcut(切入点):所谓切入点是指我们要对那些joinpoint进行拦截的定义.

Advice(通知):所谓通知是指拦截到joinpoint之后所要做的事情就是通知.通知分为前置通知,后置通知,异常通知,最终通知,环绕通知

Target(目标对象):代理的目标对象

Weave(织入):指将aspects应用到target对象并导致proxy对象创建的过程称为织入.

Introduction(引入):在不修改类代码的前提下, Introduction可以在运行期为类动态地添加一些方法或Field.



2:使用Spring进行面向切面(AOP)编程

要进行AOP编程,首先我们要在spring的配置文件中引入aop命名空间:
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/aop

            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
</beans>

Spring提供了两种切面声明方式,实际工作中我们可以选用其中一种:
基于XML配置方式声明切面。
基于注解方式声明切面。


3,基于注解方式声明切面:


首先启动对@AspectJ注解的支持(蓝色部分):
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
     <aop:aspectj-autoproxy/>
    <bean id="orderservice" class="cn.itcast.service.OrderServiceBean"/>
    <bean id="log" class="cn.itcast.service.LogPrint"/>
</beans>


实例演戏:

package cn.itcast.service;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;/** * 切面 * */@Aspectpublic class MyInterceptor {@Pointcut("execution (* cn.itcast.service.impl.PersonServiceBean.*(..))")private void anyMethod() {}//声明一个切入点@Before("anyMethod() && args(name)")public void doAccessCheck(String name) {System.out.println("前置通知:"+ name);}@AfterReturning(pointcut="anyMethod()",returning="result")public void doAfterReturning(String result) {System.out.println("后置通知:"+ result);}@After("anyMethod()")public void doAfter() {System.out.println("最终通知");}@AfterThrowing(pointcut="anyMethod()",throwing="e")public void doAfterThrowing(Exception e) {System.out.println("例外通知:"+ e);}@Around("anyMethod()")public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {//if(){//判断用户是否在权限System.out.println("进入方法");Object result = pjp.proceed();System.out.println("退出方法");//}return result;}}

package cn.itcast.service.impl;import cn.itcast.service.PersonService;public class PersonServiceBean implements PersonService {public String getPersonName(Integer id) {System.out.println("我是getPersonName()方法");return "xxx";}public void save(String name) {//throw new RuntimeException("我例外&&&");System.out.println("我是save()方法");}public void update(String name, Integer id) {System.out.println("我是update()方法");}}

<?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">                              <aop:aspectj-autoproxy/>         <bean id="myInterceptor" class="cn.itcast.service.MyInterceptor"/>        <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean></beans>

package junit.test;import org.junit.BeforeClass;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import cn.itcast.service.PersonService;public class SpringAOPTest {@BeforeClasspublic static void setUpBeforeClass() throws Exception {}@Test public void interceptorTest(){ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");PersonService personService = (PersonService)cxt.getBean("personService");personService.save("xx");}}

打印结果:

前置通知:xx
进入方法
我是save()方法
后置通知:null
最终通知
退出方法



分析:

@Pointcut("execution(* cn.itcast.service..*.*(..))")

.. 代表 :子包下也要拦截。

execution 执行。

第一个* 代表 返回值的类型。
第二个* 代表 类。所有的类
第三个* 代表 所有的方法。

第二个 .. 代表: 随意

原创粉丝点击