sevlet 注释initparams_使用注释的Servlet初始化参数

bd96500e110b49cbb3cd949968f18be7.png

I am trying to learn Servlet annotations and came across this snippet

@WebServlet(urlPatterns="/MyPattern", initParams={@WebInitParam(name="ccc", value="333")})

This makes sense to me. However, I don't understand why it is not like this

@WebServlet(urlPatterns="/MyPattern", initParams={(name="ccc", value="333"), (name="abc", value="1")})

So, the question is why we need to put @WebInitParam annotation when we already declared the attribute as initParams. It seems redundant to me, or am I missing something?

解决方案

As with most questions about language design choices we can only speculate here. I think some reasons for this are:

Keeping the language simple.

It is kind of redundant, but the syntax for annotations can be reused and does not require new language constructs. This makes it easier to parse and to read. Sure, It's longer, but it's also more explicit to write the annotation's name.

Don't restrict possible future language enhancements.

The proposed syntax would not work if annotations would support inheritance. I don't know if that's even a planned feature but it would not be possible to implement straightforward it if the type can be omitted.

In many cases an array of annotations seems like a workaround anyway. It can be avoided in Java 8, where you can add multiple annotations of the same type:

@WebServlet(urlPatterns="/MyPattern")

@WebInitParam(name="ccc", value="333")

@WebInitParam(name="abc", value="1")

(I don't know if the servlet api actually supports this yet though)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值