反对注解的论点

2022-01-13 00:00:00 annotations coding-style java

我的团队正在迁移到 Spring 3.0,有些人希望开始将所有内容都迁移到 Annotations 中.当我看到一个具有这样的方法的类时,我的直觉(代码味道?)真的很糟糕:(只是一个例子 - 不是所有真正的注释)

My team is moving to Spring 3.0 and there are some people who want to start moving everything into Annotations. I just get a really bad feeling in my gut (code smell?) when I see a class that has methods like this: (just an example - not all real annotations)

@Transaction
@Method("GET")
@PathElement("time")
@PathElement("date")
@Autowired
@Secure("ROLE_ADMIN")
public void manage(@Qualifier('time')int time) {
...
}

我只是落后于时代,还是这一切在其他人看来都是一个可怕的想法?而不是使用像继承和多态这样的OO概念,现在一切都按照约定或通过注释.我只是不喜欢它.必须重新编译所有代码来更改 IMO 的配置似乎是错误的.但这似乎是一切(尤其是春天)的方式.我应该只是克服它"还是应该回击并尝试让我们的代码尽可能地免费?

Am I just behind the times, or does this all seem like a horrible idea to anyone else? Rather then using OO concepts like inheritance and polymorphism everything is now by convention or through annotations. I just don't like it. Having to recompile all the code to change things that IMO are configuration seems wrong. But it seems to be the way everything (especially Spring) is going. Should I just "get over it" or should I push back and try to keep our code as annotation free as possible?

推荐答案

其实我觉得你内心的不好感觉更多是因为像这种混合配置和代码的注解.

Actually I think that the bad feeling in your gut against has more to do with Annotations like this mixing configuration with code.

我个人感觉和你一样,我更愿意将配置(例如事务定义、路径元素、控制器应该映射到的 URL 等)留在代码库本身和外部 Spring 之外XML 上下文文件.

Personally I feel the same way as you do, I would prefer to leave configuration (such as transaction definitions, path elements, URLs that a controller should be mapped to, etc.) outside of the code base itself and in external Spring XML context files.

我认为,虽然这里的正确方法归结为意见和您更喜欢哪种方法 - 我预测一半的社区会同意注释方法,另一半会同意外部配置方法.

I think though that the correct approach here comes down to opinion and which method you prefer - I would predict that half the community would agree with the annotations approach and the other half would agree with the external configuration approach.

相关文章