[Spring開発]MethodInterceptorの使い方
1.ログインクラスの定義
public class LoginAction{
@RequestMapping(value = “/login")
public void login(HttpServletRequest req, HttpServletResponse res) {
/some code
}
//ログイン後
@LoginMethod
@RequestMapping(value = “/userList")
public void userList(HttpServletRequest req, HttpServletResponse res) {
//ユーザリスト
}
}
2.LoginMethodアノテーションの定義
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LoginMethod {
}
3.MethodInterceptorの定義
public class SystemMethodInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
Method method = methodInvocation.getMethod();
if(method.isAnnotationPresent(LoginMethod.class)){
User user = sessionUtil.getCurrUser();
if(user == null){//未ログイン
return null;
}else{
return methodInvocation.proceed();
}
}else{
return methodInvocation.proceed();
}
}
}
4.xmlの設定
<bean id="systemMethodInterceptor" class="com.startnews24.interceptor.SystemMethodInterceptor" >
</bean>
<aop:config>
<aop:pointcut id="methodPoint" expression="execution(* com.startnews24.controllor.web.*.*(..)) “/>
<aop:advisor pointcut-ref="methodPoint" advice-ref="systemMethodInterceptor"/>
</aop:config>