aspectj aop 切面与切点

可以在方法加入 joinPoint 获取切点信息


切面:
@Aspect
public class AopAspectJ
{
// 前置通知:方法执行之前获得增强
@Before ( value = "savePoint()" )
public void beforAop ( JoinPoint joinPoint )
{
System . out . println ( "this is before aop and joinPoint : " + joinPoint ) ;
}

// 后置通知:可以获取方法返回值, result 的名字可以任意取名,但是需要和方法对应
@AfterReturning ( value = "execution(* com.imooc.service.TestService.update())" , returning = "result" )
public void afterReturningAop ( Object result )
{
System . out . println ( "This is after retruning aop and result :" + result ) ;
}

// 环绕通知:如果不调用 joinPoint.proceed(); 则目标方法不会执行
@Around ( value = "execution(* com.imooc.service.TestService.delete(..))" )
public Object aroundAop ( ProceedingJoinPoint joinPoint ) throws Throwable
{
System . out . println ( "This is around aop --- before" ) ;

Object proceed = joinPoint . proceed () ;

System . out . println ( "This is around aop --- after" ) ;

return proceed ;
}

// 异常抛出通知:可以获取异常属性
@AfterThrowing ( value = "execution(* com.imooc.service.TestService.wrong(..))" , throwing = "e" )
public void throwAop ( Exception e )
{
System . out . println ( "this is throw Aop and e : " + e ) ;
}

// 最终通知:总是会被执行 ( 即使发生异常 ) ,相当于 fianally
@After ( value = "execution(* com.imooc.service.TestService.*(..))" )
public void afterAop ()
{
System . out . println ( "This is after aop ,the last advice" ) ;
}

// 切点: public void 无参方法,方法名为切点 使用 @Pointcut 进行定义。若存在多个切点,可以使用 || 分隔
@Pointcut ( value = "execution(* com.imooc.service.TestService.save(..)) || execution(* com.imooc.service.TestService.update())" )
private void savePoint ()
{
}
}



切点:
public class TestServiceImpl implements TestService
{
@Override
public void save ()
{
System . out . println ( "This is save method" ) ;
}
}


测试类:
@RunWith ( SpringJUnit4ClassRunner . class )
@ContextConfiguration ( "classpath:applicationContext.xml" )
public class DemoTest
{
@Resource
private TestService testService ;

@Test
public void demo ()
{
testService . save () ;
getPrintln () ;
testService . update () ;
getPrintln () ;
testService . delete () ;
getPrintln () ;
testService . wrong () ;
}

private void getPrintln ()
{
System . out . println ( "---------------------" ) ;
}
}

// 切面写完了之后需要到xml对bean进行定义
<? 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd" >

< aop :aspectj-autoproxy />
< context :component-scan base-package ="com.imooc" />

<!-- 切面需要申明 -->
< bean class ="com.imooc.aop.AopAspectJ" />


</ beans >



下面给出XML的配置方法:
使用xml后,切面类不需要注解

< context :component-scan base-package ="com.imooc.xmlaspectj" />

< bean id ="AopAspectJWithXml" class ="com.imooc.xmlaspectj.aopwithxml.AopAspectJWithXml" />

<!--xml 配置 -->
< aop :config >
<!-- 切点 -->
< aop :pointcut id ="TestWithXmlService"
expression ="execution(* com.imooc.xmlaspectj.servicewithxml.TestWithXmlService.save())" />
<!-- 切面 -->
< aop :aspect ref ="AopAspectJWithXml" >
< aop :before method ="beforAop" pointcut-ref ="TestWithXmlService" />
< aop :after-returning method ="afterReturningAop" pointcut-ref ="TestWithXmlService" returning ="result" />
</ aop :aspect >
</ aop :config >



poml.xml
< dependencies >
< dependency >
< groupId > junit </ groupId >
< artifactId > junit </ artifactId >
< version > 4.12 </ version >
</ dependency >
<!-- 引入 Spring 的基本开发包 -->
< dependency >
< groupId > org.springframework </ groupId >
< artifactId > spring-core </ artifactId >
< version > 4.2.4.RELEASE </ version >
</ dependency >
< dependency >
< groupId > org.springframework </ groupId >
< artifactId > spring-context </ artifactId >
< version > 4.2.4.RELEASE </ version >
</ dependency >
< dependency >
< groupId > org.springframework </ groupId >
< artifactId > spring-beans </ artifactId >
< version > 4.2.4.RELEASE </ version >
</ dependency >
< dependency >
< groupId > org.springframework </ groupId >
< artifactId > spring-expression </ artifactId >
< version > 4.2.4.RELEASE </ version >
</ dependency >

< dependency >
< groupId > aopalliance </ groupId >
< artifactId > aopalliance </ artifactId >
< version > 1.0 </ version >
</ dependency >
< dependency >
< groupId > org.springframework </ groupId >
< artifactId > spring-aop </ artifactId >
< version > 4.2.4.RELEASE </ version >
</ dependency >

< dependency >
< groupId > org.aspectj </ groupId >
< artifactId > aspectjweaver </ artifactId >
< version > 1.8.9 </ version >
</ dependency >
< dependency >
< groupId > org.springframework </ groupId >
< artifactId > spring-aspects </ artifactId >
< version > 4.2.4.RELEASE </ version >
</ dependency >

< dependency >
< groupId > org.springframework </ groupId >
< artifactId > spring-test </ artifactId >
< version > 4.2.4.RELEASE </ version >
</ dependency >
</ dependencies >

猜你喜欢

转载自blog.csdn.net/zhou199252/article/details/80741565