spring_aop_学习笔记

aop及其实现的方法

1 什么是aop

aop 即 面向切面编程

横切关注点: 跨越应用程序的多个模块的方法或功能,与我们业务逻辑无关的,但是需要我们关注的部分,就是横切关注点。如日志,安全,缓存,事务等等。

切面(Aspect): 切面是一个横切关注点的模块化的特殊对象,即,他是一个类。

通知(advice): 切面必须完成的工作,即,他是一个方法。

目标(target): 被通知的对象。

代理(proxy): 向目标对象应用通知之后创建的对象。

切入点(pointcut): 切面通知执行的地点。

连接点(joinPoint): 被拦截到的程序执行点

2 aop的作用

AOP 主要用来解决,在不改变原有业务逻辑的情况下,增强横切逻辑代码,根本上解耦合,避免横切逻辑代码重复。

3 aop的好处

1、减少重复代码
2、提高开发效率
3、维护方便

4 实现aop的三种方式

准备工作
采用一个对用户的CRUD的例子
定义一个CRUD接口

public interface UserService {
    void add();
    void delete();
    void update();
    void select();
}

接口实现类

public class UserServiceImpl implements UserService{
    @Override
    public void add() {
        System.out.println("增加了一个User!");
    }

    @Override
    public void delete() {
        System.out.println("删除了一个User!");
    }

    @Override
    public void update() {
        System.out.println("更新了一个User!");
    }

    @Override
    public void select() {
        System.out.println("查询了一个User!");
    }
}

log类

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class Log implements MethodBeforeAdvice {
    //method 要执行的目标对象的方法
    //args 参数
    //target 目标对象
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println("[log] "+target.getClass().getName()+"的"+method.getName()+"被执行了");
    }
}

AfterLog类

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {
    //returnValue 返回值
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("执行了"+method.getName()+"返回的值为:"+returnValue);
    }
}

配置spring applicationContext.xml
一定要导入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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
		https://www.springframework.org/schema/aop/spring-aop.xsd>

    <bean id="userService" class="com.wq.service.UserServiceImpl"/>
    <bean id="log" class="com.wq.log.Log"/>
    <bean id="afterLog" class="com.wq.log.AfterLog"/>
    
</beans>

测试类

import com.wq.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = context.getBean("userService", UserService.class);
        userService.add();
    }
}

4.1 spring的API接口实现aop

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

    <bean id="userService" class="com.wq.service.UserServiceImpl"/>
    <bean id="log" class="com.wq.log.Log"/>
    <bean id="afterLog" class="com.wq.log.AfterLog"/>

    <!--  配置AOP 需要先导入aop的约束-->
    <!--  方式一   -->
    <aop:config>
        <!-- 切入点
                expression:表达式,execution(要执行的位置)  -->
        <aop:pointcut id="pointcut" expression="execution(* com.wq.service.UserServiceImpl.*(..) )"/>

        <!-- 执行环绕增加-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    </aop:config>
</beans>

测试示例

[log] com.wq.service.UserServiceImpl的add被执行了
增加了一个User!
执行了add返回的值为:null

4.2 自定义实现aop

自定义pointcut

public class DiyPointcut {
    public void before(){
        System.out.println("-------方法执行前------");
    }
    public void after(){
        System.out.println("-------方法执行后------");
    }
}

在xml中注册<bean id="diy" class="com.wq.diy.DiyPointcut"/>
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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
		https://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="userService" class="com.wq.service.UserServiceImpl"/>
    <bean id="log" class="com.wq.log.Log"/>
    <bean id="afterLog" class="com.wq.log.AfterLog"/>
    <bean id="diy" class="com.wq.diy.DiyPointcut"/>
    <!--  方式二  -->
    <aop:config>
        <!-- 自定义切面 ref->要引用的类 -->
        <aop:aspect id="diy" ref="diy">
            <!-- 切入点  -->
            <aop:pointcut id="point" expression="execution(* com.wq.service.UserServiceImpl.*(..))"/>
            <!-- 通知 -->
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>
</beans>

测试示例

-------方法执行前------
增加了一个User!
-------方法执行后------

4.3 注解实现aop

自定义pointcut

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class AnnotationPointcut {
    @Before("execution(* com.wq.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("-------方法执行前------");
    }
    @After("execution(* com.wq.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("-------方法执行后------");
    }
}

这里采用的是注解方式实现的
@aspect 标注的是这个类是一个切面
@Before("execution(* com.wq.service.UserServiceImpl.*(..))")@After("execution(* com.wq.service.UserServiceImpl.*(..))")与第二种方式的

<!-- 切入点  -->
            <aop:pointcut id="point" expression="execution(* com.wq.service.UserServiceImpl.*(..))"/>
            <!-- 通知 -->
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>

作用相同

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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
		https://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/context
		https://www.springframework.org/schema/context/spring-context.xsd">
    <!--  开启注解支持 及 扫描包的支持  -->
    <context:component-scan base-package="com"/>
    <context:annotation-config/>

    <bean id="userService" class="com.wq.service.UserServiceImpl"/>
    <bean id="log" class="com.wq.log.Log"/>
    <bean id="afterLog" class="com.wq.log.AfterLog"/>

    <!--方式三-->
    <!--  开启注解支持 默认JDK(proxy-target-class="false") 可改 cglib(proxy-target-class="true")  结果无区别-->
    <aop:aspectj-autoproxy/>
</beans>

测试示例

-------方法执行前------
增加了一个User!
-------方法执行后------
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值