Spring学习总结—— AOP的实现

#一、SpringAop配置方式
两种配置方式:
1、基于Spring配置文件:
1、定义一个包含切面方法的bean

   /**
 *  Aop bean 
 *  包含切面方法loggin
 */
public class Logger {
    /**
     * before 通知
     */
    public void before(){
        System.out.println("do before point");
    }
     /**
     * after 通知
     */
    public void after(){
        System.out.println("do after point");
    }
     /**
     * around 通知
     * 一定要 return 
     */
    public Object around(ProceedingJoinPoint joinPoint){
        System.out.println("do before point");
        Object object = joinPoint.proceed();
        System.out.println("do after point");
        return object;
    }
    /**
    * 核心业务逻辑调用正常退出后,不管是否有返回值,正常退出后,均执行此Advice 
    * @param joinPoint 
    */
     private void doReturn(JoinPoint joinPoint) {  
        System.out.println("-----doReturn().invoke-----");  
        System.out.println(" 此处可以对返回值做进一步处理");  
        System.out.println(" 可通过joinPoint来获取所需要的内容");  
        System.out.println("-----End of doReturn()------");  
    }
    /** 
     * 核心业务逻辑调用异常退出后,执行此Advice,处理错误信息 
     * @param joinPoint 
     * @param ex 
     */
    private void doThrowing(JoinPoint joinPoint,Throwable ex) {  
        System.out.println("-----doThrowing().invoke-----");  
        System.out.println(" 错误信息:"+ex.getMessage());  
        System.out.println(" 此处意在执行核心业务逻辑出错时,捕获异常,并可做一些日志记录操作等等");  
        System.out.println(" 可通过joinPoint来获取所需要的内容");  
        System.out.println("-----End of doThrowing()------");  
    }  
}

2、配置文件配置AOP 切点-切入方式-切面方法

<!-- 切面方法bean -->
<bean id="logger" class="cn.seisys.iti.pdd.demo.aop.Logger"/>
<!-- 定义切点 -->
<aop:pointcut id="perform" expression="execution(* cn.seisys.iti.pdd.demo.service.impl.*.*(..))"/>
<!-- 制定切入方式 before、after、around、return、throw -->
<aop:aspect ref="logger">
    <aop:before method="before" pointcut-ref="perform"/>
    <aop:after method="after" pointcut-ref="perform"/>
    <aop:around method="around" pointcut-ref="perform"/>
    <aop:after-returning method="doReturn"  pointcut-ref="perform"/>  
    <aop:after-throwing method="doThrowing" throwing="ex" pointcut-ref="perform"/> 
</aop:aspect>    

2、基于注解
开启Aop注解

 <!-- 开启Aop注解 -->
 <aop:aspectj-autoproxy/>

定义Aspect切面bean

/**
 *
 * 定义切面切点
 */
 
//标记bean
@Component
//切面注解
@Aspect
public class LogAspect {
	Logger logger = Logger.getLogger(LogAspect.class);
	String logInfo;

	@Pointcut("execution(* cn.seisys.iti.pdd.demo.service.impl.AopTestServiceImpl.*(..))")
	public void pointCut(){
	}

	@Before("pointCut()")
	public void before(JoinPoint jp){
		logInfo = jp.getTarget().getClass().getName()+"类的"
		+jp.getSignature().getName()+"方法开始执行******Start******";
		logger.info(logInfo);
	}

	@After("pointCut()")
	public void after(JoinPoint jp){
		logInfo = jp.getTarget().getClass().getName()+"类的"
		+jp.getSignature().getName()+"方法结束执行******End******";
		logger.info(logInfo);
	}

	@AfterThrowing(value = "pointCut()",throwing = "ex")
		public void afterThrowingMethod(JoinPoint jp, Exception ex){
		logInfo = jp.getTarget().getClass().getName()+"类的"
		+jp.getSignature().getName()+"方法结束执行出现异常:"+ex.toString();
		logger.error(logInfo);
	}
	
	@Around("pointCut()")
	public Object Around(ProceedingJoinPoint pjp){
	try {
		logInfo = pjp.getTarget().getClass().getName()+"类的"
		+pjp.getSignature().getName()+"around aop ******begin******";
		logger.info(logInfo);
		Object object =  pjp.proceed();
		logInfo = pjp.getTarget().getClass().getName()+"类的"
		+pjp.getSignature().getName()+"around aop ******end******";
		logger.info(logInfo);
		return object;
	} catch (Throwable throwable) {
		throwable.printStackTrace();
		logInfo = pjp.getTarget().getClass().getName()+"类的"
		+pjp.getSignature().getName()+"方法结束执行出现AOP异常:"+throwable.toString();
		logger.error(logInfo);
		return null;
	}
}

测试方法

/**
 * 测试
 */
public class AopAnnotationTest {

	@Test
	public void test(){
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"spring-context.xml",
				"springMVC-servlet.xml","spring-mybatis.xml","mybatis-config.xml");
		AopTestService service = (AopTestService) context.getBean("aopService");
		AopEntity aop = service.findAopByName("日志");
		System.out.println(aop.toString());

	}
}

步骤总结: 定义切点、定义切点入方式、定义通知内容

#二、AOP原理

动态代理

参考资料:https://blog.csdn.net/yuexianchang/article/details/77018603

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值