【问题标题】:Access Spring Web Acclication Context in Stateless EJB在无状态 EJB 中访问 Spring Web Acclication 上下文
【发布时间】:2016-04-29 08:56:01
【问题描述】:

目前我们使用 Spring 4 并有一个 Java 类加载我们所有的 xml-config 文件:

  • 根:基本框架
  • 孩子:项目申请服务
  • 子项:项目申请工作流程
  • 子:框架控制器
  • 子:CXF Web 应用程序上下文

每个孩子都知道它的父母的豆子,一切都很好。现在我必须在 Websphere Application Server 上使用 IBM EJB 来与遗留系统进行通信。这个 EJB 被调用,现在我想使用我们的 Spring Context 来获取一些服务。

EJB 被定义为

@Stateless(mappedName = "ejb/LegacyRocks")
@RemoteHome(com.ibm.websphere.ola.ExecuteHome.class)
public class WolaUseCaseOne {...}

我已经阅读过有关 SpringBeanAutowiringInterceptor 的信息,但我不明白这一点。我没有一个简单的 xml 文件来加载,所以任何人都可以为我提供另一种方式在 EJB 中自动装配 Spring Beans 吗?

PS: 我也找到了这篇文章 (http://www.schakko.de/2013/10/11/sharing-the-spring-application-context-from-a-war-with-ejbs/),但我们不使用 JSF,它对我没有帮助

我已经看过了,没有办法注入完整的上下文,因为我们有一个Webapplication Context,而EJB内部没有这样的Web Context...?

【问题讨论】:

    标签: spring ejb interceptor


    【解决方案1】:

    您需要按照以下方式创建ejb

    @Local
    public interface TestManager {
    
        boolean isValid();
    
    } 
    
    @Stateless(name = "java:global/TestManagerImpl", mappedName = "java:global/TestManagerImpl")
    @EJB(name = "java:global/TestManagerImpl", beanInterface = TestManager.class)
    public class TestManagerImpl implements TestManager {
    
        @PostConstruct
        public void ejbCreate() {
    
        }
    
        @PreDestroy
        public void ejbDestroy() {
    
        }
    
        @Override
        public boolean isValid(){
            //your code
        }
    }
    

    你需要用

    实现spring服务类
    @Service(value = "testServiceImpl")
    public class TestServiceImpl {
    
        @EJB(name = "java:global/TestManagerImpl", mappedName = "java:global/TestManagerImpl")
        private TestManager testManager;
    
        @Override
        public boolean isValid(){
          retrun testManager.isValid(); 
        }
    }
    

    已编辑

    调用ejb到spring服务方法

    @Stateless
    @Interceptors(SpringBeanAutowiringInterceptor.class)
    public class TestManagerImpl implements TestManager {
    
        // automatically injected with a matching Spring bean
        @Autowired
        private TestService testService ;
    
        // for business method, delegate to POJO service impl.
        public String myFacadeMethod(...) {
            return testService.myMethod(...);
        }
    
        ...
    
    }
    

    希望它对你有用!

    【讨论】:

    • 嗨 Ravi,感谢您的快速回复...当我看到代码时,我认为这是将 EJB 注入 Spring 服务的方法,但我需要它反过来:我需要将 Spring 上下文“注入”到 EJB 中以查找一些服务或自动装配一些 bean...
    • @timo.gruetter 我已经更新了我的答案,所以请检查希望它对你有用。
    猜你喜欢
    • 1970-01-01
    • 2017-01-09
    • 1970-01-01
    • 2011-05-29
    • 2012-01-30
    • 1970-01-01
    • 1970-01-01
    • 2016-11-18
    • 1970-01-01
    相关资源
    最近更新 更多