aspectJ

jar包:
aspectjweaver.jar
aopalliance-1.0.jar
commons-logging-1.2.jar
spring-aop-4.1.7.RELEASE.jar
spring-aspects-4.1.7.RELEASE.jar
spring-beans-4.3.18.RELEASE.jar
spring-context-4.3.18.RELEASE.jar
spring-core-4.3.18.RELEASE.jar
spring-expression-4.3.18.RELEASE.jar

package com.aspectJ.user;
//接口
public interface Iuser {
void printone();
void printtwo();
void printthree();
}

package com.aspectJ.user;

public class IuserImpl implements Iuser {

	@Override
	public void printone() {
		System.out.println("hello world1");
		
	}

	@Override
	public void printtwo() {
		System.out.println("hello world2");
		
	}
	@Override
	public void printthree() {
	// TODO 自动生成的方法存根
		System.out.println(10/0);
		System.out.println("出现异常了");
}
}

package com.aspectJ.advice;
//通知类(切面类)
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

//getSignature():返回被代理的目标方法
//getName():被代理的目标方法的名字

public class Advice {
	public void before(JoinPoint joinPoint) {
	//获得目标类方法的名称
	System.out.println("前置通知:"+joinPoint.getSignature().getName());
	
}
//后置增强
public void after(JoinPoint joinPoint,Object obj) {
	System.out.println("后置通知:"+joinPoint.getSignature().getName()+obj);
}

//环绕增强
public Object around(ProceedingJoinPoint jp)throws Throwable {
	System.out.println("前环绕:");
	Object obj = jp.proceed();
	System.out.println("后环绕:");
	return jp;
}
	
//抛出异常
	public void afterThrowing(JoinPoint jp1,Throwable e){
		System.out.println("抛出异常通知:"+e.getMessage());
	}

//最终通知
	public void lastafter(JoinPoint jP2) {
		System.out.println("最终通知");
	}
}

//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"   
xmlns:context="http://www.springframework.org/schema/context"  
xmlns:jee="http://www.springframework.org/schema/jee"  
xmlns:lang="http://www.springframework.org/schema/lang"  
xmlns:util="http://www.springframework.org/schema/util"  
xmlns:tx="http://www.springframework.org/schema/tx"  
xmlns:mvc="http://www.springframework.org/schema/mvc"    
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   
 http://www.springframework.org/schema/jee    
 http://www.springframework.org/schema/jee/spring-jee.xsd   
 http://www.springframework.org/schema/lang    
 http://www.springframework.org/schema/lang/spring-lang.xsd   
 http://www.springframework.org/schema/context    
 http://www.springframework.org/schema/context/spring-context.xsd   
 http://www.springframework.org/schema/tx    
 http://www.springframework.org/schema/tx/spring-tx.xsd   
 http://www.springframework.org/schema/util    
 http://www.springframework.org/schema/util/spring-util.xsd   
 http://www.springframework.org/schema/mvc    
 http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--设置目标类对象  -->
<!-- <bean id="targetObject" class="com.aspectJ.user.IuserImpl"></bean> -->
<!--增强通知类的对象  -->
<!-- <bean id="myAdvice" class="com.aspectJ.advice.Advice"></bean> -->
<!-- <aop:config> -->
<!-- 引入通知 -->
<!-- <aop:aspect ref="myAdvice"> -->
<!-- 定义切入点表达式 -->
<!-- <aop:pointcut expression="execution(* 
 com.aspectJ.user.*.*(..))" id="myPointcut"/> -->
 <!--  前置增强-->
<!--  <aop:before method="before" pointcut-ref="myPointcut"/> -->

 <!-- 后置增强 -->
<!--  <aop:after-returning method="after" pointcut-ref="myPointcut" returning="obj"/> -->

<!-- 环绕通知 -->
<!--  <aop:around method="around" pointcut-ref="myPointcut"/>  -->

<!-- 异常通知 -->
<!-- <aop:after-throwing method="afterThrowing" pointcut-ref="myPointcut" throwing="e"/> -->

<!-- 最终通知 -->
<!-- <aop:after method="lastafter" pointcut-ref="myPointcut"/> -->
<!-- </aop:aspect> -->
<!-- </aop:config> -->

	</beans>

package com.aspectJ.user;
//测试
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AspectJTest {
	@Test
	public void test() {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		//用接口类型接收
		Iuser user = context.getBean("targetObject",Iuser.class);
		user.printone();
		user.printtwo();
		//异常方法
		user.printthree();
		
	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值