简体   繁体   English

EJB优先于相同的绑定名称

[英]EJB Priority over same binding name

Say I have an EJB with the following configuration: 说我有一个具有以下配置的EJB:

package com.main.notsimulated

@Singleton
@EJB(name = "java:/sample/MainOne", beanInterface = MainOne.class)
public class MainOne {}

This ejb needs to be present for some other deployables to work. 该ejb需要存在,其他一些可部署的工具才能工作。 However, using this MainOne modue is not very feasable for me in a testing env. 但是,在测试环境中使用MainOne方法对我来说不太可行。 Instead, I would rather inject my own custom version at runtime. 相反,我宁愿在运行时注入自己的自定义版本。

package com.main.simulated

@Singleton
@EJB(name = "java:/sample/MainOne", beanInterface = MainOne.class)
public class MainOne {}

(Note, these are two different jar files) (请注意,这是两个不同的jar文件)

Hence, my idea here is, let's try to replace the currently deployed with a custom version on the fly. 因此,我的想法是,让我们尝试即时用自定义版本替换当前部署的版本。 The reason I want to do this is because I do not want to change the nonsimulated version at all, nor effect the consumers of the ejb in any way. 我之所以要这样做,是因为我根本不想更改非模拟版本,也不希望以任何方式影响ejb的使用者。 ie All that the consumer currently does is look for that particular jndi name, and performs an indejection and a casting to a particular interface. 即,消费者当前所做的只是查找该特定的jndi名称,并对特定的接口执行注入和强制转换。

I have looked at this post in hopes of figuring out if my MainOne Class from com.main.simulated can evict the currently instantiated MainOne class. 我看了这篇文章 ,希望弄清楚com.main.simulated中的MainOne类是否可以退出当前实例化的MainOne类。 However, the the selected answer states it is not programatically possible to start or stop an ejb. 但是,所选答案表明无法以编程方式启动或停止ejb。 I have also looked at this post , but this is more of a practical guide as to how we can inject these beans inour calls. 我也看了这篇文章 ,但这更多是关于如何在调用中注入这些bean的实用指南。

Hence, my question is, can my latter implementation (com.main.simulated) somehow "replace" the other bean, and ensure the com.main.notsimulated version is never executed? 因此,我的问题是,我的后一个实现(com.main.simulated)是否可以以某种方式“替换”另一个bean,并确保从不执行com.main.notsimulated版本?

Deploying two classes, with the same binding is obviously not possible. 显然不可能部署具有相同绑定的两个类。 When trying to do so, one will get a binding exception. 尝试这样做时,将获得绑定异常。 However, contrary to my original research, programatically binding a bean is entirely possible. 但是,与我最初的研究相反,以编程方式绑定bean是完全可能的。 Hence, the solution as to how one can "hijack" an old binding, and replace it with the new is as follows: (Note, replace the class names with what you need) 因此,关于如何“劫持”旧绑定并将其替换为新绑定的解决方案如下:(注意,将类名替换为所需的名称)

package com.main.simulated

@Startup
@Singleton
public class MainOne {

    @PostConstruct
    private void rebindClass() throws NamingException {
        final Context context = new InitialContext();
        context.rebind("java:/sample/MainOne", this);
    }

    // other methods that will be called

}

Three important things about this class are: Removal of the @EJB annotation, the @Startup annotation and the rebind of context. 此类的三件事很重要:删除@EJB批注,@ Startup批注和上下文的重新绑定。 The @Startup ensures the @PostConstruct method gets called when our container loads our class. @Startup确保在容器加载我们的类时调用@PostConstruct方法。 When this happens, the method rebinds a class for a value. 发生这种情况时,该方法将一个类重新绑定为一个值。 Hence, this is the hijack location. 因此,这是劫持位置。

Hope this helps someone. 希望这对某人有帮助。

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

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