spring(二)aop的使用

aop概念

AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

作用:Aop 在 不改变原有代码的情况下 , 去增加新的功能 。

aop的使用

一、导入依赖

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.8</version>
</dependency>

二、aop的三种实现

1、编写增强类

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

在配置文件中添加aop约束,并实现aop

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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="userServiceImpl" class="com.tao.service.UserServiceImpl"/>
    <bean id="beforeLog" class="com.tao.log.BeforeLog"/>
    <bean id="afterLog" class="com.tao.log.AfterLog"/>

    <aop:config>
        <!--   设置切入点     -->
        <aop:pointcut id="cut" expression="execution(* com.tao.service.UserServiceImpl.*(..))"/>
        <!--   执行环绕     -->
        <aop:advisor advice-ref="beforeLog" pointcut-ref="cut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="cut"/>
    </aop:config>

</beans>

2、自定义类实现

public class DiyLog {

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

    public void after(){
        System.out.println("执行方法后");
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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="userServiceImpl" class="com.tao.service.UserServiceImpl"/>
    <bean id="diyLog" class="com.tao.log.DiyLog"/>

    <aop:config>
        <aop:aspect id="asp" ref="diyLog">
            <aop:pointcut id="cut" expression="execution(* com.tao.service.*.*(..))"/>
            <aop:before method="before" pointcut-ref="cut"/>
            <aop:after method="after" pointcut-ref="cut"/>
        </aop:aspect>
    </aop:config>

</beans>

3、使用注解实现

@Aspect
class AnnotationPointcut {

    @Before("execution(* com.tao.service.*.*(..))")
    public void beforeLog(){
        System.out.println("执行方法前");
    }

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

    @Around("execution(* com.tao.service.*.*(..))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        //执行方法
        Object proceed = pjp.proceed();
        return proceed;
    }
}

在xml配置中还需要添加注解开启

<aop:aspectj-autoproxy/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值