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();
}
}