【AOP系列】(三)—采用Spring的注解(Annotation)实现AOP

使用注解实现Spring AOP
本文介绍如何通过注解方式实现Spring AOP,包括配置依赖包、模块化横切关注点、定义切入点和建议、配置IoC容器、启用AspectJ注解支持,以及开发客户端。注解方式在维护成本和开发效率上有显著优势,但需注意注解修改后的重新编译问题及代码简洁度影响。

前提

  这篇文章要采用注解的方式实现AOP,这种方式写起来很简单,但是需要具备对基础的概念的理解,大家可以参考上一篇博文【AOP系列】(二)—AOP相关概念 来回顾一下。

实现步骤

1、spring的依赖包配置(cglib的jar包可以不引用)

这里写图片描述


2、将横切性关注点模块化,建立LogHandler.java


3、采用注解指定LogHandler为Aspect


4、采用注解定义Advice和Pointcut

package com.tgb.spring;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class LogHandler {
    /**
     * 定义Pointcut,PointCut的名称就是addAddMethod(),此方法没有返回值和参数
     * 该方法就是一个标识,不进行调用
     * execution是表达式,表示匹配哪类方法,
     * 第一个*:方法的返回值任意
     * add* :表示方法只要以add开头就可
     * ..   :表示方法的参数任意
     */
    @Pointcut("execution(* add*(..))")
    private void addAddMethod(){};
    /*
     * 定义Advice
     * Before表示Advice在目标方法的什么位置应用
     * addAddMethod()表示按照这种Pointcut应用到JoinPoint上
     */
    @Before("addAddMethod()")
    public void WriteLog(){
        System.out.println("打印日志……………………………………");
    }
}

5、启用AspectJ对Annotation的支持,并且将目标类和Aspect类配置到IoC容器中

<?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:tx="http://www.springframework.org/schema/tx"
    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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd             
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

    <!--启用AspectJ对Annotation的支持  -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    <bean id="userManager" class="com.tgb.spring.UserManagerImpl"></bean>
    <bean id="logHandler" class="com.tgb.spring.LogHandler"></bean>
</beans>

6、开发客户端

package com.tgb.spring;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client {
    public static void main(String[] args){
        BeanFactory factory= new ClassPathXmlApplicationContext("applicationContext.xml");
        UserManager userManager=(UserManager)factory.getBean("userManager");
        userManager.addUser("1", "zhangsan");
    }
}

总结

★注解方式优点:

1、在class文件中,可以降低维护成本

2、提高开发效率。

★注解方式缺点:

1、如果对注解进行修改,需要重新编译整个工程。

2、程序中过多的annotation,对于代码的简洁度有一定影响。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值