spring框架学习12 AOP实现

 

方式一 使用spring api接口实现   方式二自定义类  方式三使用注解

业务代码

public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void select();
}
public class UserServiceImpl implements UserService{
    public void add() {
        System.out.println("add a user");
    }

    public void delete() {
        System.out.println("delete a user");
    }

    public void update() {
        System.out.println("update a user");
    }

    public void select() {
        System.out.println("select a user");
    }
}

api接口1代码 方法一使用

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class Log implements MethodBeforeAdvice {

    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println(o.getClass().getName()+"的"+method.getName()+"被执行了");
    }
}

 api接口2代码 方法一使用

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("执行了"+method.getName()+"方法,返回结果为:"+o);
    }
}

 自定义类 方法二使用

public class DiyPointCut {
    public void before(){
        System.out.println("方法被执行前");
    }

    public void after(){
        System.out.println("方法被执行后");
    }
}

自定义类 方法三使用

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

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

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

    //在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点
    @Around("execution(* com.gyc.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint jp) throws Throwable{
        System.out.println("环绕前");
        Object proceed = jp.proceed(); //执行方法
        System.out.println("环绕后");
    }
}

 配置代码

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

        <!--方式一,使用原生Spring API接口-->
        <!--配置aop,需要带入aop的约束-->
<!--        <aop:config>-->
<!--            &lt;!&ndash;切入点:expression 表达式execution(要执行的位置!* * * * *)&ndash;&gt;-->
<!--            <aop:pointcut id="pointcut" expression="execution(* com.gyc.service.UserServiceImpl.*(..))"/>-->

<!--            &lt;!&ndash;执行环绕增加&ndash;&gt;-->
<!--            <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>-->
<!--            <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>-->
<!--        </aop:config>-->


         <!--方式二,自定义类-->
<!--         <bean id="diy" class="com.gyc.diy.DiyPointCut"/>-->

<!--         <aop:config>-->
<!--             &lt;!&ndash;自定义切面,ref是要引用的类&ndash;&gt;-->
<!--             <aop:aspect ref="diy">-->
<!--                 &lt;!&ndash;切入点&ndash;&gt;-->
<!--                 <aop:pointcut id="pointcut" expression="execution(* com.gyc.service.UserServiceImpl.*(..))"/>-->
<!--                 &lt;!&ndash;通知&ndash;&gt;-->
<!--                 <aop:before method="before" pointcut-ref="pointcut"/>-->
<!--                 <aop:after method="after" pointcut-ref="pointcut"/>-->
<!--             </aop:aspect>-->
<!--         </aop:config>-->

        <!--方式三,自定义类-->
        <bean id="annotationPointCut" class="com.gyc.diy.AnnotationPointCut"/>
        <!--开启注解支持-->
        <aop:aspectj-autoproxy/>

</beans>

 测试代码

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context =  new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理代理的是接口
         UserService userService = (UserService) context.getBean("userService");
         userService.add();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值