通过@Inject将业务逻辑暴露给宁静服务的最佳方式 [英] Best way to expose business logic to restful services via @Inject

查看:168
本文介绍了通过@Inject将业务逻辑暴露给宁静服务的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SO和网络上阅读了很多关于这个主题但在处理旧帖子时似乎有问题...

I read a lot on this topic on SO and the web but there seem to be problems when dealing with older posts...

我想公开我的EJB业务逻辑到休息api /注入一个ejb到一个球衣资源。

I want to expose my EJB business logic to a rest api / inject an ejb into a jersey resource.

使用@EJB工作正常,但有人建议不要将@EJB用于本地bean 。

Using @EJB works fine but there are people out there suggesting not to use @EJB for local beans.

使用@Inject在服务中注入bean有不同的方法。最简单的(对我而言)似乎如下:

There are different methods to inject beans in services with @Inject. The easiest (to me) seems to be the following:

@RequestScoped // This line is important!
@Path("service")
    public class Rest {

    @Inject Bean beany;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String get () {
        return beany.saySomething();
    }

}

将资源注释为cdi工作。

Annotating the resource as cdi does the job.

这个讨论让我了解了解决方案,但也指出了问题(未指明行为)。我想知道现在的情况是否更清楚。

This discussion brought me to the solution but also states problems (behaviour not specified). I would like to know if the situation is clearer by now.

我正在使用glassfish 4附带的库。

I'm using the libraries shipped with glassfish 4.

是否有JEE-7最佳实践方法来实现这一目标?挖掘过时的讨论真的很难。

Is there a JEE-7-best-practice-way to achieve this? It's really hard to dig through outdated discussions.

提前致谢!

推荐答案

非常好的问题(+1),目前Java EE 7更精简,更容易,但是,SO并没有这么更新。有一种新模式可能对您有用。 边界模式,是的,是带有@Stateless的注释POJO - 在SOA环境中首选的 - 或@Stateful,你开始认为......为什么?。

Really good question (+1), currently Java EE 7 is leaner and easier, but, SO is not so updated. There is a new pattern that could be useful for you. Boundary Pattern, yes, is an annotated POJO with @Stateless -preferred in SOA environments- or @Stateful, you start to think...why?.

首先,边界是您的应用程序的起点并暴露您的服务,在REST phisolophy中您应该执行CRUD等操作(记住:获取,发布,放置,删除),以及ejb就是你想要的事务操作边界(Session Facade)(或其他强大的服务,如Asynchronous,Message Driven Beans等......)。

First, the boundary is the starting point of your application and exposes your services, in the REST phisolophy you should do operations like CRUD (remember : get, post, put, delete), and ejb is just that kind of boundary (Session Facade) that you want for transactional operations (or other powerful services such as Asynchronous, Message Driven Beans, etc...).

因此,EJB是您的服务,您可以将它注入到您想要的任何地方 - 肥皂,RMI,其他CDI POJOS-。现在感谢spec,你可以用@Inject注入一个EJB,容器会发现它真的是一个超级强大的EJB!更精简?......不可能。你的例子是正确的方法,尝试尽可能多地使用@Inject,无论我们是否正在讨论暴露的bean到JSF页面。

So, the EJB is your service and you can inject it wherever you want -rest, soap, RMI, other CDI POJOS-. Thanks to spec now you are able to inject an EJB just with @Inject, and the container will figure out that is really a super powerful EJB!. Leaner?... imposible. Your example is just the right way to go, try to use @Inject as much as possible no matter if we are even talking about exposed beans to JSF pages.

这篇关于通过@Inject将业务逻辑暴露给宁静服务的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆