球衣2 + HK2-@ApplicationScoped无法正常工作 [英] Jersey 2 + HK2 - @ApplicationScoped not working

查看:102
本文介绍了球衣2 + HK2-@ApplicationScoped无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我上课

@ApplicationScoped
public class Service{
 private Map<String, Integer> something ;
 private final Logger LOGGER = LoggerFactory.getLogger(Service.class);

 @PostConstruct
 public void initialize(){
    something = new HashMap<>();
 }

 public void increase(String userName){
    something.put(userName, something.getOrDefault(userName, 0) + 1);
 }

 public Map<String, Integer> getSomething(){
    return this.something;
 }

 public Integer getSomethingForUser(String userName){
    return something.getOrDefault(userName, 0);
 }
}

我想成为全局一个实例.

Which I want to be globally one instance.

问题是,当我在两个不同的地方注入此服务时,我有两个不同的服务实例-导致始终返回计数器0. .toString()返回如下:

The problem is, that when I'm injecting this service in two different places, I have two different instances of the service - which causes to return always counter 0. .toString() returns as follows:

package.services.Service@492e4f4b
package.services.Service@4bc86c4d

我创建了此服务来测试我的HK2-Jersey实施,显然无法正常工作.

I created this service to test my HK2-Jersey implementation, which is apparently not working as should.

Web.xml:

<servlet-name>jersey-serlvet</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
         <param-name>jersey.config.server.provider.packages</param-name>
         <param-value>io.swagger.jaxrs.listing,mypackage.rest</param-value>
    </init-param>
     <init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value>
            io.swagger.jaxrs.listing.ApiListingResource,
            io.swagger.jaxrs.listing.SwaggerSerializers
        </param-value>
    </init-param>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>mypackage.config.ApplicationConfiguration</param-value>
      </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>jersey-serlvet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

ApplicationConfiguration.java:

ApplicationConfiguration.java:

public class ApplicationConfiguration extends ResourceConfig {
public ApplicationConfiguration() {
     register(new AbstractBinder() {
         @Override
         protected void configure() {
             bind(Service.class).to(Service.class);    
         }
     });

    packages(true, "com.mypackage.rest");
}

}

如果没有此bind函数,则服务器将引发不满足@Inject的异常.

Without this bind function, server throws exception that @Inject wasn't satisfied.

任何人都可以指出,这是怎么回事?

Can anybody point out, what is wrong?

推荐答案

HK2中没有@ApplicationScoped这样的东西.那只是CDI(这是不同的).在HK2中有一个单例范围.通过配置,您可以

There's no such thing as @ApplicationScoped in HK2. That is only CDI (which is different). In HK2 there's a singleton scope. With your configuration, you can either do

bind(new Service()).to(Service.class); 

这将自动使其成为单例.唯一的问题是容器会丢失所有注射(如果需要).另一种方法是在in(Scope)方法中设置范围

which will automatically make it a singleton. Only problem with this is that you lose any injection by the container (if you require any). The other way is to set the scope in the in(Scope) method

bind(Service.class).to(Service.class).in(Singleton.class); 

那是javax.inject.Singleton.

这篇关于球衣2 + HK2-@ApplicationScoped无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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