MethodInterceptor拦截器

1.自定义一个annotation

package com.websystem.util;

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

/**
 * des:自定义使方法跳过拦截的注解
 * author: zbl
 * date: 2014年9月3日
 **/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public abstract @interface RequiredInterceptor{
	boolean required() default true;
}

2.在Controller里面的方法使用自定义的annotation,下面是一个登进登出的例子。

package com.websystem.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.websystem.model.ManagerModel;
import com.websystem.service.ManagerService;
import com.websystem.util.AESPlusHelper;
import com.websystem.util.Constant;
import com.websystem.util.RequiredInterceptor;

/**
 * des:
 * author: zbl
 * date: 2014年8月26日
 **/
@Controller
@SessionAttributes(Constant.Manager)
public class ManagerController {

	@Resource
	private ManagerService managerServiceImpl;

	@RequiredInterceptor(required = false)
	@RequestMapping(value = "manager/login.do",method = RequestMethod.GET)  
	public ModelAndView login(ManagerModel managerModel,ModelMap model){
		ManagerModel manager = managerServiceImpl.getManager(managerModel);
		
		if(manager!=null){
			manager.setPassword("");
			model.addAttribute(Constant.Manager, manager);
			return new ModelAndView(new RedirectView(Constant.MainURL));
		}else{
			return new ModelAndView(new RedirectView(Constant.LoginURL));
		}
	}
	
	@RequiredInterceptor(required = false)
	@RequestMapping(value = "manager/logout.do",method = RequestMethod.GET)
	@ResponseBody
	public Object logout(SessionStatus status){
		status.setComplete();
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(Constant.Success, true);
		return map;
	}
}

3.定义MethodInterceptor,里面可以处理AOP逻辑代码。

package com.websystem.util;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

/**
 * des:
 * author: zbl
 * date: 2014年9月3日
 **/
@Component
public class SessionInterceptor implements MethodInterceptor {

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		// TODO Auto-generated method stub
		RequiredInterceptor requiredInterceptor = AnnotationUtils.findAnnotation(invocation.getMethod(), RequiredInterceptor.class);
		if(requiredInterceptor!=null){
			System.out.println(invocation.getMethod().getName());
			//你要做的逻辑代码
		}
		return invocation.proceed();
	}

}
4.添加配置

<context:component-scan base-package="com.websystem.controller,com.websystem.*.impl,com.websystem.util"/>
    
	<mvc:annotation-driven/>
	
	<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		<property name="beanNames">
			<list><value>*Controller</value></list>
		</property>
		<property name="interceptorNames">
			<list><value>sessionInterceptor</value></list>
		</property>
	</bean>

总结:不废话解析那么多,直接上成功的例子。



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值