无法使用较新版本的Java示例化Bean

oalqel3c  于 2023-03-21  发布在  Java
关注(0)|答案(3)|浏览(148)

我面临着一个项目升级,我正在努力解决一个问题,从很多天。我真的希望一些SpringMaven可以帮助。基本上,这个项目是一个Java应用程序使用Spring和ZK,部署在Websphere上。该项目是用Java 7编译的,运行在Websphere上的Java 7,现在它必须是Java 8到Java 8。
我的问题发生在服务器启动时:

[3/17/23 14:07:16:223 CET] 00000094 SystemOut     O [Default : 0] 14:07:16.222 | ERROR (Logger.java:37) - getSession error
java.lang.NullPointerException
    at it.reply.filenet.services.SuperUserService.getSession(SuperUserService.java:84)
    at it.reply.filenet.services.DataDictionaryService.init(DataDictionaryService.java:212)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:90)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
    at java.lang.reflect.Method.invoke(Method.java:508)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1640)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1581)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1511)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:636)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:384)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
    at com.ibm.ws.webcontainer.webapp.WebApp.notifyServletContextCreated(WebApp.java:1734)
    at com.ibm.ws.webcontainer.webapp.WebAppImpl.initialize(WebAppImpl.java:415)
    at com.ibm.ws.webcontainer.webapp.WebGroupImpl.addWebApplication(WebGroupImpl.java:88)
    at com.ibm.ws.webcontainer.VirtualHostImpl.addWebApplication(VirtualHostImpl.java:171)
    at com.ibm.ws.webcontainer.WSWebContainer.addWebApp(WSWebContainer.java:904)
    at com.ibm.ws.webcontainer.WSWebContainer.addWebApplication(WSWebContainer.java:789)
    at com.ibm.ws.webcontainer.component.WebContainerImpl.install(WebContainerImpl.java:427)
    at com.ibm.ws.webcontainer.component.WebContainerImpl.start(WebContainerImpl.java:719)
    at com.ibm.ws.runtime.component.ApplicationMgrImpl.start(ApplicationMgrImpl.java:1211)
    at com.ibm.ws.runtime.component.DeployedApplicationImpl.fireDeployedObjectStart(DeployedApplicationImpl.java:1390)
    at com.ibm.ws.runtime.component.DeployedModuleImpl.start(DeployedModuleImpl.java:639)
    at com.ibm.ws.runtime.component.DeployedApplicationImpl.start(DeployedApplicationImpl.java:979)
    at com.ibm.ws.runtime.component.ApplicationMgrImpl.startApplication(ApplicationMgrImpl.java:795)
    at com.ibm.ws.runtime.component.ApplicationMgrImpl.startApplicationDynamically(ApplicationMgrImpl.java:1413)
    at com.ibm.ws.runtime.component.ApplicationMgrImpl.start(ApplicationMgrImpl.java:2273)
    at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.start(CompositionUnitMgrImpl.java:436)
    at com.ibm.ws.runtime.component.CompositionUnitImpl.start(CompositionUnitImpl.java:123)
    at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.start(CompositionUnitMgrImpl.java:379)
    at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.access$500(CompositionUnitMgrImpl.java:127)
    at com.ibm.ws.runtime.component.CompositionUnitMgrImpl$1.run(CompositionUnitMgrImpl.java:654)
    at com.ibm.ws.security.auth.ContextManagerImpl.runAs(ContextManagerImpl.java:5554)
    at com.ibm.ws.security.auth.ContextManagerImpl.runAsSystem(ContextManagerImpl.java:5680)
    at com.ibm.ws.security.core.SecurityContext.runAsSystem(SecurityContext.java:255)
    at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.startCompositionUnit(CompositionUnitMgrImpl.java:668)
    at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.startCompositionUnit(CompositionUnitMgrImpl.java:612)
    at com.ibm.ws.runtime.component.ApplicationMgrImpl.startApplication(ApplicationMgrImpl.java:1303)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:90)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
    at java.lang.reflect.Method.invoke(Method.java:508)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:83)
    at sun.reflect.GeneratedMethodAccessor42.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
    at java.lang.reflect.Method.invoke(Method.java:508)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:287)
    at javax.management.modelmbean.RequiredModelMBean$4.run(RequiredModelMBean.java:1263)
    at java.security.AccessController.doPrivileged(AccessController.java:664)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:91)
    at javax.management.modelmbean.RequiredModelMBean.invokeMethod(RequiredModelMBean.java:1257)
    at javax.management.modelmbean.RequiredModelMBean.invoke(RequiredModelMBean.java:1096)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:831)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:813)
    at com.ibm.ws.management.AdminServiceImpl$1.run(AdminServiceImpl.java:1350)
    at com.ibm.ws.security.util.AccessController.doPrivileged(AccessController.java:118)
    at com.ibm.ws.management.AdminServiceImpl.invoke(AdminServiceImpl.java:1243)
    at com.ibm.ws.management.connector.AdminServiceDelegator.invoke(AdminServiceDelegator.java:181)
    at com.ibm.ws.management.connector.ipc.CallRouter.route(CallRouter.java:247)
    at com.ibm.ws.management.connector.ipc.IPCConnectorInboundLink.doWork(IPCConnectorInboundLink.java:360)
    at com.ibm.ws.management.connector.ipc.IPCConnectorInboundLink$IPCConnectorReadCallback.complete(IPCConnectorInboundLink.java:602)
    at com.ibm.ws.ssl.channel.impl.SSLReadServiceContext$SSLReadCompletedCallback.complete(SSLReadServiceContext.java:1833)
    at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:175)
    at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
    at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
    at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
    at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
    at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
    at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1892)

SuperUserService是这样的,它失败了,因为authenticationMgr是null(注意,当“Java 7 to 7”工作时):

@Override
    public Object getSession() {
        if (session != null) return session;
    
        try {
            ITrustedLoginManager trustedLoginManager = new TrustedLoginManager(loginFile);
            LoginInfo loginInfo = trustedLoginManager.getLoginInfo();
    
            if (authenticationMgr == null) {
                authenticationMgr = (IAuthenticationMgr) BeanFactory.getBean("SessionlessAuthenticationMgr");
            }
    
            session = (ObjectStore) authenticationMgr.generateSession(loginInfo.getUser(), loginInfo.getPassword(),
                    loginInfo.getRepository()); //LINE 84
        } catch (Exception e) {
            BeanFactory.getLogger().error(this, "getSession error", e);
        }
        return session;
    }

BeanFactory.getBean(),返回null:

public static Object getBean(String beanName) {
        Object bean = null;
        try {
            bean = SpringUtil.getBean(beanName);
        } catch (org.zkoss.zk.ui.UiException e) {
            WebApplicationContext context = null;
            try {
                HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                        .getRequest();
                context = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
            } catch (Throwable t) {
                // ignore, returns null
                // Logger.getRootLogger().debug("Eccezione ignorata - getBean: " + beanName);
            }
            if (context != null) {
                bean = context.getBean(beanName);
            } else {
                try {
                    bean = SpringApplicationContext.getBean(beanName); //<-- RETURNS NULL HERE, BECAUSE THERE'S NO CONTEXT
                } catch (BeanCreationException ex) {
                        Logger.getRootLogger().warn(beanName
                                + " - E' richiesta la creazione del bean in un contesto non valido. Nota: è possibile che il chiamante ignori l'errore",
                                ex);
                    } else {
                        Logger.getRootLogger()
                                .warn("Problema in creazione del bean spring tramite SpringApplicationContext", ex);
                    }
                } catch (Throwable t) {
                    Logger.getRootLogger().warn(
                            "Eccezione non gestita in creazione del bean spring tramite SpringApplicationContext", t);
                }
            }
        } catch (Exception e) {
        }
        return bean;
    }

SpringApplicationContext:

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * Wrapper to always return a reference to the Spring Application Context from within non-Spring enabled beans. Unlike
 * Spring MVC's WebApplicationContextUtils we do not need a reference to the Servlet context for this. All we need is
 * for this bean to be initialized during application startup.
 */
public class SpringApplicationContext implements ApplicationContextAware {

    private static ApplicationContext CONTEXT;

    /**
     * This method is called from within the ApplicationContext once it is done starting up, it will stick a reference
     * to itself into this bean.
     *
     * @param context
     *            a reference to the ApplicationContext.
     */
    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        System.out.println(
                "_____________________________ SpringApplicationContext - calling setApplicationContext _____________________________");
        if (Logger.getRootLogger().isDebugEnabled()) Logger.getRootLogger()
                .debug("SpringApplicationContext - calling setApplicationContext");
        System.out.println("SpringApplicationContext - calling setApplicationContext");
        CONTEXT = context;
    }

    /**
     * This is about the same as context.getBean("beanName"), except it has its own static handle to the Spring context,
     * so calling this method statically will give access to the beans by name in the Spring application context. As in
     * the context.getBean("beanName") call, the caller must cast to the appropriate target class. If the bean does not
     * exist, then a Runtime error will be thrown.
     *
     * @param beanName
     *            the name of the bean to get.
     * @return an Object reference to the named bean.
     */
    public static Object getBean(String beanName) {
        if (CONTEXT != null) {
            //System.out.println("start getBean Spring di " + beanName);
            return CONTEXT.getBean(beanName);
        } else {
            //System.out.println("No CONTEXT defined in SpringApplicationContext");
            return null;
        }
    }

当输入getBean()时,CONTEXT为空,因为setApplicationContext()从未启动!
这个项目是用一些非常旧的jar编译的:

所以,我用3.2.16版本更新了它们(我在其他一些线程中读到它是Java 8的最低版本):

不幸的是,问题是一样的。
我的applicationContext的一部分(注意GlobalCacheServiceGlobalPropertiesService是示例化的,使用两个版本的Spring):

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <bean id="GlobalCacheService" scope="singleton" class="it.reply.services.GlobalCacheService" />
    
    <bean id="ObjectsAttrsDef" scope="singleton" class="it.reply.filenet.entities.ObjectsAttrsDef" />
    
    <bean id="ObjectActionService" scope="singleton" class="it.reply.client.services.actions.ObjectActionService"/>
    
    <bean id="GlobalPropertiesService" scope="singleton" class="it.reply.services.GlobalPropertiesService" init-method="init" >
        <property name="propertiesFiles" value="${geno.global.properties}" />
    </bean> 

    <bean id="SessionlessAuthenticationMgr" scope="singleton" parent="AuthenticationMgr" >
        <property name="sessionless" value="true"/>
    </bean>

    <bean id="DataDictionaryService" scope="singleton" class="it.reply.filenet.services.DataDictionaryService" init-method="init">
        <property name="globalPropertiesService" ref="GlobalPropertiesService" />
    </bean>

    <bean id="AuthenticationMgr" scope="session" class="it.reply.client.filenet.authentication.ClientAuthenticationMgr" init-method="init" >
        <property name="globalPropertiesService" ref="GlobalPropertiesService" />
        <property name="userService" ref="UserService" />
        <property name="objectStoreName" value="${login.objectStore.name}" />
        <property name="objectStoreUri" value="${login.objectStore.uri}" />
        <property name="contentEngineTransport" value="${login.objectStore.transport}" />
    </bean>

</beans>

即使AuthenticationMgrinit()也被调用,并且它在两个版本的Spring中都正确结束,因为我可以记录它。
我知道这个问题很复杂,我的问题可能缺乏一些信息,但请,如果你需要一些关于我的项目的其他信息,帮助将非常感谢。

zdwk9cvp

zdwk9cvp1#

SessionlessAuthenticationMgr(scope=“singleton”)有一个父AuthenticationMgr(scope=“session”)。
我怀疑AuthenticationMgr的作用域也应该是singleton而不是session
也许老jdk在这方面不那么严格?

fwzugrvs

fwzugrvs2#

我猜问题出在 AuthenticationMgr scope session上。您已经将Spring版本从3.1更新到了3.2。Spring 3.2.0 release notes说:
现在可以从父bean定义继承子bean定义的scope属性
您的 SessionlessAuthenticationMgr 父节点是具有会话作用域的 AuthenticationMgr。它仍然没有回答为什么getBean返回null的问题,但您绝对应该检查这里发生了什么

d6kp6zgx

d6kp6zgx3#

我已经能够解决我的问题。基本上,我的应用程序有很多Spring上下文:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/applicationContext*.xml
    </param-value>
</context-param>

没有开始的DataDictionaryService是这样定义的(正如我在问题中所说的):

<bean id="DataDictionaryService" scope="singleton" class="it.reply.filenet.services.DataDictionaryService" init-method="init">
        <property name="globalPropertiesService" ref="GlobalPropertiesService" />
</bean>

这取决于SpringApplicationContext(用于初始化上下文),它的定义(在另一个xml中)如下:

<bean id="SpringApplicationContext" class="it.reply.bean.SpringApplicationContext" scope="singleton"/>

由于某些原因,在Java 7上,SpringApplicationContext首先启动。在Java 8上,这不会发生。几乎可以肯定,Spring classloader在不同的JRE之间具有不同的逻辑,关于隐式Bean的分类法。
我通过指定depends-on属性来解决:

<bean id="DataDictionaryService" scope="singleton" class="it.reply.filenet.services.DataDictionaryService" init-method="init" depends-on="SpringApplicationContext">
    <property name="globalPropertiesService" ref="GlobalPropertiesService" />
</bean>

我还可以保留Spring的3.1.x版本。
非常感谢那些试图帮助我的人!

相关问题