论坛首页 Java企业应用论坛

对Annotations忍耐的极限,谈谈常用框架Annotations使用感受

浏览 24822 次
精华帖 (0) :: 良好帖 (14) :: 新手帖 (5) :: 隐藏帖 (3)
作者 正文
   发表时间:2009-08-11   最后修改:2009-08-11
今天看到iBatis3已经支持Annotation了,不禁有点头晕目眩,看来又一次的抉择开始了。Annotions的确带来了便利,看看Spring的配置:
@Controller
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class DeptRuleAction extends BaseAction {
  @Autowired
  private DeptRuleManager manager;
  @Autowired
  private LoginUserService loginUserSerivce;
  @PostConstruct
  public void init() {
  }
  @Transactional
  //Action哪里有事务呀,呵呵
}

嗯,的确比XML简单,也容易维护。也许有其他的问题,比如自动装配带来的隐忧,比如静态数据的注入等等。但是瑕不掩瑜,Spring的Annotation是我喜欢的。
再看看Hibernate:
@Entity
@Table(name = "capital_sources")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class CapitalSource extends BaseModel {
  
  private Integer id;
  @Id
  @GeneratedValue(generator = "hibseq")
  @GenericGenerator(name = "hibseq", strategy = "hilo")
  @Column(name = "id", unique = true, nullable = false)
  public Integer getId() {
    return id;
  }
}

hibernate的配置也很简单(上面的代码已经算是复杂了),而且遵循COC风格,与Spring配合几乎做到了领配置。这个,我也喜欢。
Struts2的Annotaions就比较夸张了,下面的代码是Annotaion验证:
@Validations(requiredStrings = {
      @RequiredStringValidator(fieldName = "model.loginId", message = "登录名是必须的."),
      @RequiredStringValidator(fieldName = "model.password", message = "密码是必须的."),
      @RequiredStringValidator(fieldName = "model.email", message = "电子邮件是必须的."),
      @RequiredStringValidator(fieldName = "model.confirmPwd", message = "请两次输入密码.") }, stringLengthFields = { @StringLengthFieldValidator(fieldName = "password", minLength = "3", maxLength = "32", message = "密码应多于3字符", trim = true) }, emails = { @EmailValidator(fieldName = "model.email", message = "请输入正确的e-Mail.") }, expressions = { @ExpressionValidator(message = "两次输入的密码必须相同.", expression = "model.password==model.confirmPwd") })
public String save() {
}


晕了,这哪里是简化,分明是代码污染!再看看strut2的Action配置:
@Action(value="/different/url", 
    results={@Result(name="success", type="httpheader", params={"status", "500", "errorMessage", "Internal Error"})}
  )
  public String execute() {
    return SUCCESS;
  }

这是struts2文档中的例子,我无语了.....
其实struts2的XML配置Action已经很简单了:
<action name="*/*" class="{1}Action" method="{2}">
		     <result name="index" type="dispatcher">/pages/vehicles/basic/{1}/index.jsp</result>
		     <result name="input">/pages/vehicles/basic/{1}/edit.jsp</result>
		     <result name="success" type="redirect">{1}/index.do</result>
		 </action>		 

在实际的项目中,我用这段XML匹配了8个Action,每个Action都有完整的CRUD操作,这,要比Annotation简单多了吧。
BTW:要说MVC框架,我还是更喜欢SpringMVC,用它做过一个小项目,那才叫零配置,那才叫灵活,那才叫零侵入....可惜我们已经上了Struts2的船了。

所以,Annotations就像盐,放少了没有味道,放多了就候儿了。现在,iBatis又为我们带来了新的Annotations,不知道这道菜是咸还是淡。
   发表时间:2009-08-11  
struts2 的annotation 配法要是能够重用就简化多了!!
0 请登录后投票
   发表时间:2009-08-11  
不看好,annotation,也不想用.
0 请登录后投票
   发表时间:2009-08-11   最后修改:2009-08-11
kjj 写道
struts2 的annotation 配法要是能够重用就简化多了!!

应该也支持COC吧(要是不支持就太土了),这个没有用过,因为已经突破了我对Annoation承受的极限。
0 请登录后投票
   发表时间:2009-08-11  
Struts2 没必要用什么 Annotation
0 请登录后投票
   发表时间:2009-08-11  
yiding_he 写道
Struts2 没必要用什么 Annotation

完全同意,Struts2的XML配置已经足够简单了,Annotation有点画蛇添足的感觉。Struts2还有一些类似的蛇足,比如对dojo的封装,好在已经拿出来成为plugins了。
0 请登录后投票
   发表时间:2009-08-11  
个人还是很推崇spring和hibernate的annotation应用的
0 请登录后投票
   发表时间:2009-08-11  
hbcui1984 写道
个人还是很推崇spring和hibernate的annotation应用的

的确很方便。这两个类库很好的做到了“平衡”,但是struts2就比较乱了。
0 请登录后投票
   发表时间:2009-08-12  
Struts2的annoation还是别用了,简单的配置一下XML就行了。但是要使用通配符,否则也是异常灾难。

使用Struts2关键在于规划,把一些常用的Action的名称的规则定死,不要让开发人员任意去命名,用了通配符之后配置就相当简单了。
0 请登录后投票
   发表时间:2009-08-12  
其实感觉关键在于Annotation里面没有更加简介的对象语言,如果能够自建annotation对象并自动解析这个对象就OK了
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics