You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring 6.x/Spring Boot 3.x中SimpleRemoteStatelessSessionProxyFactoryBean替代方案咨询

替代SimpleRemoteStatelessSessionProxyFactoryBean的方案

Spring 6.x 版本移除了org.springframework.ejb包下的EJB代理相关类,官方不再封装EJB远程调用的特定实现,推荐直接使用Jakarta EE原生的EJB远程调用API来替代。

具体实现方案

1. 引入必要依赖

确保项目中包含Jakarta EE EJB API依赖,Spring Boot项目可直接引入spring-boot-starter-ejb,或手动添加API依赖:

<!-- Maven依赖示例 -->
<dependency>
    <groupId>jakarta.ejb</groupId>
    <artifactId>jakarta.ejb-api</artifactId>
    <version>4.0.1</version>
    <scope>provided</scope>
</dependency>

2. 用原生API获取远程EJB实例

通过InitialContext直接查找远程EJB,示例代码如下:

import jakarta.naming.InitialContext;
import java.util.Properties;

public class RemoteEjbClient {

    public <T> T getRemoteEjb(Class<T> ejbInterface, String jndiName) throws Exception {
        Properties jndiProps = new Properties();
        // 根据你的EJB服务器配置调整属性,以WildFly为例:
        jndiProps.put("java.naming.factory.initial", "org.wildfly.naming.client.WildFlyInitialContextFactory");
        jndiProps.put("java.naming.provider.url", "http-remoting://localhost:8080");
        jndiProps.put("java.naming.security.principal", "your-username");
        jndiProps.put("java.naming.security.credentials", "your-password");

        InitialContext ctx = new InitialContext(jndiProps);
        return ejbInterface.cast(ctx.lookup(jndiName));
    }

    // 使用示例
    public void invokeRemoteMethod() throws Exception {
        RemoteEjbService ejb = getRemoteEjb(RemoteEjbService.class, 
            "ejb:/your-app/RemoteEjbServiceImpl!" + RemoteEjbService.class.getName());
        ejb.executeRemoteTask();
    }
}

3. 整合Spring容器管理

如果需要将EJB实例纳入Spring容器管理,可通过@Bean方法注册:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class EjbConfig {

    @Bean
    public RemoteEjbService remoteEjbService() throws Exception {
        RemoteEjbClient client = new RemoteEjbClient();
        return client.getRemoteEjb(RemoteEjbService.class, 
            "ejb:/your-app/RemoteEjbServiceImpl!" + RemoteEjbService.class.getName());
    }
}

注意事项

  • JNDI名称格式取决于你的EJB服务器(如WildFly、WebLogic等),需参考对应服务器文档确认命名规则。
  • 不同EJB服务器的InitialContext配置属性存在差异,比如WebLogic会使用weblogic.jndi.WLInitialContextFactory作为初始上下文工厂。
  • Jakarta EE 9+环境下,API包名从javax.ejb改为jakarta.ejb,需确保代码导入路径正确。

内容的提问来源于stack exchange,提问作者Fábio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 22:10:07