Spring5【学习笔记三】

AOP操作术语

  1. 连接点:类里呢些方法可以被增强,这些方法被称为连接点
  2. 切入点:实际被真正增强的方法
  3. 通知(增强)
  1. 1.实际增强的逻辑部分称为通知
  2. 2.通知有多种类型:前置通知、后置通知、环绕通知、异常通知、最终通知

4.切面

把通知应用到切入点的过程

AOP操作

  1. Spring框架一般都是基于AspectJ实现AOP操作

AspectJ不是Spring的组成部分,独立AOP框架,一般把AspectJ和Spring一起使用进行AOP操作。

添加依赖和约束

前置通知实现MethodBeforeAdvice接口 

public class MyAOPBeforeConfig implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println(method.getName()+"方法之前执行语句");
    }
}

后置通知实现AfterReturningAdvice接口

public class MyAOPAfterConfig implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println(method.getName()+"方法之后执行语句。。");
    }
}

将所有方法写入bean配置文件中

 <bean id="userServiceImp" class="com.wang.service.UserServiceImp"/>
    <bean id="beforeConfig" class="com.wang.config.MyAOPBeforeConfig"/>
    <bean id="afterConfig" class="com.wang.config.MyAOPAfterConfig"/>

进行AOP配置:

首先配置切入点:id和expression(executor(方法位置))

* com.wang.service.UserServiceImp.*(..)

第一个*代表所有返回类型,第二个代表UserServiceImp类中的所有方法

..代表每个方法的参数。

然后配置环绕通知advisor:id、advice-ref(增强方法的逻辑bean)、pointcut-ref(切入点的id)

    <aop:config>
    <!--切入点(被增强的方法在UserServiceImp类中的所有方法*,返回类型*,参数多个..)-->
        <aop:pointcut id="pointcut" expression="execution(* com.wang.service.UserServiceImp.*(..))"/>
        <aop:advisor id="before" advice-ref="beforeConfig" pointcut-ref="pointcut"/>
        <aop:advisor id="after" advice-ref="afterConfig" pointcut-ref="pointcut"/>
    </aop:config>
@Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        UserService userServiceImp = context.getBean("userServiceImp", UserService.class);
        userServiceImp.add();
    }

如果不是实现指定接口,自定义环绕通知配置类:

//自定义的通知
public class AOPConfig {
    public void before(){
        System.out.println("自定义的配置。。。方法执行前");
    }
    public void after(){
        System.out.println("自定义的配置。。。方法执行后");
    }
}
    <bean id="myConfig" class="com.wang.config.AOPConfig"/>
    <aop:config>
        <aop:aspect ref="myConfig">
            <aop:pointcut id="pointcut" expression="execution(* com.wang.service.UserServiceImp.*(..))"/>
            <aop:before method="before" pointcut-ref="pointcut"/>
            <aop:after method="after" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>

首先先将自定义配置类注册在bean中,同样配置aop,使用自定义切面aop:aspect映射自定义配置类的bean,同样在里面定义切入点,和环绕方法

 

注解实现AOP

@Aspect自定义切面

@Before切入点执行前的方法,内部写切入点的具体位置具体方法

@Aspect
public class AnnotationConfig {
    @Before("execution(* com.wang.service.UserServiceImp.*(..))")
    public void before(){
        System.out.println("注解方法。。。执行前");
    }
    @After("execution(* com.wang.service.UserServiceImp.*(..))")
    public void after(){
        System.out.println("注解方法。。。执行后");
    }
    @Around("execution(* com.wang.service.UserServiceImp.*(..))")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("注解方法。。。环绕前");
        point.proceed();
        System.out.println("注解方法。。。环绕后");
        return null;
    }
}

要注意,上面的注解要先导入依赖

注解类注册,<aop:aspecttj-autoproxy>开启注解aop,默认是false可以直接圣罗不写(JDK动态代理,true是CGLIB动态代理)

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值