AOP的使用

AOP

名次解释:
切入点:设置被代理的对象类,以及需要被代理的方法
切面类:为被代理的类附加方法。
实例代码:
<1>代理接口

package com.kuang.service;

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

<2>被代理的类

在这里插入代码片

<3>切面类

package com.kuang.log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class Log implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] objects, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
    }
}

package com.kuang.log;

import org.springframework.aop.AfterAdvice;
import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {

    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object target) throws Throwable {
        System.out.println("执行了"+method.getName()+"方法,返回结果为"+o);
    }
}

<4>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.kuang.service.UserServiceImpl"/>
    <bean id="log" class="com.kuang.log.Log"/>
    <bean id="afterLog" class="com.kuang.log.AfterLog"/>

    <!--配置aop-->
    <aop:config>
        <!--配置切入点 expression:表达式(要执行的位置)-->
        <!--execution(* com.kuang.service.UserServiceImpl.*(..))表示被代理类的位置-->
        <!--第一个参数*表示返回值,*表示任何种类的返回值-->
        <!--第二个参数为类的全路径-->
        <!--第二个*,表示UserServiceImpl中的所有方法-->
        <!--括号中表示参数,“(..)”表示所有种类的参数-->
        <aop:pointcut id="pointcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
        
        <!--执行环绕增加-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    </aop:config>
</beans>

<5>测试类

import com.kuang.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("aop.xml");
        //动态代理使用的是接口,则应该得到一个代理接口对象
        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }
}

执行过程:
(1)Spring加载xml文件
(2)装配bean
(3)解析aop:config
(4)通过切入点表达式,匹配上文中装配好的bean。
匹配成功:为该bean创建代理对象,代理对象的方法=目标方法+切面类方法
匹配失败:直接为该bean创建代理对象

附:
execution:


execution(public * *..))  所有的公共方法
 
execution(* set*..))  以set开头的任意方法
 
execution(* com.ci.service.AccountService.*(..)) com.cj.service.AccountService类中的所有的方法
 
execution(* com.cj.service.*.*(..))  com.cj.service包中的所有的类的所有的方法
 
execution(* com.cj.service..*.*(..)) com.cj.service包及子包中所有的类的所有的方法
 
execution(* com.cj.spring.aop..*.*(String,?,Integer))  com.cj.spring.aop包及子包中所有的类的有三个参数
                                                            且第一个参数为String,第二个参数为任意类型,
 

第二种方式:
直接定义一个切面类,里面包含代理类前后的附加方法。无需实现aop的api接口。
切面类:

package com.kuang.diy;

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

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.kuang.service.UserServiceImpl"/>
    <bean id="log" class="com.kuang.log.Log"/>
    <bean id="afterLog" class="com.kuang.log.AfterLog"/>

    <bean id="diy" class="com.kuang.diy.DIy"/>

    <aop:config>
        <aop:aspect ref="diy">
            <aop:pointcut id="pointcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>

            <aop:before method="before" pointcut-ref="pointcut"/>
            <aop:after method="after" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>
</beans>

同理也可以实现代理。


第三种方式:注解实现
定义注解实现类:

package com.kuang.diy;

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

@Aspect //标注这个类是切面类
public class AnnotationPointcut {
    @Before("execution(* com.kuang.service.UserServiceImpl.*(..))")
    public void before() {
        System.out.println("方法执行前");
    }

    @After("execution(* com.kuang.service.UserServiceImpl.*(..))")
    public void after() {
        System.out.println("方法执行后");
    }
}

附:还有其它注解
如:Around

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值