Spring中AOP下的AspectJ使用
1、导包:spring-aspects-4.3.18.RELEASE.jar(新版IDEA创建Spring项目时会自动下载这个包,没有的就需要下载,确认有aspects这个包)
2、配置:
<?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:annotation-config/>
<!--注解的位置-->
<context:component-scan base-package="com.hwt"></context:component-scan>
<aop:config proxy-target-class="true">
<aop:aspect ref="myAspect2">
<aop:pointcut id="myPointcut" expression="execution(* com.hwt.service.*.*(..))"/>
<!--配置前置通知-->
<aop:before method="myBefore" pointcut-ref="myPointcut"/>
<!--配置后置通知-->
<aop:after-returning method="myAfterReturning" pointcut-ref="myPointcut" returning="retValue"/>
<!--配置环绕通知-->
<aop:around method="myAround" pointcut-ref="myPointcut"/>
<!--配置异常通知-->
<aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointcut" throwing="throwable"/>
<!--配置最终通知-->
<aop:after method="myAfter" pointcut-ref="myPointcut" />
</aop:aspect>
</aop:config>
</beans>
3、切面类(增强类):
PS:此种方法不用继承类
package com.hwt.service;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;
@Component("myAspect2")
public class MyAspect2{
public void myBefore(){
System.out.println("myBefore前置通知。。。");
}
public void myAfter(){
System.out.println("myAfter最终通知。。。");
}
public void myAfterReturning(JoinPoint joinPoint,Object retValue) throws Throwable {
System.out.println("myAfterReturning后置通知+返回值。。。"+retValue);
}
public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("myAround环绕通知。。。");
System.out.println("myAround开启事务。。。");
System.out.println("myAround关闭事务。。。");
proceedingJoinPoint.proceed();
return proceedingJoinPoint.proceed();
}
public void myAfterThrowing(JoinPoint joinPoint,Throwable throwable) throws Throwable {
System.out.println("myAfterThrowing异常通知+异常。。。"+ throwable.getMessage());
}
}
4、原业务类:
package com.hwt.service;
import org.springframework.stereotype.Service;
@Service("studentService")
public class StudentService {
public void delete(){
System.out.println("删除学生!");
}
public void add(){
System.out.println("添加学生!");
}
public void update(){
System.out.println("修改信息!");
}
}
5、测试类:
package com.hwt.test;
import com.hwt.service.StudentService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test05 {
@Test
public void test01(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans05.xml");
StudentService studentService = (StudentService) context.getBean("studentService");
studentService.add();
// studentService.delete();
// studentService.update();
}
}