1导的包和目录结构
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3c5cc885fd703c151170504647a7ab06.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1fde692d2be8b9bf8c8cd67a6b079b19.png)
2XML配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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.baidu"></context:component-scan>
<!--使注解和配置都生效-->
<context:annotation-config/>
<bean id="deptService" class="com.baidu.XML.DeptServiceImp"></bean>
<bean id="myXML" class="com.baidu.XML.MyXML"></bean>
<aop:config>
<aop:aspect ref="myXML">
<aop:pointcut id="poi1" expression="execution(* com.baidu.XML.DeptServiceImp.*(..))"></aop:pointcut>
<aop:pointcut id="poi2" expression="execution(* com.baidu.XML.DeptServiceImpl.findName(..))"></aop:pointcut>
<aop:before method="myBefore" pointcut-ref="poi1"></aop:before>
<aop:after-returning method="myAfter" pointcut-ref="poi1" returning="reval"></aop:after-returning>
</aop:aspect>
</aop:config>
</beans>
3接口和实现类
package com.baidu.XML;
public interface DeptServiceI {
void add();
void del();
String findName();
}
package com.baidu.XML;
public class DeptServiceImp implements DeptServiceI {
@Override
public void add() {
System.out.println("add---");
}
@Override
public void del() {
System.out.println("del---");
}
@Override
public String findName() {
System.out.println(" findName");
//int i=1/0;
return "Lisi";
}
}
4开发类
package com.baidu.XML;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
//纯注解开发
@Aspect
public class MyXML {
//@Before(value = "execution(* com.baidu.AspectJ.DeptServiceImp.*(..))")
public void myBefore(JoinPoint joinPoint){
System.out.println("前置增强"+joinPoint);
}
// @AfterReturning(value = "execution(* com.baidu.AspectJ.DeptServiceImp.findName(..))",returning="reval")
public void myAfter(JoinPoint joinPoint,Object reval){
System.out.println("后置增强,返回值:"+reval);
}
// @Around(value ="execution(* com.baidu.AspectJ.DeptServiceImp.findName(..))")
public Object myAround(ProceedingJoinPoint point) throws Throwable {
System.out.println("环绕通知前1");
Object returnVal=point.proceed();
System.out.println("环绕通知后2");
return returnVal;
}
//@AfterThrowing(value ="execution(* com.baidu.AspectJ.DeptServiceImp.findName(..))",throwing = "ex")
public void myAfterThrowing(JoinPoint joinPoint,Throwable ex) {
System.out.println("异常通知,异常信息是:" + ex.getMessage());
}
// @After(value ="execution(* com.baidu.AspectJ.DeptServiceImp.findName(..))")
public void myAfter() {
System.out.println("最终通知" );
}
//简化表达式
@Before(value = "MyAspectJ.zz()")
public void myBeforePointcut(JoinPoint joinPoint){
System.out.println("前置增强"+joinPoint);
}
@Pointcut(value = "execution(* com.baidu.XML.DeptServiceImp.*(..))")
private void zz(){
}
}
5测试类
package com.baidu.XML;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestMyXML {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext =
new ClassPathXmlApplicationContext("XMLapplicationContext.xml");
DeptServiceI deptServiceI = (DeptServiceI)applicationContext.getBean("deptService");//
deptServiceI.add();
deptServiceI.del();
deptServiceI.findName();
}
}
6运行结果
前置增强execution(void com.baidu.XML.DeptServiceI.add())
add---
最终通知
前置增强execution(void com.baidu.XML.DeptServiceI.del())
del---
最终通知
前置增强execution(String com.baidu.XML.DeptServiceI.findName())
findName
最终通知
Process finished with exit code 0