java bean 监听_添加bean的监听

packagecom.qysxy.erp.common.listener;importcom.qysxy.erp.constants.ShiroConstants;importcom.qysxy.erp.framework.shiro.annotation.PermissionDesc;importcom.qysxy.erp.util.reflect.AopTargetUtils;importcom.yizhilu.os.core.service.cache.MemCache;importorg.apache.shiro.authz.annotation.RequiresPermissions;importorg.springframework.context.ApplicationListener;importorg.springframework.context.annotation.Lazy;importorg.springframework.context.event.ContextRefreshedEvent;importorg.springframework.stereotype.Service;importjava.lang.annotation.Annotation;importjava.lang.reflect.Method;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importjava.util.Set;/*** @Description 利用spring的监听获取bean,

* @Author fuguangli businessfgl@163.com

* @Create date: 2017/8/30*/@Service

@Lazy(true)public class ShiroApplicationListener implements ApplicationListener{private MemCache memCache =MemCache.getInstance();

@Overridepublic voidonApplicationEvent(ContextRefreshedEvent event) {//ReflectionUtils.getAllDeclaredMethods()

Map savePerms = new HashMap<>();

Map beans = event.getApplicationContext().getBeansWithAnnotation(PermissionDesc.class);if (beans != null) {

Set keys =beans.keySet();for(String key : keys) {

Object o= null;try{

o=AopTargetUtils.getTarget(beans.get(key));

}catch(Exception e) {

e.printStackTrace();

}

Class clazz=o.getClass();

Method[] methods=clazz.getMethods();//Method[] methods = ReflectionUtils.getAllDeclaredMethods(o.getClass());

if (methods != null) {for(Method method : methods) {//method.setAccessible(true);

Annotation[] annos =method.getAnnotations();

PermissionDesc permissionDesc= null;

RequiresPermissions requiresPermissions= null;for(Annotation a : annos) {if (a.annotationType().equals(PermissionDesc.class)) {

permissionDesc=(PermissionDesc) a;

}if (a.annotationType().equals(RequiresPermissions.class)) {

requiresPermissions=(RequiresPermissions) a;

}

}if (permissionDesc != null && requiresPermissions != null) {

String desc=permissionDesc.value();

String[] perms=requiresPermissions.value();for(String perm : perms) {/*TODO:此处应存入缓存 perm:desc*/System.out.println(perm+ ":" +desc);

savePerms.put(perm, desc);

}

}

}

}

}if (savePerms.size() != 0) {

memCache.set(ShiroConstants.PERMS_KEY, savePerms);//默认存储24小时,用于新建权限的时候的权限钥匙

}

}

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值