Spring:09-2 基于注解配置的AOP程序开发

Spring:基于注解配置的AOP程序开发

以基于JDK的动态代理为例(即都会有一个接口)

此处基于注解配置,还需要配以XML配置。----“XML配置+注解配置

实现步骤:

1、依赖导入:AOP织入依赖 aspectjweaver 第三方的框架,spring-aop组件中引入了它

2、定义目标接口

3、定义目标对象类

4、定义切面类

5、xml补充配置 部分内容不便用注解配置的,用xml来进行配置更方便

6、编写测试代码

1、依赖导入:AOP织入依赖 aspectjweaver

省略! 同基于xml配置开发AOP程序

2、定义目标接口

public interface TargetInterface {
    void save();
}

3、定义目标对象类

需要加@Component注解,将其对象的创建交由IoC容器

这里因为还没有集成web项目,不知道这个Target对象放的是哪一层,故直接用@Component注解。

@Component("target")//加个名字,便于从IoC容器中获取它
public class Target  implements TargetInterface{
    public void save(){
        System.out.println("Target:save() running........");
    }
}

4、定义切面类

Spring框架提供了5种增强类型。

使用注解配置时,切面类中的内容就变得多了。----将原来在XML中配置的内容,用注解进行了替代。

@Component("myAspect")
@Aspect//声明切面类
@Order//可以控制增强的顺序
public class MyAspect {
    //--------定义切点表达式----------------
    /*
        当一个切点表达式,被多个增强方法使用时,将其抽取出来是十分必要的。
        (1)定义切点表达式
        (2)增强方法引用切点表达式
            方式1:@Before("pointcutExp()")
                切点表达式的定义方法与引用切点表达式的增强方法,在同一个类文件中。
            方式2:@Before("MyAspect.pointcutExp()")
                切点表达式的定义方法与引用切点表达式的增强方法,不在同一个类文件中。
                比如:
                    可以将pointcutExp()方法写到类ClassA中去,MyAspect类中的各个增强方法
                    通过ClassA.pointcutExp()来引用。

     */
    @Pointcut("execution(* cn.itcast.aop_anno.*.*(..))")
    public void pointcutExp(){

    }

    //-------------------配置前置增强--------------------------
    @Before("pointcutExp()")
    public void before(){
        System.out.println("前置增强");
    }

    /*
    使用注解,同时配有两个前置增强时,谁先执行呢?
        1. 根据方法名,按字母自然顺序执行
        2. 切面类上,以注解@Order进行控制
     */
    @Before("pointcutExp()")
    public void a(){
        System.out.println("前置增强1");
    }

    //-----------------------配置后置增强-----------------------
    @AfterReturning("pointcutExp()")
    public void afterRunning(){
        System.out.println("后置增强");
    }

    //-----------------------环绕增强---------------------------
    /*
    ProceedingJoinPoint 正在执行的连接点,即切点
     */
    @Around("pointcutExp()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强");
        Object proceed = pjp.proceed();//切点方法
        System.out.println("环绕后增强");
        return proceed;
    }

    //------------------------异常抛出增强--------------------------
    @AfterThrowing("pointcutExp()")
    public void afterThrowing(){
        System.out.println("异常抛出增强");
    }

    //--------------------------最终增强----------------------------
    /*
    无论怎样,都会执行的增强。
        “无论怎样”是指如果程序出现异常,有的增强方式可能就执行不到,但最终增强一定会执行。
        这有点像try-catch-finally中的finally块中的代码,是一定会被执行的。
     */
    @After("pointcutExp()")
    public void after(){
        System.out.println("最终增强");
    }
}

5、xml补充配置

applicationContext.xml中配置

Bean对象声明、aop织入,都使用注解替代了。

需要配置的内容:

(1)组件扫描

​ 需要引入命名空间“context”

​ 也可以用新注解来代替,不过用xml配置这个更方便!

(2)aop自动代理

​ 这个配置,非常重要且必不可少!

​ 需要引入命名空间“aop”

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        ">

    <!--组件扫描-->
    <context:component-scan base-package="cn.leap"/>

    <!--====aop自动代理=====
        这个必不可少!
    -->
    <aop:aspectj-autoproxy/>
</beans>

6、编写测试代码

maven项目的测试包下写,使用SpringJunit来进行测试。

相较之前XML配置的测试,更改一下指定的Spring配置文件即可!

/**
 * Desc: SpringJunit测试--AOP 注解配置测试
 */
@RunWith(SpringJUnit4ClassRunner.class) //SpringJunit中指定新的运行器,由它来创建IoC窗口
@ContextConfiguration("classpath:applicationContext-anno.xml")//SpringJunit中,指定配置文件
public class AopXMLTest {

    /*
    目标方法save()方法是目标对象Target类中的方法。
    若不用AOP对其增强,可以写成private Target target 或 private TargetInterface target
        这只是普通的方法调用。
    若使用AOP对其增强,必须是private TargetInterface target
        原因:
            JDK动态代理模式中,代理对象与目标对象Target实现同一个接口,两者是兄弟关系。

     AOP中,代理对象是由IoC容器创建的。
     */
    @Autowired
    private TargetInterface target;//这个其实是代理对象

    @Test
    public void test01() {
        target.save();
    }
}

控制台打印内容:

环绕前增强
前置增强1
前置增强
Target:save() running........
环绕后增强
最终增强
后置增强

分析总结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值