Spring AOP之@Aspect

本文介绍如何使用Spring的AOP功能,通过XML配置和注解方式实现业务逻辑的增强。包括如何定义切面、切入点和通知,以及如何进行基于类的代理。

不知道从何时开始,流行用注解形式来做配置,要去XML,其实怎么配置都差多!

xml

首先来看Spring的bean xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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/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">

  <!--扫描所有component、service、repository,注册为bean-->
  <context:component-scan base-package="org.lanqiao.springDemo.aop"/>
  <!-- 定义扫描@aspectj -->
  <aop:aspectj-autoproxy/>

</beans>

功能看注释就可以了解,下面先定义一个新的可以被扫描的业务bean:

业务bean

package org.lanqiao.springDemo.aop;

import org.springframework.stereotype.Service;

@Service
public class NoBiz implements SomeBiz {
  public void addBiz() {
    System.out.println("NoBiz------新增数据的业务逻辑");

  }

  public void other() {
    System.out.println("NoBiz---------其他方法");

  }
}

注意@Service,这个注解配合XML中声明的<context:component-scan就可以完成bean的声明。

业务bean相比之前无其他特点。

注解+切面

然后是注解配置的切面,Advice2:

package org.lanqiao.springDemo.aop;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class Advice2  {
  @Pointcut("execution(* org.lanqiao..*Biz*.*Biz(..))")
  public void bizPointcut(){}


  @Before("bizPointcut()")
  public void invoke(JoinPoint joinPoint) throws Throwable {
    System.out.printf("代理逻辑发生在调用方法%s之前\n", joinPoint.toLongString());
  }
}

其中@Component是标注该类可以被spring的bean扫描器扫到;
@Aspect标注这是一个切面;
@Pointcut声明了一个切入点表达式;
@Before声明了其下的方法是一个前置通知的逻辑;方法参数一个连接点数据的封装——JoinPoint对象。

测试

测试代码:

package org.lanqiao.springDemo.aop;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

import static org.junit.Assert.*;

@ContextConfiguration("classpath:aop3.xml")
public class NoBizTest extends AbstractJUnit4SpringContextTests{
  @Autowired
  private SomeBiz noBiz;
  @Test
  public void addBiz() throws Exception {
    noBiz.addBiz();
  }

  @Test
  public void other() throws Exception {
  }

}

基于类的代理

如果NoBiz并未实现某种接口,那我们就要启动基于类的字节码代理,而不是用JDK的动态代理。

<!--开启基于类的代理-->
  <aop:aspectj-autoproxy proxy-target-class="true"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值