applicationContext.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: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">
<!-- 目标类 -->
<bean id="userServiceId" class="com.study.spring_aspectj.a_xml.UserServiceImpl"></bean>
<!-- 切面类 -->
<bean id="myAspectId" class="com.study.spring_aspectj.a_xml.MyAspect"></bean>
<!--
<aop:aspect>将切面类声明“切面” 从而获得通知(方法)
ref 切面类引用
<aop:pointcut> 声明一个切入点表达式
expression 切入点表达式
id 名称,用于其他通知引用
-->
<aop:config>
<aop:aspect ref="myAspectId">
<aop:pointcut expression="execution(* com.study.spring_aspectj.a_xml.UserServiceImpl.*(..))" id="myPointcut"/>
<!-- 前置通知
<aop:before method="myBefore" pointcut="" pointcut-ref=""/>
method : 通知,及方法名
pointcut:切入点表达式,此表达式只能当前通知
pointcut-ref:切入点引用,可以与其他通知共享切入点
通知方法可以有参数
-->
<!-- 前置通知 -->
<!-- <aop:before method="myBefore" pointcut-ref="myPointcut"/> -->
<!-- 后置通知 -->
<!-- <aop:after-returning method="myAfterReturning" returning="ret" pointcut-ref="myPointcut"/> -->
<!-- 环绕通知 -->
<!-- <aop:around method="myArount" pointcut-ref="myPointcut"/> -->
<!-- 抛出异常通知 -->
<!-- <aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointcut" throwing="e"/> -->
<!-- 最终通知 -->
<aop:after method="myAfter" pointcut-ref="myPointcut"/>
</aop:aspect>
</aop:config>
</beans>
Aspect 代码:
package com.study.spring_aspectj.a_xml;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyAspect {
public void myBefore(JoinPoint joinPoint){
System.out.println("前置通知"+joinPoint.getSignature().getName());
}
public void myAfterReturning(JoinPoint joinPoint,Object ret){
System.out.println("后置通知"+joinPoint.getSignature().getName()+",-->"+ret);
}
public Object myArount(ProceedingJoinPoint joinPoint) throws Throwable {
//手动执行目标方法
System.out.println("前");
Object object = joinPoint.proceed();
System.out.println("后");
return object;
}
public void myAfterThrowing(JoinPoint joinPoint,Throwable e){
System.out.println("抛出异常通知:"+e.getMessage());
}
public void myAfter(){
System.out.println("after");
}
}
service实现类:
package com.study.spring_aspectj.a_xml;
public class UserServiceImpl implements UserService {
@Override
public void addUser() {
System.out.println("addUser...");
}
@Override
public void updateUser() {
//int i = 10/0;
System.out.println("updateUser...");
}
@Override
public void deleteUser() {
System.out.println("deleteUser...");
}
}
测试类:
package com.study.spring_aspectj.a_xml;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpringxml {
@Test
public void demo1(){
String xmlPath = "com/study/spring_aspectj/a_xml/applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
//获得的是目标类
UserService userService = (UserService) applicationContext.getBean("userServiceId");
userService.addUser();
userService.deleteUser();
userService.updateUser();
}
}