0
点赞
收藏
分享

微信扫一扫

Spring自定义注解



[url]http://chenjumin.iteye.com/blog/454459[/url]

[color=red]Pattern属性符: (.) 符合任何单一字符 (+)符合前一个字符一次或多次 (*)符合前一个字符零次或多次.[/color]

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

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将在运行期也保留注释,因此可以通过反射机制读取注解的信息。


[size=large][color=red]Spring自定义注解[/color][/size]
一、注解类源码

/** 
 * 方法访问角色注解 
 */  

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



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


@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;  
    }  
}




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


/** 
 * 环绕通知:在类方法调用前,先判断角色是否匹配。 
 */  
@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配置


<!-- 声明通过注解定义bean,同时也通过注解自动注入 -->  
<context:component-scan base-package="com.cjm" annotation-config="true"/>  

<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>    

<bean id="nameMatchMethodPointcutAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">    
    <property name="advice" ref="visitorRoleAdvice"/>    
    <property name="mappedNames">    
        <list>    
            <value>say</value>    
        </list>    
    </property>    
</bean>




[b][color=red]注意:[/color][/b]


1.监视annotationTest和save开头的方法。


<bean id="nameMatchMethodPointcutAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">      
	    <property name="advice" ref="myAnnotationAdvice"/>      
	    <property name="mappedNames">      
	        <list>      
	            <value>annotationTest*</value>
                    <value>save*</value>
	        </list>      
	    </property>
	</bean>


2.监视所有com开头的方法,包括com.XXX.XXX......()方法。


Pattern属性符:


[b](.) 符合任何单一字符 (+)符合前一个字符一次或多次 (*)符合前一个字符零次或多次[/b]


<bean id="regexpMethodPointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">      
	    <property name="advice" ref="myAnnotationAdvice"/>      
	    <property name="patterns" value="com.+">      
	    </property>
	</bean>




[size=large][color=red]spring 拦截器 spring自定义注解[/color][/size],访问HttpServletRequest等方式


最近项目里加上了用户权限,有些操作需要登录,有些操作不需要,之前做项目做权限,喜欢使用过滤器,但在此使用过滤器比较死板,如果用的话,就必须在配置文件里加上所有方法,而且 不好使用通配符。所以想了想,之前在人人用过的一种比较简单灵活的权限判断,是采用Spring 的 methhodInterceptor拦截器完成的,并且是基于注解的。


现在自己写了一套。大概是用法是这样的:


@LoginRequired  
 @RequestMapping(value = "/comment")  
 public void comment(HttpServletRequest req, HttpServletResponse res) {  
    // doSomething,,,,,,,,  
}


我是在Spring mvc 的controller层的方法上拦截的,注意上面的@LoginRequired 是我自定义的注解。这样的话,该方法被拦截后,如果有该 注解,则表明该 方法需要用户登录后才能执行某种操作,于是乎,我们可以判断request里的session或者Cookie是否包含用户已经登录的身份,然后判断是否执行该方法;如果没有,则执行另一种操作。


-------------------------------------------------------------------------


下面是自定义注解的代码:


package com.qunar.wireless.ugc.controllor.web;  

import java.lang.annotation.ElementType;  
import java.lang.annotation.Retention;  
import java.lang.annotation.RetentionPolicy;  
import java.lang.annotation.Target;  

@Target(ElementType.METHOD)  
@Retention(RetentionPolicy.RUNTIME)  
public @interface LoginRequired  {  

}


-----------------------------------------------------------------------------


下面是自定义的方法拦截器,继续自aop的MethodInterceptor


import javax.servlet.http.HttpServletRequest;  
import org.aopalliance.intercept.MethodInterceptor;  
import org.aopalliance.intercept.MethodInvocation;  
import com.qunar.wireless.ugc.controllor.web.LoginRequired;  


/** 
 * @author tao.zhang 
 * @create-time 2012-2-31 
 */  
public class LoginRequiredInterceptor1 implements MethodInterceptor {  


    @Override  
    public Object invoke(MethodInvocation mi) throws Throwable {  

        Object[] ars = mi.getArguments();  

        for(Object o :ars){  
            if(o instanceof HttpServletRequest){  
               System.out.println("------------this is a HttpServletRequest Parameter------------ ");  
            }  
        }  
        // 判断该方法是否加了@LoginRequired 注解  
        if(mi.getMethod().isAnnotationPresent(LoginRequired.class)){  
             System.out.println("----------this method is added @LoginRequired-------------------------");  
        }  
       //执行被拦截的方法,切记,如果此方法不调用,则被拦截的方法不会被执行。  
        return mi.proceed();  
    }  
}


------------------------------------------------------------------------


配置文件:


<bean id="springMethodInterceptor" class="com.qunar.wireless.ugc.interceptor.LoginRequiredInterceptor1" ></bean>  
    <aop:config>  
                 <!--切入点-->  
                 <aop:pointcut id="loginPoint"  
                 expression="execution(public * com.qunar.wireless.ugc.controllor.web.*.*(..)) "/>   
                 <!--在该切入点使用自定义拦截器-->  
                 <aop:advisor pointcut-ref="loginPoint" advice-ref="springMethodInterceptor"/>  
      </aop:config>

举报

相关推荐

0 条评论