简体   繁体   English

Spring MVC对单例的多个请求

[英]Spring MVC multiple requests on singleton

I have a problem with Spring MVC. 我有Spring MVC的问题。 I am using Stanford NLP and I put initialization of it to singleton class. 我正在使用Stanford NLP,我把它初始化为singleton类。

@Component
@Scope("singleton")
public class JavaNLP implements NlpInterface
{
  private DependencyNLP nlp_object = null;

  @PostConstruct
  public void init()
  {
    if (nlp_object == null) {
      nlp_object = new DependencyNLP();
      nlp_object.init("tokenize, ssplit, pos, lemma, ner, parse, dcoref");
    }
  }
  @Override
  public void init(String name, DataContainer container)
  {
    this.container = container;
    nlp_object.annotate(container.getText());
  }
  @Override
  public void execute()
  {
     ...
  }
}

Every request is calling init(String name, DataContainer container) and execute . 每个请求都调用init(String name, DataContainer container)execute Problem is in 70% requests nlp_object is not initialized. 问题在于70%的请求 nlp_object未初始化。

Inside of a controller: 控制器内部:

@Autowired
@Qualifier("javaNLP")
private NlpInterface nlpInterface;

@RequestMapping(value = "/parse", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, headers = {"Content-type=application/json"})
  public @ResponseBody
  String parseWithGazetteerinJSON(@RequestBody DataContainer container)
  {
    String name = "Parsing text";

    nlpInterface.init(name, container);
    nlpInterface.execute();
    JSONArray triples = nlpInterface.getTriplesAsJSON();

    return triples.toString();
  }

EDIT 1 编辑1

Unfortunetly it is still not working. 不幸的是,它仍然无法正常工作。 I think I found where is the problem, just dont know how to fix it. 我想我发现问题在哪里,只是不知道如何解决它。

I put configuration + calling init function into config 我将配置+调用init函数放入config

<mvc:annotation-driven />
<context:component-scan base-package="com.metadata.tripletws.model" />
<context:component-scan base-package="com.metadata.tripletws.controller" />
<bean id="nlp" class="com.metadata.tripletws.service.Nlp" init-method="init"></bean>

I have created new object called Nlp and it is just sort of envelope for DependencyNLP (external library) 我创建了一个名为Nlp的新对象,它只是DependencyNLP(外部库)的一种包络

@Component
public class Nlp
{

  private DependencyNLP nlp;

  public void init()
  {
    nlp = new DependencyNLP();
    nlp.init();
  }

  public DependencyNLP getInstance()
  {
    return nlp;
  }

  public void execute()
  {
     ...
  }
  ...
}

Then I added this code to the controller: 然后我将此代码添加到控制器:

DependencyNLP nlpInstance = nlp.getInstance();
System.out.println(nlpInstance);
nlpInstance.annotate(container.getSection().getText());
nlpInstance.execute(...);

and defined private Autowired variable 并定义了私有自动变量

@Autowired
private Nlp nlp;

Printed nlpInstance is always same. 打印的nlpInstance始终相同。 That makes sense in the end. 这最终有意义。 But here is the problem I beliave. 但这是我所看到的问题 Requests are influencing each other. 请求相互影响。

Does somebody know how to make it run? 有人知道如何让它运行?

Thanks 谢谢

Instead of initializing the nlp_object in a @PostConstruct method try injecting it as a resource to your implementation of NlpInterface 而不是在@PostConstruct方法中初始化nlp_object尝试将其作为资源注入到NlpInterface的实现中

Not sure if this will solve your problem, but if it were me I'd give it a go. 不确定这是否能解决您的问题,但如果是我,我会试一试。

Also, you init() method(if you must go that way) should be synhcronized with double if ( singletonObj == null) gates outside and inside the sync block. 此外, init()方法(如果必须采用这种方式)应与同步块外部和内部的double if ( singletonObj == null)门同步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM