java bean 监听_添加bean的监听

  • Post author:
  • Post category:java


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小时,用于新建权限的时候的权限钥匙

}

}

}

}



版权声明:本文为weixin_39945679原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。