Spring bean 多次加载

发布于 2024-11-08 16:34:28 字数 3442 浏览 0 评论 0原文

我正在编写 Apache CXF Web 服务并使用 Spring 加载我的 bean。我唯一的 bean 是从 Java 调用外部进程 (MATLAB)。我的 bean 定义如下:

<bean id="matlabEngine" class="org.burch.pca.matlab.MatlabEngine"
    init-method="start" scope="singleton">
    <constructor-arg value="${matlab.engine.path}"></constructor-arg>
</bean>

<bean
    class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="searchContextAttributes" value="true" />
    <property name="contextOverride" value="true" />
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
        <list>
            <value>classpath:/pca-engine.properties</value>
        </list>
    </property>
</bean>

我的 MatlabEngine bean 的一部分如下:

/**
 * Path to MATLAB engine.
 */
private String pathToEngine;

public MatlabEngine(String pathToEngine) throws MatlabConnectionException, MatlabInvocationException{
    super();
    setPathToEngine(pathToEngine);
}

/**
 * Starts engine and goes to path defined by argument
 * @param pathToEngine
 * @throws MatlabConnectionException
 * @throws MatlabInvocationException
 */
public void start() throws MatlabConnectionException, MatlabInvocationException{
  //Create a factory
  RemoteMatlabProxyFactory factory = new RemoteMatlabProxyFactory();

  //Get a proxy, launching MATLAB in the process
  proxy = factory.getProxy();

  //Display welcoming messages in MATLAB Command Window
  proxy.eval(MatlabCommandsRegistry.disp(MATLAB_ENGINE_WELCOME_1));
  proxy.eval(MatlabCommandsRegistry.disp(MATLAB_ENGINE_WELCOME_2));

  if(pathToEngine!= null && !"".equals(pathToEngine)){
      logM("Switching to engine directory...");
      String goToEngineRootDir = MatlabCommandsRegistry.cd(pathToEngine);
      proxy.eval(goToEngineRootDir);
      logM("Sucessfully changed engine dir to "+pathToEngine);
  }
}

当我在 Tomcat 中部署 Web 服务时,它会很好地启动 MATLAB 进程(加载 bean)。

但是,当我使用以下代码创建对 Web 服务端点的客户端请求时:

public static void main(String args[]) throws Exception {

    JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

    factory.getInInterceptors().add(new LoggingInInterceptor());
    factory.getOutInterceptors().add(new LoggingOutInterceptor());
    factory.setServiceClass(UploadService.class);
    factory.setAddress("http://localhost:8080/auth-ws-1.0.0/services/upload");
    UploadService client = (UploadService) factory.create();

    UploadEntity resume=new UploadEntity();
    resume.setFileName("Image490");
    resume.setFileType("jpg");

    //Work arround data handler....
    DataSource source = new FileDataSource(new File("C:\\Users\\Pictures\\thumb.png"));
    DataHandler dataHandle = new DataHandler(source);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    dataHandle.writeTo(stream);
    resume.setPayload(stream.toByteArray());
    client.uploadFile(resume);
    System.exit(0);

}

我的服务器带出 MATLAB 进程的新实例(bean 再次加载 - 非常重且不受欢迎)。如果只有一个 bean 来服务所有处理和所有请求,我该怎么办?我是 Spring 新手,我认为我的问题是我在这里处理多个上下文。我希望他们共享单例 bean 的单个实例,但不知道如何管理它。

谢谢您的宝贵时间!

I am writing Apache CXF web services and use Spring for loading my beans. My only bean is calling external process (MATLAB) from Java. My beans definition looks as below:

<bean id="matlabEngine" class="org.burch.pca.matlab.MatlabEngine"
    init-method="start" scope="singleton">
    <constructor-arg value="${matlab.engine.path}"></constructor-arg>
</bean>

<bean
    class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="searchContextAttributes" value="true" />
    <property name="contextOverride" value="true" />
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
        <list>
            <value>classpath:/pca-engine.properties</value>
        </list>
    </property>
</bean>

Piece of my MatlabEngine bean is as below:

/**
 * Path to MATLAB engine.
 */
private String pathToEngine;

public MatlabEngine(String pathToEngine) throws MatlabConnectionException, MatlabInvocationException{
    super();
    setPathToEngine(pathToEngine);
}

/**
 * Starts engine and goes to path defined by argument
 * @param pathToEngine
 * @throws MatlabConnectionException
 * @throws MatlabInvocationException
 */
public void start() throws MatlabConnectionException, MatlabInvocationException{
  //Create a factory
  RemoteMatlabProxyFactory factory = new RemoteMatlabProxyFactory();

  //Get a proxy, launching MATLAB in the process
  proxy = factory.getProxy();

  //Display welcoming messages in MATLAB Command Window
  proxy.eval(MatlabCommandsRegistry.disp(MATLAB_ENGINE_WELCOME_1));
  proxy.eval(MatlabCommandsRegistry.disp(MATLAB_ENGINE_WELCOME_2));

  if(pathToEngine!= null && !"".equals(pathToEngine)){
      logM("Switching to engine directory...");
      String goToEngineRootDir = MatlabCommandsRegistry.cd(pathToEngine);
      proxy.eval(goToEngineRootDir);
      logM("Sucessfully changed engine dir to "+pathToEngine);
  }
}

When I deploy web services in Tomcat, it brings up MATLAB process nicely (bean gets loaded).

However, when I create client request to web service endpoint with this code:

public static void main(String args[]) throws Exception {

    JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

    factory.getInInterceptors().add(new LoggingInInterceptor());
    factory.getOutInterceptors().add(new LoggingOutInterceptor());
    factory.setServiceClass(UploadService.class);
    factory.setAddress("http://localhost:8080/auth-ws-1.0.0/services/upload");
    UploadService client = (UploadService) factory.create();

    UploadEntity resume=new UploadEntity();
    resume.setFileName("Image490");
    resume.setFileType("jpg");

    //Work arround data handler....
    DataSource source = new FileDataSource(new File("C:\\Users\\Pictures\\thumb.png"));
    DataHandler dataHandle = new DataHandler(source);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    dataHandle.writeTo(stream);
    resume.setPayload(stream.toByteArray());
    client.uploadFile(resume);
    System.exit(0);

}

my server brings out new instance of MATLAB process (bean gets loaded again - very heavy and undesirable). What could I do to have only one bean which will be used to serve all processing and all requests? I am new to Spring, and I am thinking that my problem is that I am dealing with multiple contexts here. I want them to share a single instance of a singleton bean but don't know how to manage this.

Thank you for your time!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫腻 2024-11-15 16:34:28

您应该为您的 bean 启用单例模式。

看看这个: http://static .springsource.org/spring/docs/1.2.x/reference/beans.html#beans-factory-modes

Bean 防御可能如下所示:

<!-- Spring property loading bean -->
<bean
    class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer" singleton="true">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="searchContextAttributes" value="true" />
    <property name="contextOverride" value="true" />
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
        <list>
            <value>classpath:/pca-engine.properties</value>
        </list>
    </property>
</bean>

我认为您应该明智地管理 MATLAB 进程生命周期以减少资源负载。

You should enable singleton mode for you bean.

Look at this: http://static.springsource.org/spring/docs/1.2.x/reference/beans.html#beans-factory-modes

Bean defenition may looks like this:

<!-- Spring property loading bean -->
<bean
    class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer" singleton="true">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="searchContextAttributes" value="true" />
    <property name="contextOverride" value="true" />
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
        <list>
            <value>classpath:/pca-engine.properties</value>
        </list>
    </property>
</bean>

I think you should manage MATLAB process lifecircle wisely to decrease resource loading.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文