public interface Author{ public String getName(); public void setName(String name);
.... }
实体bean的本地接口扩展业务接口 public interface AuthorLocal extends Author,EJBLocalObject { }
实体bean本身也接口扩展业务接口 public abstract ArticleLocalBean implements Author,EntityBean {
... }
可户使用sessionbean 获得和更新值对象。 public Author getAuthor() { try { return new AuthorValues(AuthorLocal); } catch(Exception e) { throw new EJBException("Unable to create Value Object. Cause: " + e.getMessage()); } }
AuthorValues 实现Author接口,在构造器中实现数据赋值
public class AuthorValues implements Author{
....
public AuthorValues(Author author) throws Exception { Class c = this.getClass(); String methodName = null; Object[] parameter = new Object[1]; Class[] returnType = new Class[1];
Method[] methods = Author.class.getMethods(); for (int i = 0; i < methods.length; i++) { if (methods[i].getName().startsWith("get")) { methodName = "set" + methods[i].getName().substring(3); returnType[0] = methods[i].getReturnType(); Method localMethod = c.getMethod(methodName, returnType); parameter[0] = methods[i].invoke(artikel, new Object[] {}); localMethod.invoke(this, parameter); } }
}
public String getName() { return this.name; } public void setName(String name) { this.name = name; }
(entity bean)动态赋值值对象-- Dynamic Create Value Object 模式
from entity baen to value object dynamic create your valueobject :use value object摘要:在j2ee应用中,采用jsp+bean+servlet+ejb开发,如果你的业务 接口有大量的setter/getter方法,需要重复大量的赋值语句,本 文描述了如何动态赋值值对象。为什么要动态赋值弱类