在Java Jersey 2 JAX-RS中初始化单例 [英] Initialize singleton in Java Jersey 2 JAX-RS

查看:192
本文介绍了在Java Jersey 2 JAX-RS中初始化单例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是泽西岛的新手(2.22.2),所以请耐心等待。我正在创建一个与LDAP服务器连接的REST服务,用于存储,删除和检索用户数据。该服务通过执行加密/解密充当安全中介。

I'm new to Jersey (2.22.2), so bear with me please. I am creating a REST service that interfaces with an LDAP server for storing, removing, and retrieving user data. The service acts as a security intermediary by performing encryption/decryption.

在使用REST服务之前必须进行相当多的初始化,我会喜欢只执行一次初始化(当应用程序部署在服务器上时)。因此,这项服务将以单身人士的形式运行。

There is quite a bit of initialization that must take place before the REST services can be utilized, and I would like to only perform this initialization once (when the application is deployed on the server). So this service would be run as a singleton.

如果有人可以给我一些关于最佳方法的指示,我将不胜感激?谢谢!

Would appreciate if someone could give me some pointers on the best way to go about doing this? Thanks!

推荐答案

用户Spring框架。

User Spring Framework.

https://projects.spring.io / spring-framework /

https://jersey.java.net/documentation/latest/spring.html

这里有一个完整的工作示例:

There a full working example here:

https: //github.com/jersey/jersey/tree/2.22.2/examples/helloworld-spring-webapp

您基本上将此依赖项添加到您的球衣中项目,它将自动包括Spring:

You basically add this dependency to your jersey project and it'll include Spring automatically:

        <dependency>
          <groupId>org.glassfish.jersey.ext</groupId>
          <artifactId>jersey-spring3</artifactId>
          <version>${project.version}</version>
        </dependency>

然后在名为applicationContext.xml和src / main / resources的文件中定义Spring Bean:

and then you define your Spring Beans in a file called applicationContext.xml and src/main/resources:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="mySingletonService" class="com.test.MyService"/>
<beans/>

最后但并非最不重要的是,在您的实际资源中,您可以使用@Autowire注释注入此单件服务:

Last But not least, in your actual resource you can inject this singleton service using the @Autowire annotation:

    @Path("/resource")
@Component
public class MyResource {

    @Autowired
    private MyService myService;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getHello() {
        return myService.sayHello();
    }
}

在资源中使用@SingletonResource和初始化状态是一个可怕的,可怕的想法IMO。关注点分离非常重要,将状态保存在REST资源中非常糟糕。通过创建一个LDAPResource和一个LDAPService来分离处理您的接口(REST)和业务逻辑的代码。 Spring在这里播放的部分只是你必须自己实例化的连线。

Using the @SingletonResource and initializing state within the resource is a terrible, terrible idea IMO. Separation of concerns is important, and keeping state in a REST Resource is just plain awful. Separate the code that deals with your interface (REST) and your business logic by creating let's say an LDAPResource and an LDAPService. The part Spring plays here is just the wiring that otherwise you'd have to instantiate yourself.

这篇关于在Java Jersey 2 JAX-RS中初始化单例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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