9、spring-AOP

为什么学习AOP?
在不修改源码的情况下,对程序进行增强
AOP可以进行权限校验,日志记录,性能监控,事务控制。
导包

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.9</version>
</dependency>

方式一,使用原生的Spring API接口
步骤:
1、接口

public interface UserService {
    void add1();

    void del();

    void update();

    void query();
}

2、实现类

public class UserServiceImpl implements UserService {
    @Override
    public void add1() {
        System.out.println("增加了一个用户");
    }

    @Override
    public void del() {
        System.out.println("删除了一个用户");
    }

    @Override
    public void update() {
        System.out.println("修改了一个用户");
    }

    @Override
    public void query() {
        System.out.println("查询了一个用户");
    }
}

3、日志类

public class BeforeLog implements MethodBeforeAdvice {

    //method:要执行的目标参数的方法
    //args:参数
    //target:目标对象或接口
    @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 {
    //method:要执行的目标参数的方法
    //args:参数
    //target:目标对象或接口
    //returnValue:返回值
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("执行了" + method.getName() + "方法,返回结果是" + returnValue);

    }
}

4、配置文件

<?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:context="http://www.springframework.org/schema/context"
       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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--xmlns:context="http://www.springframework.org/schema/context" 注意别导错了-->
    <!--开启组件扫描功能 扫描cn.zhy.pojo包下所有类是否添加了@Component注解-->
    <context:component-scan base-package="cn.zhy"/>
    <context:annotation-config/>
    <!--    注册bean-->
    <bean id="userService" class="cn.zhy.service.UserServiceImpl"/>
    <bean id="beforeLog" class="cn.zhy.log.BeforeLog"/>
    <bean id="afterLog" class="cn.zhy.log.AfterLog"/>
    <!--方式一,使用原生的Spring  API接口-->
    <!--配置aop:需要导入aop约束-->
    <aop:config>
        <!--切入点  expression表达式,execution(要执行的位置)-->
        <aop:pointcut id="pointcut" expression="execution(* cn.zhy.service.UserServiceImpl.*(..))"/>
        <!--执行环绕增加-->
        <aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    </aop:config>
</beans>

5、测试

public class Mytest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理代理的是接口,因此要返回接口UserService
        UserService userService = (UserService) context.getBean("userService");
        userService.add1();
    }
}

结果

方式二:自定义类实现aop
步骤
1、接口
2、实现类
3、自定义日志类

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

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

4、配置文件

<!--方式二:自定义类实现aop-->
<bean class="cn.zhy.diy.DiyPointCut" id="diy"/>
<aop:config>
  <aop:aspect ref="diy">
      <aop:pointcut id="point" expression="execution(* cn.zhy.service.UserServiceImpl.*(..))"/>
      <!--通知,设置方法执行位置-->
      <aop:before method="before" pointcut-ref="point"/>
      <aop:after method="after" pointcut-ref="point"/>
  </aop:aspect>
</aop:config>

5、测试
6、结果

方式三:使用注解实现aop

//使用注解方式实现aop
//@Aspect标注这个类是一个切面
@Aspect
public class AnnotationAop {

    @Before("execution(* cn.zhy.service.UserServiceImpl.*(..))")
    public void before() {
        System.out.println("方法执行前");
    }

    @After("execution(* cn.zhy.service.UserServiceImpl.*(..))")
    public void after() {
        System.out.println("方法执行后");
    }
    //在环绕 增强中,我们可以给定一个参数,代表我们获取处理的切入点
    @Around("execution(* cn.zhy.service.UserServiceImpl.*(..))")
    public void arround(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕前");
        //执行方法
        Object proceed = joinPoint.proceed();
        System.out.println("环绕后");
    }
}
<!--方式三-->
    <bean class="cn.zhy.diy.AnnotationAop" id="annotationAop"/>
    <!--开启Aop注解支持-->
    <aop:aspectj-autoproxy/>

反射设置属性值

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值