使用注释的Servlet初始化参数 [英] Servlet Initialization parameters using annotation

查看:947
本文介绍了使用注释的Servlet初始化参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我努力学习的Servlet注解并在这个片段

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")})

所以,问题是为什么我们需要把 @WebInitParam 注释时,我们已经宣布属性作为 initParams在 。这似乎是多余的我,还是我失去了一些东西?

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:


  1. 保持语言简单。结果
    它是一种冗余的,但是对于注释的语法可以重复使用,并且不需要新的语言结构。这使得更容易解析和阅读。当然,它的时间越长,但它也更加明确写注释的名称。

  1. 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.

在很多情况下,注解的数组似乎是一个解决办法呢。它可避免在Java中8,在这里你可以添加多个相同类型的注释:

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")

(我不知道,如果Servlet API的实际支持这一尚未虽然)

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

这篇关于使用注释的Servlet初始化参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆