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>
总结:不废话解析那么多,直接上成功的例子。