java - 将类引用传递给注入(inject)实例的更好方法

标签 java annotations mvp cdi

我正在尝试使用 CDI 和注释在 JSF2 应用程序中获取 MVP(被动 View )模式的运行示例。

我的目标是解耦所有相关项目,这意味着 View 位于 jsf-war-project 中,演示者位于单独的 jar 中。我稍微关注Adam Biens example在 Oracle 上,但我不希望演示者了解有关 View 框架(在本例中为 JSF)的任何信息。

我的问题是演示者应该被注入(inject)到 View 中,但是演示者应该有一个在当前 View 实例中传递的参数构造函数。所以我希望 @Inject-Annotation 能够允许类似 @Inject(this) 的东西,但它不会:-(

我通过使用 @PostConstruct 的 init-method 解决了这个问题,该方法调用演示者上的 setter 并将其传递给当前 View 。我想这是根据 CDI 规范我能得到的最接近的结果...如果我错了,请随时纠正我。

查看注释:

@Stereotype
@Named
@RequestScoped
@Target(TYPE)
@Retention(RUNTIME)
@Documented
public @interface View {}

演示者注释:

@Stereotype
@Named
@Target(TYPE)
@Retention(RUNTIME)
@Documented
public @interface Presenter {}

演示者实例:

@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

编辑:更准确地说:我可以使用注释作为装饰器吗?

最佳答案

实现您想要的效果的最简单方法是在演示器中延迟注入(inject)(以避免循环注入(inject)) View 。它看起来像这样:

演示者实例:

@Presenter
public class BikeGaragePresenter {

    BikeGarageView view;

    @Inject
    @Any
    Instance<BikeGarageView> viewInstances;

    public BikeGarageView getView()
    {
        return viewInstances.get();
    }       

    public void save(){
        System.out.println(view.getOwner());
    }


}

查看实例:

@View
public class BikeGarageBB implements BikeGarageView {

    @Inject
    private BikeGaragePresenter presenter;

    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();
    }
} 

关于java - 将类引用传递给注入(inject)实例的更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8749073/

相关文章:

java - GWT - MVP 事件总线。创建多个处理程序

Java 2d drawLine 对于长线来说很慢

java - 用一个自定义注释替换复杂的注释组合

java - 类型注释属于有界通配符的什么位置?

java - 通过注解获取java bean属性值

android - 存储库和用例之间有什么区别?

c# - 使用 EntityFramework : Context per Presenter or long conversation pattern for Windows Forms Applications?

java - 如何在 JTextArea 中将文本右对齐?

java - 为什么 1942/4/3 00 :00:00 an illegal date in java. util.Calendar?

java - JTable 排序在单元格中没有好的值(value)