Spring AOP 之@DeclareParents 引入

本文承接上文:@AspectJ注解开发Spring AOP

https://blog.csdn.net/single_wolf_wolf/article/details/81772837

在上文源码的基础上进行改进,可以完全复制源码demo进行测试(Spring所需jar需要自己引入)。

引言:

SpringAOP只是通过动态代理技术,把各类通知织入到它约定的流程中,如果我们希望引入其他类的方法来进行更好的实现,譬如上文中printRole函数,如果我们希望当角色为空时不打印,这就需要一个新的检测器对其进行检测。

开始

先定一个一个接口RoleVerifier

package com.cmb.test;

public interface RoleVerifier {

	public boolean verify(Role role);
	
}

然后实现它:RoleVerifierImpl

package com.cmb.test;
public class RoleVerifierImpl implements RoleVerifier {

	public boolean verify(Role role) {
		return role != null;
	}

}

然后修改RoleAspect类:

package com.cmb.test;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.DeclareParents;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class RoleAspect {
	
	@DeclareParents(value="com.cmb.test.RoleServiceImpl+", defaultImpl=RoleVerifierImpl.class)
	public RoleVerifier roleVerifier;

	
	@Pointcut("execution(* com.cmb.test.RoleServiceImpl.printRole(..))")
	public void print(){
		
	}
	
	@Before("print()")
	public void before(){
		System.out.println("before....");
	}
	
	@After("print()")
	public void after(){
		System.out.println("after....");
	}
	
	@AfterReturning("print()")
	public void afterReturning(){
		System.out.println("afterReturning....");
	}
	
	@AfterThrowing("print()")
	public void afterThrowing(){
		System.out.println("afterThrowing....");
	}
	
	@Around("print()")
	public void around(ProceedingJoinPoint jp){
		System.out.println("around before ...");
		try{
			jp.proceed();
		}catch(Throwable e){
			e.printStackTrace();
		}
		System.out.println("around after ...");
	}

	
}

请注意代码:

	@DeclareParents(value="com.cmb.test.RoleServiceImpl+", defaultImpl=RoleVerifierImpl.class)
	public RoleVerifier roleVerifier;

这是我们新加入的,解释一下:

value="com.cmb.test.RoleServiceImpl+" :表示RoleServiceImpl类进行增强,也就是RoleServiceImpl中引入一个新的接口;

defaultImpl : 代表其默认的实现类,这里指的是RoleVerifierImpl

最后修改主函数:

package com.cmb.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class GameMain2 {

	public static void main(String[] args) {

		ApplicationContext ctx = new AnnotationConfigApplicationContext(AopConfig.class);
		
		RoleService roleService = (RoleService) ctx.getBean(RoleService.class);
		
		RoleVerifier roleVerifier = (RoleVerifier) roleService;
		
		Role role = new Role(1L, "role_name_1", "note_1");
		if(roleVerifier.verify(role)){
			roleService.printRole(role);
		}
		System.out.println("#######################################");
		
		//测试异常通知
		//role = null;
		//roleService.printRole(role);
		
	}

}

请注意:

RoleVerifier roleVerifier = (RoleVerifier) roleService;

我们进行了强制转换。

我们知道SpringAOP依赖于动态代理,而生成动态代理对象是通过类似于下面这行代码实现的

return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), _this);

其中obj.getClass().getInterfaces()表示代理对象下面可以挂在多个接口下面,也就是说SpringAOP让代理对象挂到RoleServicehe RoleVerifier两个接口下,那么,我们就可以把堆型的Bean进行强制类型转换。

结果

如下:

around before ...
before....
{id=1,roleName = role_name_1, note = note_1
around after ...
after....
afterReturning....
#######################################

参考文献:《javaEE互联网轻量级框架整合开发》

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值