Struts/Spring WebSphere jndi 错误

Posted

技术标签:

【中文标题】Struts/Spring WebSphere jndi 错误【英文标题】:Struts/Spring WebSphere jndi error 【发布时间】:2015-05-14 17:12:25 【问题描述】:

我对 WebSphere 还很陌生(主要使用 Tomcat),并且正在尝试托管一个 Struts/Spring/Hibernate 网站。该网站应该能够从 Oracle 数据库中查找数据并显示该信息。

我目前无法让网站运行,这似乎是一个 jndi 错误。这是错误:

[ERROR   ] Context initialization failed
Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is javax.naming.NamingException: CWNEN1001E: The object referenced by the java:comp/env/jdbc/test JNDI name could not be instantiated. If the reference name maps to a JNDI name in the deployment descriptor bindings for the application performing the JNDI lookup, make sure that the JNDI name mapping in the deployment descriptor binding is correct. If the JNDI name mapping is correct, make sure the target resource can be resolved with the specified name relative to the default initial context. [Root exception is com.ibm.wsspi.injectionengine.InjectionException: CWNEN0030E: The server was unable to obtain an object instance for the java:comp/env/jdbc/test reference.  The exception message was: CWNEN1003E: The server was unable to find the jdbc/test binding with the javax.sql.DataSource type for the java:comp/env/jdbc/test reference.]
[ERROR   ] SRVE0283E: Exception caught while initializing context: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is javax.naming.NamingException: CWNEN1001E: The object referenced by the java:comp/env/jdbc/test JNDI name could not be instantiated. If the reference name maps to a JNDI name in the deployment descriptor bindings for the application performing the JNDI lookup, make sure that the JNDI name mapping in the deployment descriptor binding is correct. If the JNDI name mapping is correct, make sure the target resource can be resolved with the specified name relative to the default initial context. [Root exception is com.ibm.wsspi.injectionengine.InjectionException: CWNEN0030E: The server was unable to obtain an object instance for the java:comp/env/jdbc/test reference.  The exception message was: CWNEN1003E: The server was unable to find the jdbc/test binding with the javax.sql.DataSource type for the java:comp/env/jdbc/test reference.]
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:336)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1456)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1197)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:973)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:750)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at com.ibm.ws.webcontainer.webapp.WebApp.notifyServletContextCreated(WebApp.java:2374)
    at [internal classes]
Caused by: javax.naming.NamingException: CWNEN1001E: The object referenced by the java:comp/env/jdbc/test JNDI name could not be instantiated. If the reference name maps to a JNDI name in the deployment descriptor bindings for the application performing the JNDI lookup, make sure that the JNDI name mapping in the deployment descriptor binding is correct. If the JNDI name mapping is correct, make sure the target resource can be resolved with the specified name relative to the default initial context. [Root exception is com.ibm.wsspi.injectionengine.InjectionException: CWNEN0030E: The server was unable to obtain an object instance for the java:comp/env/jdbc/test reference.  The exception message was: CWNEN1003E: The server was unable to find the jdbc/test binding with the javax.sql.DataSource type for the java:comp/env/jdbc/test reference.]
    at com.ibm.ws.injectionengine.osgi.internal.naming.InjectionJavaColonHelper.newCannotInstantiateObjectException(InjectionJavaColonHelper.java:134)
    at [internal classes]
    at javax.naming.InitialContext.lookup(Unknown Source)
    at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:155)
    at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87)
    at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152)
    at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:179)
    at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95)
    at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:106)
    at org.springframework.jndi.JndiObjectTargetSource.afterPropertiesSet(JndiObjectTargetSource.java:97)
    at org.springframework.jndi.JndiObjectFactoryBean$JndiObjectProxyFactory.createJndiObjectProxy(JndiObjectFactoryBean.java:318)
    at org.springframework.jndi.JndiObjectFactoryBean$JndiObjectProxyFactory.access$000(JndiObjectFactoryBean.java:307)
    at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:200)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
    ... 24 more
Caused by: com.ibm.wsspi.injectionengine.InjectionException: CWNEN0030E: The server was unable to obtain an object instance for the java:comp/env/jdbc/test reference.  The exception message was: CWNEN1003E: The server was unable to find the jdbc/test binding with the javax.sql.DataSource type for the java:comp/env/jdbc/test reference.
    at com.ibm.wsspi.injectionengine.InjectionBinding.getInjectionObject(InjectionBinding.java:1087)
    ... 38 more
Caused by: com.ibm.wsspi.injectionengine.InjectionException: CWNEN1003E: The server was unable to find the jdbc/test binding with the javax.sql.DataSource type for the java:comp/env/jdbc/test reference.
    at com.ibm.ws.injectionengine.osgi.internal.IndirectJndiLookupObjectFactory.getObjectInstance(IndirectJndiLookupObjectFactory.java:201)
    ... 38 more

这是我的 WebSphere server.xml

<server description="new server">   
    <featureManager continueOnError="false">
        <feature>localConnector-1.0</feature>
        <feature>servlet-3.0</feature>
        <feature>jsp-2.2</feature>
        <feature>jaxrs-1.1</feature>
        <feature>jndi-1.0</feature>
    </featureManager>
    <applicationMonitor updateTrigger="disabled"/>
    <application id="proxy" location="proxy.war" name="proxy" type="war">
    </application>
    <httpEndpoint id="defaultHttpEndpoint"
                  host="localhost"
                  httpPort="9080"
                  httpsPort="9443" />

    <dataSource id="oracle" jndiName="jdbc/test" type="javax.sql.DataSource">
        <jdbcDriver libraryRef="oracle-lib" id="oracle-driver"/>
        <connectionManager numConnectionsPerThreadLocal="10" id="ConnectionManager" minPoolSize="1"/>
        <properties.oracle user="test" password="test" URL="jdbc:oracle:thin:@test.local:1521:STILOG"/>
    </dataSource>

    <library id="oracle-lib">
        <fileset dir="C:/Users/test/workspace/WebAdmin/WebContent/WEB-INF/lib/" includes="ojdbc6.jar"/>
    </library>

    <application context-root="WebAdmin" id="WebAdmin" location="WebAdmin.war" name="WebAdmin" type="war">
        <classloader delegation="parentLast" commonLibraryRef="oracle-lib"/>
    </application>
</server>

applicationContext.xml

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

    <bean id="commonService" class="com.ddvc.service.CommonServiceImpl"/>
    <bean id="mainMenuService" class="com.ddvc.service.MainMenuServiceImpl"/>
    <bean id="storeFilterService" class="com.ddvc.service.StoreFilterServiceImpl"/>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
            <bean
                class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="database" value="ORACLE" />
                <property name="showSql" value="true" />
            </bean>
        </property>
        <property name="persistenceUnitName" value="pu1" />
        <property name="jpaProperties">
            <props>
                <prop key="org.hibernate.envers.store_data_at_delete">true</prop>
            </props>
        </property>
    </bean>

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName">
<!--             value>java:/comp/env/IVRConfig</value -->
<!--             value>IVRConfigCluster</value -->
            <value>java:comp/env/jdbc/test</value>
        </property>
        <property name="lookupOnStartup" 
            value="true"/>
        <property name="cache" 
            value="true"/>
        <property name="proxyInterface" 
            value="javax.sql.DataSource"/>
    </bean>

<!--     <jee:jndi-lookup id="dataSource" -->
<!--        jndi-name="java:comp/env/jdbc/test" -->
<!--        cache="true" -->
<!--        resource-ref="true" -->
<!--        lookup-on-startup="true" -->
<!--        proxy-interface="javax.sql.DataSource"> -->
<!--    </jee:jndi-lookup> -->

<!--    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> -->

<!--    <bean id="pum" -->
<!--         class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager"> -->
<!--         <property name="persistenceXmlLocations"> -->
<!--             <list> -->
<!--                 <value>classpath:META-INF/persistence.xml</value> -->
<!--             </list> -->
<!--         </property> -->
<!--         <property name="defaultDataSource" ref="dataSource"></property> -->
<!--     </bean> -->

<!--    <bean id="emf-p" -->
<!--        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> -->
<!--        <property name="persistenceXmlLocation" value="/WebContent/META-INF/persistence.xml" /> -->
<!--        <property name="persistenceUnitManager" ref="pum"/> -->
<!--        <property name="persistenceUnitName" value="pu1" /> -->
<!--        <property name="loadTimeWeaver"> -->
<!--            <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" /> -->
<!--        </property> -->
<!--    </bean> -->

    <bean id="transactionManager"
        class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="loginAction" scope="prototype" class="com.ddvc.action.LoginAction">
        <constructor-arg ref="commonService" />
    </bean>

    <bean id="userAction" scope="prototype" class="com.ddvc.action.UserAction">
        <constructor-arg ref="commonService" />
    </bean>

    <bean id="mainMenuAction" scope="prototype" class="com.ddvc.action.MainMenuAction">
        <constructor-arg ref="mainMenuService" />
    </bean>  
</beans>

ibm-web-bnd.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-bnd 
    xmlns="http://websphere.ibm.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-bnd_1_0.xsd"
    version="1.0">

    <virtual-host name="default_host" />

    <resource-ref name="jdbc/test" binding-name="jdbc/test"/>
</web-bnd>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <display-name>WebAdmin</display-name>

    <!-- Include this if you are using Hibernate -->
    <filter>
        <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
        <filter-class>
            org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
        </filter-class>
    </filter>

    <filter-mapping>
        <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <resource-ref>
        <description>DataSource</description>
        <res-ref-name>jdbc/test</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
        <mapped-name>jdbc/test</mapped-name>
    </resource-ref>
</web-app>

有人建议尝试&lt;jee:jndi-lookup id="dataSource"&gt;,我把它挂了。但它的行为方式与&lt;bean id="dataSource"&gt; 完全相同。

【问题讨论】:

尝试将&lt;feature&gt;jdbc-4.0&lt;/feature&gt; 添加到你的server.xml,你错过了它。 当我添加 &lt;feature&gt;jdbc-4.0&lt;/feature&gt; I get the following error: [ERROR] CWWKF0001E: 找不到 jdbc-4.0 的功能定义。 你一定是打错字了。您是否在 Eclipse 中安装了 WebSphere Developer Tools?如果是,则使用服务器配置编辑器添加该功能。您应该将其添加到具有其他功能的 featureManager 部分。您也可以从wlp\bin\productInfo featureInfo 运行命令,以查看您在运行时中是否具有该功能。 我目前只是在 Eclipse 中为 WebSphere 使用 Web Preview Server。此服务器上似乎没有任何 featureManager 选项。而且我不确定在哪里运行此服务器的wlp\bin\productInfo 命令。我刚刚直接编辑了C:\Users\user\workspace\.metadata\.plugins\com.ibm.etools.wdt.server.core\tmp0\usr\servers\defaultServer0\server.xml 文件以添加连接信息和功能。 我目前已下载 IBM Installation Manager 并尝试下载安装它所需的 WebService 文件 (www-01.ibm.com/support/docview.wss?uid=swg24034969),但我不确定我下载的文件是否正确。我目前正在下载这两个文件8.5.5-WS-WAS-RP-part1.zip8.5.5-WS-WAS-RP-part2.zip。将尝试提取它们,并希望有我需要的 IBM IM 存储库以正确安装它。 【参考方案1】:

这不是正确的做法。执行以下操作:

    在 Eclipse 中,转到 Help &gt; Eclipse Marketplace 搜索 ibm liberty,您将找到 - WebSphere Application Server Liberty Profile Developer tools for Luna(或您正在使用的其他 Eclipse 版本)。安装工具。 安装后,您将能够创建新服务器 - WebSphere Application server Liberty Profile - 创建它。您尝试使用的 Web 预览适用于静态文件,而不是 Java EE 应用程序。

    在创建服务器的向导中,您可以选择下载服务器运行时,执行此操作。你也可以从 wasdev.net 手动下载:

    Download Liberty profile in Eclipse Download just the Liberty profile runtime

    将您的应用程序添加到服务器。在服务器视图中,展开 Liberty 服务器并双击服务器配置。您将在那里拥有功能管理器,并且您将能够在那里添加 jdbc。

【讨论】:

好的,我有一个 WebSphere Application Server Liberty Profile at localhost 正在运行。我有添加了功能的服务器配置。我添加了数据源。我有网络管理员。但我现在收到此错误:Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Conflicting persistence unit definitions for name 'pu1': file:/C:/Users/user/workspace/WebAdmin/build/classes, file:/C:/Users/user/workspace/WebAdmin/WebContent. 必须与经理指定。在entityManagerFactory 我更改以下内容:&lt;property name="persistenceUnitManager" ref="pum"/&gt; &lt;!-- &lt;property name="persistenceUnitName" value="pu1" /&gt; --&gt; 然后添加经理:&lt;bean id="pum" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager"&gt; &lt;property name="persistenceXmlLocations"&gt; &lt;list&gt; &lt;value&gt;classpath:META-INF/persistence.xml&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;property name="defaultDataSource" ref="dataSource"&gt;&lt;/property&gt; &lt;/bean&gt;

以上是关于Struts/Spring WebSphere jndi 错误的主要内容,如果未能解决你的问题,请参考以下文章

struts2+spring+hibernate web项目参考示例

Struts + Spring + Hibernate 集成

SSH之Spring整合struts2

Struts2和Spring集成

Mybatis+struts2+spring整合

SSH(Struts+spring+hibernate)配置