6
How JPA 2.1 has become the new EJB 2.0 | Java, SQL and jOOQ. (blog.jooq.org)
IngramChen 積分 3 編輯於

kaif 專案的實例,下面是 Spring mvc 的 unit test super class:

@ActiveProfiles(SpringProfile.TEST)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringApplicationConfiguration(classes = MvcIntegrationTests.WebTestApplication.class)
public abstract class MvcIntegrationTests {

  @Profile(SpringProfile.TEST)
  @EnableWebMvc
  @ComponentScan(basePackages = "io.kaif.web")
  @Import(value = { UtilConfiguration.class, 
      MessageSourceAutoConfiguration.class,
      MockTestConfig.class, 
      WebConfiguration.class, 
      FreeMarkerAutoConfiguration.class,
      AppProperties.class, 
      SwaggerConfiguration.class })
  public static class WebTestApplication {
  }
}

內外 class 都夾了四個 annotation,而且你看上面 annotations,命名都是很難理解的: @WebApplicationConfigurate 是 spring mvc test,@SpringApplicationConfiguration@EnableWebMvc 是 spring boot。這些 annotation 寫多了你真的不知道哪個 annotation 是負責什麼,該放在哪 (無法從名字直接推敲..)

kaif 實例二,這是 Open API 的 restful Controller:

  @ApiOperation(
      value = "[vote] List votes of the user on multiple debates",
      notes = "List votes of the user on multiple "
          + "debates. When you paging debates, "
          + "you may want to know what user voted "
          + "for those debates in the page. "
          + "You can send multiple debateIds "
          + "to obtain votes in batch.")
  @RequiredScope(VOTE)
  @RequestMapping(value = "/debate", 
                  method = RequestMethod.GET)
  public List<V1VoteDto> votesOfDebates(
      ClientAppUserAccessToken token,
      @ApiParam(value = "comma separated debateIds", 
                allowMultiple = true)
      @RequestParam("debate-id") List<String> debateIds) {
  }

其實上面只是一個 /debate?debate-id=xxx 這樣的 restful query 而已,不過你看看這個 anontation 的 訊噪比 實在是低的可憐,一個簡單的 method call 被 annotation 淹沒了(上面混了 swagger/spring mvc/客製的 三種 annotation)

我已經避開 JPA/Hibernate 這種怪獸了,但其他地方還是跑不掉啊,我很懷念用 xml 設定 spring 的年代啊。

kaif 積分 3

標題在講JPA,內文都在表annotation,有點文不對題。

作者認為annotation只能用在一些非關邏輯/非runtime的地方,像是@Deprecated、@Override

但有時候結合AOP感覺很好用呀,像是每個method要不要認證、授權方法、要不要transaction之類的...沒有annotation,不就只能靠method命名去做regular expression,或另外維護一個mapping的規則表。