gpt4 book ai didi

java - Spring bean 多次加载

转载 作者:行者123 更新时间:2023-12-01 15:50:48 25 4
gpt4 key购买 nike

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

<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 的单个实例,但不知道如何管理它。

感谢您的宝贵时间!

最佳答案

您应该为您的 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 进程生命周期以减少资源负载。

关于java - Spring bean 多次加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6081226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com