在EJB中自动装配并在静态变量中访问应用程序上下文

时间:2013-12-19 10:24:50

标签: java spring java-ee static ejb

我们正尝试在EJBS中启用自动装配。

我们有一个用于EJB的拦截器用于自动装配,这个拦截器从不同启动类中的静态变量获取应用上下文。此启动类包含并初始化应用程序上下文。但是,当我们尝试从拦截器访问appContext静态变量时,它为null,当我们从Controller或InternalService中从外部访问它时,它不是null。

我们有两个上下文,即应用程序上下文和Web上下文。

我们将应用程序上下文存储在类中的静态变量中。我们以下列方式动态加载应用程序上下文,并将其存储在静态变量

            // Create Context
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(path);

            // Register Context
            AppInitializer.ctx = context;

除此之外,我们也以类似的方式存储WebApplicationContext。

        XmlWebApplicationContext webContext = new XmlWebApplicationContext();
        webContext.setParent(AppInitializer.getApplicationContext());
        webContext.setConfigLocation(path);
        webContext.setServletContext(getServletContext());
        webContext.refresh();

这会初始化两个应用程序上下文。 EJB按以下方式映射到父(非Web)应用程序上下文中

<!-- Service Register -->
<bean id="testService" class="org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean">
    <property name="jndiName" value="testapp/TestServiceBean/local" />
    <property name="businessInterface" value="com.service.test.api.TestService" />
</bean>

一切正常,应用程序启动时没有任何问题,但是当我们尝试访问applicationContext或者甚至来自Controller或InternalService(Spring Service)的简单静态长变量时,我们得到了

Long value = TestClass.getSampleLongValue(); //The sample long value was set by the AppInitializer class for testing with a long value

value变量不会为NULL并包含已设置的long值。

同样,如果我们尝试从控制器或内部服务(如

)访问应用程序上下文
ApplicationContext application = AppInitializer.getApplicationContext();

应用程序变量不会为空且包含已设置的长值

无论其

从EJB拦截器(WHICH IS THE GOAL)中的静态变量获取应用程序上下文时,为了启用内部服务进行自动装配,如下所示

public class ServiceAutowiringInterceptor extends SpringBeanAutowiringInterceptor {

    @Override
    protected BeanFactory getBeanFactory(Object target) {
        ClassPathXmlApplicationContext ctx = (ClassPathXmlApplicationContext) AppInitializer
                .getApplicationContext();

        return ctx.getBeanFactory();
    }

}

ctx变量为空

此外,当我们在没有拦截器的情况下尝试使用来自WITHIN的TestServiceBean EJB(用于测试)时,这些变量(包括Long变量)都为null

Long value = TestClass.getSampleLongValue(); //The sample long value was set by the AppInitializer class for testing with a long value
ApplicationContext application = AppInitializer.getApplicationContext();

除此之外,AppInitializer类中还有一个静态块,它在服务器启动时被调用。调用AppInitializer.getApplicationContext()时;来自控制器的方法静态块不运行但是当从EJB或EJB拦截器调用时运行静态块。

我们也试过调用“Thread.currentThread()。getContextClassLoader();”控制器和EJB中的方法,以查看是否存在两个类加载器,但两个实例都提供了相同的值

WebappClassLoader
  delegate: false
  repositories:
    /WEB-INF/classes/
----------> Parent Classloader:
java.net.FactoryURLClassLoader@f5f972

有没有人知道为什么EJB访问应用程序上下文(或任何静态变量)时它们显示为null等等。

非常感谢任何帮助。

0 个答案:

没有答案
相关问题