Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java @与Jersey 1.17、Weblogic一起使用的后期构造_Java_Jakarta Ee_Jersey_Weblogic_Jax Rs - Fatal编程技术网

Java @与Jersey 1.17、Weblogic一起使用的后期构造

Java @与Jersey 1.17、Weblogic一起使用的后期构造,java,jakarta-ee,jersey,weblogic,jax-rs,Java,Jakarta Ee,Jersey,Weblogic,Jax Rs,我使用Jersey将一些统计数据作为REST服务公开。 我正在使用Weblogic 但是每次我执行请求以获取统计信息时 System.out.println(“施工后”);被称为 即使我在path@Stateless旁边添加注释,也会发生这种情况 其行为类似于在每个请求上实例化StorageService(req范围) 有没有办法让initialize只调用一次并避免在每个请求上创建StorageService @Path("/statistics") public class StorageS

我使用Jersey将一些统计数据作为REST服务公开。 我正在使用Weblogic

但是每次我执行请求以获取统计信息时 System.out.println(“施工后”);被称为

即使我在path@Stateless旁边添加注释,也会发生这种情况

其行为类似于在每个请求上实例化StorageService(req范围)

有没有办法让initialize只调用一次并避免在每个请求上创建StorageService

@Path("/statistics")
public class StorageService {

    @Context
    private ServletContext application;

    StatisticsStorage statisticsStorage;

    @PostConstruct
    public void initialize() {
        System.out.println("In PostConstruct");
        try {
            statisticsStorage = new StatisticsStorage((String) application.getAttribute(AppProperties.PropKey.STATS_OUTPUT_PATH_PROP.toString()));
        } catch (Exception sqle) {
            sqle.printStackTrace();
        }
    }


    @GET
    // The Java method will produce content identified by the MIME Media type "text/plain"
    @Produces({MediaType.APPLICATION_JSON})
    public Domain getSnapshot() {}

谢谢

Jersey会根据每个请求实例化该类。请注意,可以使用@Context注释的大多数内容都是特定于请求的。最好的办法是使StatisticsStorage字段保持静态,并在第一次请求时初始化它(需要同步)。也许是这样的:

public StorageService(@Context ServletContext application) {
    super(servletConfig, request, httpServletRequest, uriInfo, httpHeaders, securityContext);
    synchronized (this.getClass()) {
        if (statisticsStorage == null) {
            try {
                statisticsStorage = new StatisticsStorage((String) application.getAttribute(AppProperties.PropKey.STATS_OUTPUT_PATH_PROP.toString()));
            } catch (Exception sqle) {
                sqle.printStackTrace();
            }
        }
    }
}
static StatisticsStorage statisticsStorage;
JAX-RS资源类的默认“作用域”是每个请求的作用域。我们还可以将该类注册为singleton。不同的JAX-RS实现可能有不同的实现方式。例如,ApacheWink有一个
@Scope(ScopeType.SINGLETON)
。但唯一可移植的方法是使用子类。比如:

@ApplicationPath("/api")
public class MyApplication extends Application {
    @Override
    public Set<Class<?>> getClasses() {
    }
    @Override
    public Set<Object> getSingletons() {
        Set<Object> singletons = new HashSet<>();
        singletons.add(new StorageService());
        return singletons;
    }
}
@ApplicationPath(“/api”)
公共类MyApplication扩展了应用程序{
@凌驾
公共集