Spring Roo DataOnDemand和getNewTransientObject方法的java定制?
在Spring Roo托管实体的集成测试中,我们在使用AspectJ mix-In风格接口时遇到了一个问题,该接口不仅引入了带有默认实现的方法,而且还添加了一个用JSR 303验证规则注释的属性。问题是Spring Roo DataOnDemand逻辑没有看到这些字段,因此在getNewTransientObject()方法中没有考虑这些字段
例如,以下(过于简单的)接口为实现“键控”接口的每个实体添加了一个“键”字段:
import javax.persistence.Column;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public interface Keyed {
public String getKey();
public void setKey(String key);
/**
* AspectJ MixIn for any class which implements the interface. Provides a
* default implementation using AspectJ. This is the style shown in
* Manning's AspectJ in Action (2nd edition) for providing a default
* implementation interface.
*
* @author tgh
*/
static aspect Impl {
@NotNull
@Column(name = "key", unique = true)
@Size(min = 8, max = 12)
private String Keyed.key;
public void Keyed.setKey(String key) {
this.key = key;
}
public String Keyed.getKey() {
return this.key;
}
}
}
对我们来说,这实际上是一个解决了的问题。我们所做的是从Spring Roo创建的AspectJ ITD(.aj)文件中推入getNewTransientObject()方法,然后确保属性设置为可接受的值。在我们的数据命令中。对于测试类的java文件,我们有以下片段
public KeyedExampleA getNewTransientKeyedExampleA(int index) {
KeyedExampleA obj = new KeyedExampleA();
setDescription(obj, index);
setListPrice(obj, index);
setName(obj, index);
// Deal with mix-in variables that Roo doesn't know about
obj.setKey("KEY_" + index);
return obj;
}
让我感到不安的是,我们没有在DataOnDemand中创建setKey(obj,index)方法。java文件,并使用与Spring Roo为getNewTransientKeyedObject(int)方法生成的样式相同的样式
是否值得为这样一个琐碎的情况创建setKey(obj,index)方法,还是我们应该继续使用我们所拥有的obj方法。getNewTransientObject()方法中的设置键(值)
看看Roo创造的春天。aj文件,我看不出它有多大区别,因为除了getNewTransientObject()方法之外,没有调用setField(obj,index)的方法。既然Spring Roo没有发现我们已经混合了引入的属性,它可能也不会在意未来
# 1 楼答案
为了保持一致性,我会编写setKey()方法。 但不这样做并不会造成任何问题