带有EJB

时间:2016-10-28 03:40:06

标签: java java-ee design-patterns cdi ejb-3.1

我需要从我的系统获取pdf文档。我正在使用Apache Fop - 这个库使用2个文件来生成pdf - xsl文件,其中包含结构和样式以及带数据的xml。所以我从Web资源获取xsl文件,但现在我需要使用数据库中的数据生成xml。我试过这个解决方案: 我有这个界面:

public interface PrintableDocument {
    Object getJaxBOjbect(Long personId);
}

这是获取对象的无状态bean之一,我需要10个这样的bean来获取不同文档的不同数据。

@Stateless
@PrintableDocumentOneQualifier
public class PrintableDocumentOne implements PrintableDocument {

    @Inject
    private SomeRepository repository;

    public Object getJaxBOjbect(Long personId) {
    // Getting information from database
    // formulating Object with data and returning it
    }
}

所以现在我想创建像这样的工厂:

@Stateless
@LocalBean
public class PrintableDocumentsFactory {

    @Inject
    @PrintableDocumentOneQualifier
    private PrintableDocument printableDocumentOne;

    @Inject
    @PrintableDocumentTwoQualifier
    private PrintableDocument printableDocumentTwo;

    private Map<String, PrintableDocument> map = new HashMap<>();

    @PostConstruct
    public void init() {
        map.put("one", printableDocumentOne);
        map.put("two", printableDocumentTwo);
    }

    public PrintableDocument getPrintableDocument(String type) {
        return map.get(type);
    }

}

在服务bean上我想使用这个工厂:

@Stateless
@Local(DocumentService.class)
public class DocumentServiceBean {

    @Inject
    private PrintableDocumentsFactory factory;

    public byte[] getPdf(InputStream xsl, Long id, String type) {
        PrintableDocument printableDocument = 
             factory.getPrintableDocument(type);
        Object jaxBOject = printableDocument.getJaxBObject(id);
        //Use this object to get pdf and return it to web controller.
    }

}

但是现在我从工厂的getPrintableDocument得到了null。我认为问题是我需要无状态bean,当getPrintableDocument方法结束时,它们会被挑回EJB容器。所以我的问题是:我该如何处理这种情况?

编辑1:在工厂中的init上错过了PostConstruct注释。修正了,仍有问题。

编辑2:如果我的工厂上有@Singleton它将只保留一个无状态PrintableDocument bean的实例,或者它将返回池实例?因为现在我必须在工厂重新填写战略持有人地图,当系统需要另一个人来回答请求时。

2 个答案:

答案 0 :(得分:1)

您可以尝试使用@Inject代替PrintableDocumentsFactoryDocumentServiceBean注入<div class="challenge-accomplished-date-banner"> <% if @correct_user %> <%= challenge.notes.count.to_s.rjust(2, "0") %> <% else %> <%= challenge.notes.publish.count.to_s.rjust(2, "0") %> <% end %> </div> <% if challenge.categorization == "adventure" %> <%= link_to categorization_path(categorization: :adventure) do %> <span class="glyphicon glyphicon-picture", id="challenge-flag"></span> <% end %> <% elsif challenge.categorization == "health" %> <%= link_to categorization_path(categorization: :health) do %> <span class="glyphicon glyphicon-heart", id="challenge-flag"></span> <% end %> <% elsif challenge.categorization == "work" %> <%= link_to categorization_path(categorization: :work) do %> <span class="glyphicon glyphicon-briefcase", id="challenge-flag"></span> <% end %> <% elsif challenge.categorization == "gift" %> <%= link_to categorization_path(categorization: :gift) do %> <span class="glyphicon glyphicon-tree-deciduous", id="challenge-flag"></span> <% end %> <% else %> <%= link_to categorization_path(categorization: :wacky) do %> <span class="glyphicon glyphicon-glass", id="challenge-flag"></span> <% end %> <% end %> <% if challenge.duels.present? && challenge.duels.last.duelers.order(id: :asc).last.accept =! false %> <span class="glyphicon glyphicon-tower", id="challenge-flag"></span> <% elsif challenge.conceal == true %> <span class="glyphicon glyphicon-eye-close", id="challenge-flag"></span> <% else %> <span class="glyphicon glyphicon-eye-open", id="challenge-flag"></span> <% end %>

答案 1 :(得分:0)

尝试向@PostConstruct方法添加PrintableDocumentsFactory.init()注释。目前不会调用init方法,因此不会在地图中注册。