如何使用基于注释的配置(即:JavaConfig)在Spring中定义远程EJB Bean [英] How to define Remote EJBs Beans in Spring using annotation-based configuration (ie: JavaConfig)

查看:167
本文介绍了如何使用基于注释的配置(即:JavaConfig)在Spring中定义远程EJB Bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出使用JavaConfig(基于注释的配置)在Spring 4.x中定义Remote EJB 3 bean的最佳方式。



ve看了一下 的文档>< jee:remote-slsb> ,并且已经将一个功能配置连接在一起,但可怕的是:

  @Bean 
public LoginManager getLoginManager(){
SimpleRemoteStatelessSessionProxyFactoryBean factory = new SimpleRemoteStatelessSessionProxyFactoryBean();
String beanName =jndi.ejb3.LoginManager;
factory.setJndiName(beanName);
factory.setBusinessInterface(LoginManager.class);
属性p = new Properties();
p.setProperty(java.naming.factory.initial,org.jnp.interfaces.NamingContextFactory);
p.setProperty(java.naming.factory.url.pkgs,org.jboss.naming:org.jnp.interfaces);
p.setProperty(java.naming.provider.url,jnp:localhost:1099);
factory.setJndiEnvironment(p);
try {
factory.afterPropertiesSet();
} catch(NamingException e1){
e1.printStackTrace();
}
return(LoginManager)factory.getObject();
}

我不应该调用 afterPropertiesSet() / code>在bean定义中,我可以预期,Spring应该自动调用 getObject()。此外,它还意味着为每个要加载的远程EJB定义工厂,这似乎不正确。我会喜欢/预期的一种方式,我可以定义一个可重复使用的工厂,并传递每个bean创建的Interface / JNDI名称,但不起作用。



春季文档表示:


另外,使用@Bean方法,通常会选择使用
编程JNDI查找:使用Spring的
JndiTemplate / JndiLocatorDelegate助手或直接JNDI
InitialContext使用,但不是JndiObjectFactoryBean变体,
将强制您声明返回类型为FactoryBean类型
而不是实际目标类型,使其更难用于其他@Bean方法中的
交叉引用调用,这些方法将在此引用
所提供的资源。


所以现在我很困惑要做什么。



EJB具体的spring文档还建议使用 SimpleRemoteStatelessSessionProxyFactoryBean


定义显式 < jee:local-slsb> / < jee:remote-slsb> 查找简单的
提供一致和更明确的EJB访问配置。


那么我该怎么做干净?

解决方案

您不需要调用显性的afterProperties方法,因为这是spring bean生命周期的一部分。另外,如果将bean声明为工厂bean,那么在需要时,spring会自动使用getObject来获取真实对象。这是修改的代码

  @Bean 
public FactoryBean getLoginManagerFactory(){
SimpleRemoteStatelessSessionProxyFactoryBean factory = new SimpleRemoteStatelessSessionProxyFactoryBean ();
String beanName =jndi.ejb3.LoginManager;
factory.setJndiName(beanName);
factory.setBusinessInterface(LoginManager.class);
属性p = new Properties();
p.setProperty(java.naming.factory.initial,org.jnp.interfaces.NamingContextFactory);
p.setProperty(java.naming.factory.url.pkgs,org.jboss.naming:org.jnp.interfaces);
p.setProperty(java.naming.provider.url,jnp:localhost:1099);
factory.setJndiEnvironment(p);
返回工厂;
}


I'm trying to figure out the best way to define Remote EJB 3 beans in Spring 4.x using JavaConfig (annotation-based configuration).

I've looked at the Spring Docs for <jee:remote-slsb> and have hacked together a functional configuration, but it is terrible:

@Bean
public LoginManager getLoginManager(){
    SimpleRemoteStatelessSessionProxyFactoryBean factory = new SimpleRemoteStatelessSessionProxyFactoryBean();
    String beanName = "jndi.ejb3.LoginManager";
    factory.setJndiName(beanName);
    factory.setBusinessInterface(LoginManager.class);
    Properties p = new Properties();
    p.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory" );
    p.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces" );
    p.setProperty("java.naming.provider.url", "jnp:localhost:1099");
    factory.setJndiEnvironment(p);
    try {
        factory.afterPropertiesSet();
    } catch (NamingException e1) {
        e1.printStackTrace();
    }
    return (LoginManager) factory.getObject();
}

I shouldn't be calling afterPropertiesSet() in the bean definition, and I would have expected that the getObject() should be automatically called by Spring. Furthermore, it also means defining the factory for every Remote EJB that I want to load, which doesn't seem right. I would have liked/expected a way I could define a reusable factory and just pass it the Interface/JNDI name for each bean creation, but that doesn't work.

The spring docs indicate:

Also, with @Bean methods, you will typically choose to use programmatic JNDI lookups: either using Spring’s JndiTemplate/JndiLocatorDelegate helpers or straight JNDI InitialContext usage, but not the JndiObjectFactoryBean variant which would force you to declare the return type as the FactoryBean type instead of the actual target type, making it harder to use for cross-reference calls in other @Bean methods that intend to refer to the provided resource here.

So now I'm confused what to do.

The EJB Specific spring docs also recommend using the SimpleRemoteStatelessSessionProxyFactoryBean:

Defining explicit <jee:local-slsb> / <jee:remote-slsb> lookups simply provides consistent and more explicit EJB access configuration.

So how do I do this cleanly?

解决方案

You don't need to call explicity the afterProperties method since that's part of spring bean lifecycle. Additionally if you declare the bean as a factory bean, spring will automatically use the getObject to get the real object when it is needed. Here is the modified code

    @Bean
public FactoryBean getLoginManagerFactory(){
    SimpleRemoteStatelessSessionProxyFactoryBean factory = new SimpleRemoteStatelessSessionProxyFactoryBean();
    String beanName = "jndi.ejb3.LoginManager";
    factory.setJndiName(beanName);
    factory.setBusinessInterface(LoginManager.class);
    Properties p = new Properties();
    p.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory" );
    p.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces" );
    p.setProperty("java.naming.provider.url", "jnp:localhost:1099");
    factory.setJndiEnvironment(p);
return factory;
}

这篇关于如何使用基于注释的配置(即:JavaConfig)在Spring中定义远程EJB Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆