【问题标题】:Override LocalStatelessSessionProxyFactoryBean without xml configuration在没有 xml 配置的情况下覆盖 LocalStatelessSessionProxyFactoryBean
【发布时间】:2014-01-20 09:30:11
【问题描述】:

首先是问题,然后是解释:我想摆脱 Beans XML 配置并向我的 EJB 上下文添加一些信息。

我在我的项目(项目的 Web 部分)中有一个名为 StatelessEjbSupportLocal 的类,它扩展了 LocalStatelessSessionProxyFactoryBean,这是实现:

public class StatelessEjbSupportLocal 
        extends LocalStatelessSessionProxyFactoryBean {

    private UserInfo userInfo;

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        FacesContext context = FacesContext.getCurrentInstance();
        Properties properties = new Properties();
        properties.put(Context.INITIAL_CONTEXT_FACTORY, 
             "org.jboss.security.jndi.JndiLoginInitialContextFactory");
        properties.put(Context.PROVIDER_URL, "jnp://localhost:1099/");
        properties.put(Context.SECURITY_PRINCIPAL, 
                userInfo.getLogin() + ";" + userInfo.getHost());
        properties.put(Context.SECURITY_CREDENTIALS, userInfo.getIdUser());
        super.setJndiEnvironment(properties);
        refreshHome();
    }
    public UserInfo getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }
}

我的 apps.xml 上有 bean 配置:

(...)
<context:annotation-config/>
<context:component-scan base-package="my.package.controller" />

<bean id="someBusiness" class="my.package.StatelessEjbSupportLocal">
    <property name="jndiName" value="SomeBusinessImpl/local" />
    <property name="businessInterface" value="my.package.business.SomeBusiness" />
    <property name="userInfo" ref="userInfo" />
</bean>
(...)

我的 web.xml 具有加载上述 xml 的配置:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:apps.xml</param-value>
</context-param>

我的UserInfo 类使用@Component 进行注释,并在用户登录后填充到我的SecurityFilter(在我的web.xml 中配置为过滤器)。我在那里设置了主机、登录名和 id 并将其放在会话中。

一切正常。我有这个是因为我在 EJB SessionContext 上设置了用户,以便在我的业务(服务)类的拦截器上获取此信息。这样我就不需要将用户信息传递给我的所有业务实现(我需要这个,因为在我的连接上,我使用类似于某种预连接配置的过程将用户信息设置到数据库以达到审计目的)。

我想做的是摆脱apps.xml 上的beans XML 配置。换句话说,此 LocalStatelessSessionProxyFactoryBean 的某种拦截器将设置 UserInfo 而无需在每个 bean 配置上使用 &lt;property name="userInfo" ref="userInfo" /&gt;。 因为我可以拥有我的apps.xml

<context:annotation-config/>
<context:component-scan base-package="my.package.controller" />

我的托管 bean 上的 SomeBusiness 属性用 @Autowired 注释可以正常工作。

如果您需要更多信息,请告诉我,我会用它来编辑这个问题。

提前致谢。

【问题讨论】:

    标签: java spring ejb-3.0


    【解决方案1】:

    这不漂亮,我不推荐它。您应该使用 @Bean 带注释的方法来构造 bean 或保留您的 XML。

    如果您仍想继续此操作,请使用 @Component 注释您的 StatelessEjbSupportLocal 类。用@Autowired 注释它的setUserInfo 并覆盖setBusinessInterfacesetJndiName 方法,用@Value 用您要使用的值注释它们,并调用它们的超级实现。例如,

    @Override
    @Value("SomeBusinessImpl/local") // or reference some property
    public void setJndiName(String jndiName) {
        super.setJndiName(jndiName);
    }
    

    然后在你的组件扫描中声明包含StatelessEjbSupportLocal 的包。

    由 OP 编辑​​:

    正如 Sotirios Delimanolis 推荐的那样。我离开了 XML 配置。

    【讨论】:

    • 我会按照您的建议使用 XML 配置。但是对于您的建议,我不太同意:public void setJndiName(String jndiName) 应该在哪里覆盖它?因为有了@Value("SomeBusinessImpl/local"),它只适合这个特定的业务,那么我的应用程序中的其他一百个业务呢?我在这个问题上是对的吗?
    • @JorgeCampos 它应该在你的StatelessEjbSupportLocal 类中被覆盖。 If you want to externalize the value used, you can do that。 (见第二个答案)
    • 我必须有一个属性文件而不是 XML =\ 我将使用 XML 配置来完成。明天当我开始工作并投入工作时,我会接受你的建议。感谢您抽出宝贵的时间,非常感谢。
    猜你喜欢
    • 2019-06-29
    • 1970-01-01
    • 1970-01-01
    • 2014-11-18
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    • 2013-05-18
    相关资源
    最近更新 更多