spring-aop AspectJ五种类型

AfterReturnAdvice.java

package com.gitv.advice;

 

public class AfterReturnAdvice {

public void doAfter(){

System.out.println("增强功能之后的方法");

}

public void doAfter(String returnValue){

// System.out.println("增强功能之后的方法-->"+returnValue);

// } //spring-xml中 增加 returning="returnValue" 参数

 

}

BeforeAdvice.java

package com.gitv.advice;

 

public class BeforeAdvice {

public void doBefore(){

System.out.println("增强功能之前的方法");

}

// public void doBefore(JoinPoint joinPoint){ //可以传入参数

// System.out.println("增强功能之前的方法->"+joinPoint);

// }

}

AfterThrowingAdvice.java

package com.gitv.advice;

public class AfterThrowingAdvice {

public void doAfterThrow(Throwable throwable) {

System.out.println("异常之后的方法--->" + throwable);

}

 

}

AroundAdvice.java

package com.gitv.advice;

import org.aspectj.lang.ProceedingJoinPoint;

public class AroundAdvice {

public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

System.out.println("环绕通知在前面的功能");

Object proceed = proceedingJoinPoint.proceed();

System.out.println("环绕通知在后面的功能");

return proceed;

 

}

}

UserService.java

package cn.gtiv.service;

 

public interface UserService {

public void testUserService();

}

UserServiceImpl.java

package com.gitv.service;

 

import org.springframework.stereotype.Service;

 

@Service

public class UserServiceImpl implements UserService {

@Override

public void testUserService() {

//System.out.println(10/0);

System.out.println("UserServiceImpl.testUserService----核心功能");

}

}

spring-config.xml

<?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">

 

<context:component-scan base-package="com.gitv.service">

</context:component-scan>

 

<bean class="com.gitv.advice.BeforeAdvice" id="beforeAdvice"></bean>

<bean class="com.gitv.advice.AfterReturnAdvice" id="afterReturnAdvice"></bean>

<bean class="com.gitv.advice.AfterThrowingAdvice" id="afterThrowingAdvice"></bean>

<aop:config>

<aop:pointcut id="pointcut1" expression="bean(userServiceImpl)"></aop:pointcut>

<!--

aop:aspect 用来构建切面 ref:指明我们当前切面所执行的具体功能,填入一个advice

aop:before 用来指明一个前置切面功能,即在目标方法之前执行

aop:after 用来指明一个前置切面功能,即在目标方法之后执行,抛出异常之前

aop:after-returning(只执行其中一个)用来指明一个前置切面功能,即在目标方法之后执行

aop:after-throwing(只执行其中一个)用来指明一个前置切面功能,即在目标方法之后抛出异常执行

method:具体执行什么功能

pointcut-ref:来指明目标的方法,即切点

-->

<aop:aspect ref="beforeAdvice">

<aop:before method="doBefore" pointcut-ref="pointcut1"></aop:before>

</aop:aspect>

<aop:aspect ref="afterReturnAdvice">

<aop:after-returning method="doAfter" pointcut-ref="pointcut1"></aop:after-returning>

</aop:aspect>

<aop:aspect ref="afterThrowingAdvice">

<aop:after-throwing method="doAfterThrow" pointcut-ref="pointcut1" throwing="throwable"></aop:after-throwing>

</aop:aspect>

<aop:aspect ref="aroundAdvice">

<aop:around method="doAround" pointcut-ref="pointcut1" arg-names="proceedingJoinPoint"></aop:around>

</aop:aspect>

</aop:config>

</beans>

Test.java

public class Test {

public static void main(String[] args) {

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");

UserService userService = applicationContext.getBean(UserService.class);

userService.testUserService();

 

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值