自定义注解【转】

本自定义注解的作用:用于控制类方法的调用,只有拥有某个角色时才能调用。

 

java内置注解
     1、@Target 表示该注解用于什么地方,可能的 ElemenetType 参数包括: 
            ElemenetType.CONSTRUCTOR   构造器声明 
            ElemenetType.FIELD   域声明(包括 enum 实例) 
            ElemenetType.LOCAL_VARIABLE   局部变量声明 
            ElemenetType.METHOD   方法声明 
            ElemenetType.PACKAGE   包声明 
            ElemenetType.PARAMETER   参数声明 
            ElemenetType.TYPE   类,接口(包括注解类型)或enum声明
   
     2、@Retention 表示在什么级别保存该注解信息。可选的 RetentionPolicy 参数包括: 
           RetentionPolicy.SOURCE   注解将被编译器丢弃 
           RetentionPolicy.CLASS   注解在class文件中可用,但会被VM丢弃 
           RetentionPolicy.RUNTIME   VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息。

 

一、注解类源码

Java代码
  1. /**  
  2.  * 方 法访问角色注解  
  3.  */   
  4.   
  5. @Target (ElementType.METHOD)  
  6. @Retention (RetentionPolicy.RUNTIME)  
  7. @Documented   
  8. @Inherited   
  9. public   @interface  VisitorRole {  
  10.     String value();  
  11. }  
/**
 * 方法访问角色注解
 */

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface VisitorRole {
	String value();
}

 

二、Person类的源码。该类使用了自定义注解。

Java代码
  1. @Component ( "person" )    //此处通过注解的方式定义受管Bean   
  2. public   class  Person {  
  3.     private  String userId =  "cjm" ;  
  4.     private  String userName =  "jumin.chen" ;  
  5.       
  6.     @VisitorRole ( "ADMIN" )    //自定 义注解的使用。只有具有ADMIN角色才能调用本方法。   
  7.     public  String say(){  
  8.         return   "I'm "  + userName;  
  9.     }  
  10. }  
@Component("person")   //此处通过注解的方式定义受管Bean
public class Person {
	private String userId = "cjm";
	private String userName = "jumin.chen";
	
	@VisitorRole("ADMIN")   //自定义注解的使用。只有具有ADMIN角色才能调用本方法。
	public String say(){
		return "I'm " + userName;
	}
}

 

三、通过环绕通知对方法进行拦截,只有当角色匹配时,才能执行方法。

Java代码
  1. /**  
  2.  * 环 绕通知:在类方法调用前,先判断角色是否匹配。  
  3.  */   
  4. @Component ( "visitorRoleAdvice" )  
  5. public   class  VisitorRoleAdvice  implements  MethodInterceptor {  
  6.     public  Object invoke(MethodInvocation invocation)  throws  Throwable {  
  7.         if (invocation.getMethod().isAnnotationPresent(VisitorRole. class )){  //有指 定注解   
  8.             String role = null ;  
  9.             Annotation annotation = invocation.getMethod().getAnnotation(VisitorRole.class );  //获取指 定注解   
  10.             if (annotation!= null ){  
  11.                 role = ((VisitorRole)annotation).value(); //从注解中获取角色   
  12.             }  
  13.               
  14.             if ( "ADMIN" .equals(role)){  
  15.                 return  invocation.proceed();   //角色匹配,继续执行方法   
  16.             }else {  
  17.                 System.out.println("没有角色权限!" );  
  18.                 return   null ;  
  19.             }  
  20.               
  21.         }else//类方法没有 自定义注解,直接执行该方法   
  22.             return  invocation.proceed();  
  23.         }  
  24.     }  
  25. }  
/**
 * 环绕通知:在类方法调用前,先判断角色是否匹配。
 */
@Component("visitorRoleAdvice")
public class VisitorRoleAdvice implements MethodInterceptor {
	public Object invoke(MethodInvocation invocation) throws Throwable {
		if(invocation.getMethod().isAnnotationPresent(VisitorRole.class)){ //有指定注解
			String role = null;
			Annotation annotation = invocation.getMethod().getAnnotation(VisitorRole.class); //获取指定注解
			if(annotation!=null){
				role = ((VisitorRole)annotation).value(); //从注解中获取角色
			}
			
			if("ADMIN".equals(role)){
				return invocation.proceed();  //角色匹配,继续执行方法
			}else{
				System.out.println("没有角色权限!");
				return null;
			}
			
		}else{ //类方法没有自定义注解,直接执行该方法
			return invocation.proceed();
		}
	}
}

 

四、Spring配置

Xml代码
  1. <!-- 声明通过注解定义bean,同时也通过注解自动注入 -->   
  2. < context:component-scan   base-package = "com.cjm"   annotation-config = "true" />   
  3.   
  4. < bean   class = "org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />     
  5.   
  6. < bean   id = "nameMatchMethodPointcutAdvisor"   class = "org.springframework.aop.support.NameMatchMethodPointcutAdvisor" >     
  7.     < property   name = "advice"   ref = "visitorRoleAdvice" />     
  8.     < property   name = "mappedNames" >     
  9.         < list >     
  10.             < value > say </ value >     
  11.         </ list >     
  12.     </ property >     
  13. </ bean >   

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值