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

今天看到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,不知道这道菜是咸还是淡。

你可能感兴趣的:(spring,xml,Hibernate,框架,配置管理)