Spring中Aop的理解与代码实现

aop

aop为横向编程的思想,即在不影响原来业务类的情况下实现动态的增强。

aop作用:我们可以关注我们需要的关注的部分也就是横切关注点,如日志,安全,缓存横切插入。类似于动态代理。

切面:也就是将关注点模块化,其实就是一个类。

通知:对于切面里必须完成的工作也就是一个方法

切入点:其实就是在程序的哪个地方执行。

代理:目标通知后创建的对象(spring帮助实现)

实现方式一:使用Spring的Api接口

<?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
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
    <bean id="userService" class="com.liang.service.UserServiceImpl"/>
    <bean id="log" class="com.liang.log.Log"/>
    <bean id="afrteLog" class="com.liang.log.AfterLog"/>
<!--方式一:使用Spring API接口-->
    <aop:config>
        <!--切入点,需要在哪个地方执行-->
        <aop:pointcut id="pointcut" expression="execution(* com.liang.service.UserServiceImpl.*(..))"/>
        <!--执行环绕增加-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afrteLog" pointcut-ref="pointcut"/>
    </aop:config>

</beans>

log日志包1:

package com.liang.log;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

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

log日志包2:

package com.liang.log;


import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

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

Mytest测试类:

import com.liang.service.UserService;
import com.liang.service.UserServiceImpl;
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 =context.getBean("userService",UserService.class);
        userService.add();

    }
}

方式二:使用自定义类

<?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
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
    <bean id="userService" class="com.liang.service.UserServiceImpl"/>
    <bean id="log" class="com.liang.log.Log"/>
    <bean id="afrteLog" class="com.liang.log.AfterLog"/>
    <bean id="diy" class="com.liang.diy.DiyPointCut"/>

    <aop:config>
        <aop:aspect ref="diy">
        <aop:pointcut id="point" expression="execution(* com.liang.service.UserServiceImpl.*(..))"/>
         <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>
</beans>

自定义类为:

package com.liang.diy;

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

实现方式三:使用注解来实现spring

package com.liang.diy;

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;

//使用注解实现aop
@Aspect
public class AnnotationPointCut {
    @Before("execution(* com.liang.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("方法执行前");
    }
    @After("execution(* com.liang.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("方法执行后");
    }
    @Around("execution(* com.liang.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("环绕前");
        Object proceed=jp.proceed();
        System.out.println("环绕后");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值