有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在Spring中使用基于注释的配置(即:JavaConfig)定义远程EJB bean

我试图找出在Spring4中定义远程EJB3bean的最佳方法。x使用JavaConfig(基于注释的配置)

我已经研究了Spring Docs for ^{}并拼凑了一个功能配置,但它很糟糕:

@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();
}

我不应该在bean定义中调用afterPropertiesSet(),我本来希望getObject()应该由Spring自动调用。此外,它还意味着为我要加载的每个远程EJB定义工厂,这似乎是不对的。我希望/期望有一种方法可以定义一个可重用工厂,并为每个bean创建传递接口/JNDI名称,但这不起作用

{a2}表示:

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.

所以现在我不知道该怎么办

{a3}还建议使用{}:

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

那么,我该如何干净利落地做到这一点呢


共 (1) 个答案

  1. # 1 楼答案

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

        @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;
    }