如何在Spring MVC中自定义注解
2个回答
2014-12-13 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517193
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
在一次调试中我发现了这个问题,很明显,客户端的token已经过期,可是controller依然还会有值,说明这个值一直都在存在,因为客户端的token已经过期而没有被重写。
于是乎就要找办法解决,把原先写到类实例的值改成放到方法执行时传入,这样的话,只要在interceptor中把相关的值写以request attributes中就可以了,然后在方法执行时注入到相应的参数中。而这样就要用到注解了,可是Spring里的注解没有关于Request Attribute的,只有@RequestParameter和@ModeAttribute这类的,这些并不合用。看来只能另想方法。
自定义一个自己的Spring Annotation
尽管没有@RequestAttribute这样的注解,那有没有办法自己弄一个出来呢?
有的,强大的SpringFramework留给我们一个入口,我们只要实现一个HandlerMethodArgumentResolver接口,这样的话就可处理我们感兴趣的Annotation,把相关的值返回:
转载仅供参考,版权属于原作者。祝你愉快,满意请采纳哦
2014-12-13
展开全部
For example:
@Controller public class MyController {
@RequestMapping("/abc")
@RequiresSomeSpecialHandling
public void handleSecureRequest() {
} }
Here @RequiresSomeSpecialHandling would be my own annotation that causes some special work to be done before or after the given web request /abc.
I know that on a very high level I would need to write a bean post processor, scan classes for my annotations, and inject custom mvc interceptors when needed. But are there any shortcuts to simplify this task? Especially for the two examples above.
Thanks in advance,
Spring框架介绍
Can anyone explain what I need to do to implement my own annotation that would add functionality to my web requests?
For example:
@Controller public class MyController {
@RequestMapping("/abc")
@RequiresSomeSpecialHandling
public void handleSecureRequest() {
} }
Here @RequiresSomeSpecialHandling would be my own annotation that causes some special work to be done before or after the given web request /abc.
I know that on a very high level I would need to write a bean post processor, scan classes for my annotations, and inject custom mvc interceptors when needed. But are there any shortcuts to simplify this task? Especially for the two examples above.
Thanks in advance,
@Controller public class MyController {
@RequestMapping("/abc")
@RequiresSomeSpecialHandling
public void handleSecureRequest() {
} }
Here @RequiresSomeSpecialHandling would be my own annotation that causes some special work to be done before or after the given web request /abc.
I know that on a very high level I would need to write a bean post processor, scan classes for my annotations, and inject custom mvc interceptors when needed. But are there any shortcuts to simplify this task? Especially for the two examples above.
Thanks in advance,
Spring框架介绍
Can anyone explain what I need to do to implement my own annotation that would add functionality to my web requests?
For example:
@Controller public class MyController {
@RequestMapping("/abc")
@RequiresSomeSpecialHandling
public void handleSecureRequest() {
} }
Here @RequiresSomeSpecialHandling would be my own annotation that causes some special work to be done before or after the given web request /abc.
I know that on a very high level I would need to write a bean post processor, scan classes for my annotations, and inject custom mvc interceptors when needed. But are there any shortcuts to simplify this task? Especially for the two examples above.
Thanks in advance,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询