【发布时间】:2012-01-05 20:10:52
【问题描述】:
我正在尝试使用 CDI 和注释在 JSF2 应用程序中获取 MVP(被动视图)模式的运行示例。
我的目标是解耦所有相关项目,这意味着视图位于 jsf-war-project 中,而演示者位于单独的 jar 中。我在 Oracle 上略微关注 Adam Biens example,但我不希望演示者了解有关视图框架(在本例中为 JSF)的任何信息。
我的问题是演示者应该被注入到视图中,但是演示者应该有一个在当前视图实例中传递的参数构造函数。所以我希望@Inject-Annotation 允许@Inject(this) 之类的东西,但它不允许:-(
我通过带有 @PostConstruct 的 init 方法解决了这个问题,该方法在演示者上调用 setter 并将其传递给当前视图。我想这是根据 CDI 规范我能得到的最接近的值...如果我错了,请随时纠正我。
视图-注释:
@Stereotype
@Named
@RequestScoped
@Target(TYPE)
@Retention(RUNTIME)
@Documented
public @interface View {}
Presenter-Annotation:
@Stereotype
@Named
@Target(TYPE)
@Retention(RUNTIME)
@Documented
public @interface Presenter {}
Presenter-Instance:
@Presenter
public class BikeGaragePresenter {
BikeGarageView view;
protected BikeGaragePresenter(){}
public BikeGaragePresenter(BikeGarageView view){
assert view != null;
this.view = view;
}
public void save(){
System.out.println(view.getOwner());
}
public void setView(BikeGarageView view) {
this.view = view;
}
}
视图实例:
@View
public class BikeGarageBB implements BikeGarageView {
@Inject
private BikeGaragePresenter presenter;
@PostConstruct
public void init(){
this.presenter.setView(this);
}
private String owner;
public void setOwner(String owner) {
this.owner = owner;
}
@Override
public String getOwner() {
return this.owner;
}
@Override
public void displayMessage(String message) {
}
public void save(){
presenter.save();
}
}
现在我的问题是:我可以以某种方式将这个样板代码(init-method)提取到注释中(有点类似于 Aspects)吗?我不太了解如何编写注释...我通常只使用它们:-D
编辑:更准确地说:我可以将注释用作装饰器吗?
【问题讨论】:
-
Presenter 没有使用 RequestScoped 注释 - 对吗?在这种情况下,BikeGaragePresenter 将是单例的。
-
我不想对实际的前端技术有任何依赖(例如,对 rcp-app 也使用相同的演示者)。我认为 RequestScope 来自 Servlet-API。但是是的,presenter 不能是单例(每个视图都需要一个新的 Presenter)。
-
是的,它不会是单例,因为默认范围是 @Dependendent,所以每次注入 BikeGaragePresenterit 时都会是一个新 bean。
-
您是否考虑过使用 InjectionPoint CDI bean 来让您的 bean 了解其注入上下文?
-
不...我刚开始使用 CDI。你能举个例子吗?
标签: java annotations mvp cdi