0

我的耳朵只包含 2 个模块:一个包含 Hibernate 逻辑的 jar(名为 CorbeilleGestionHibernate.jar)和一个 EJB jar(名为 CorbeilleGestionEJBImpl.jar)。

<?xml version="1.0" encoding="UTF-8"?>
<application 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/application_6.xsd" version="6">
  <display-name>CorbeilleGestionEAR</display-name>
  <initialize-in-order>true</initialize-in-order>
  <module>
    <java>./CorbeilleGestionHibernate.jar</java>
  </module>
  <module>
    <ejb>CorbeilleGestionEJBImpl.jar</ejb>
  </module>
  <library-directory>lib</library-directory>
</application>

我的问题是在 EJB jar 中我可以通过 JNDI 访问 Hibernate jar 中的对象。

在独立模式下启动 JBoss 时,这在本地非常有用。但是当我在域模式下启动 JBoss 时,出现以下错误:

08:56:43,181 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-2) MSC00001: Failed to start service jboss.deployment.subunit."CorbeilleGestionEAR.ear"."CorbeilleGestionEJBImpl.jar".component.StartupBean.START: org.jboss.msc.service.StartException in service jboss.deployment.subunit."CorbeilleGestionEAR.ear"."CorbeilleGestionEJBImpl.jar".component.StartupBean.START: Failed to start service
        at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1767) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [rt.jar:1.6.0_30]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [rt.jar:1.6.0_30]
        at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_30]
Caused by: java.lang.IllegalStateException: JBAS011048: Failed to construct component instance
        at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:163)
        at org.jboss.as.ee.component.BasicComponent.createInstance(BasicComponent.java:85)
        at org.jboss.as.ejb3.component.singleton.SingletonComponent.getComponentInstance(SingletonComponent.java:116)
        at org.jboss.as.ejb3.component.singleton.SingletonComponent.start(SingletonComponent.java:130)
        at org.jboss.as.ee.component.ComponentStartService.start(ComponentStartService.java:44)
        at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
        at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
        ... 3 more
Caused by: javax.ejb.EJBException: java.lang.IllegalStateException: javax.naming.NameNotFoundException: CorbeilleGestionHibernate -- service jboss.naming.context.java.jboss.CorbeilleGestionHibernate
        at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:166)
        at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:230)
        at org.jboss.as.ejb3.tx.CMTTxInterceptor.requiresNew(CMTTxInterceptor.java:333)
        at org.jboss.as.ejb3.tx.SingletonLifecycleCMTTxInterceptor.processInvocation(SingletonLifecycleCMTTxInterceptor.java:56)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]
        at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]
        at org.jboss.as.ee.component.TCCLInterceptor.processInvocation(TCCLInterceptor.java:45)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]
        at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]
        at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:161)
        ... 9 more
Caused by: java.lang.IllegalStateException: javax.naming.NameNotFoundException: CorbeilleGestionHibernate -- service jboss.naming.context.java.jboss.CorbeilleGestionHibernate
        at org.jboss.spring.support.SpringInjectionSupport.lookup(SpringInjectionSupport.java:180)
        at org.jboss.spring.support.SpringInjectionSupport.getObjectFromBeanFactory(SpringInjectionSupport.java:136)
        at org.jboss.spring.support.SpringInjectionSupport.injectToField(SpringInjectionSupport.java:202)
        at org.jboss.spring.support.SpringInjectionSupport.inject(SpringInjectionSupport.java:80)
        at org.jboss.spring.callback.SpringLifecycleInterceptor.postConstruct(SpringLifecycleInterceptor.java:43)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.6.0_30]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [rt.jar:1.6.0_30]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [rt.jar:1.6.0_30]
        at java.lang.reflect.Method.invoke(Method.java:597) [rt.jar:1.6.0_30]
        at org.jboss.as.ee.component.ManagedReferenceLifecycleMethodInterceptorFactory$ManagedReferenceLifecycleMethodInterceptor.processInvocation(ManagedReferenceLifecycleMethodInterceptorFactory.java:118)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]
        at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]
        at org.jboss.as.ee.component.ManagedReferenceFieldInjectionInterceptorFactory$ManagedReferenceFieldInjectionInterceptor.processInvocation(ManagedReferenceFieldInjectionInterceptorFactory.java:112)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]
        at org.jboss.as.ee.component.ManagedReferenceFieldInjectionInterceptorFactory$ManagedReferenceFieldInjectionInterceptor.processInvocation(ManagedReferenceFieldInjectionInterceptorFactory.java:112)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]
        at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]
        at org.jboss.as.ee.component.ManagedReferenceInterceptorFactory$ManagedReferenceInterceptor.processInvocation(ManagedReferenceInterceptorFactory.java:95)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]
        at org.jboss.as.ee.component.ManagedReferenceInterceptorFactory$ManagedReferenceInterceptor.processInvocation(ManagedReferenceInterceptorFactory.java:95)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]
        at org.jboss.as.ee.component.ManagedReferenceInterceptorFactory$ManagedReferenceInterceptor.processInvocation(ManagedReferenceInterceptorFactory.java:95)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]
        at org.jboss.as.ee.component.ManagedReferenceInterceptorFactory$ManagedReferenceInterceptor.processInvocation(ManagedReferenceInterceptorFactory.java:95)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]
        at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]
        at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]
        at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:228)
        ... 18 more
Caused by: javax.naming.NameNotFoundException: CorbeilleGestionHibernate -- service jboss.naming.context.java.jboss.CorbeilleGestionHibernate
        at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:97)
        at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:178)
        at org.jboss.as.naming.InitialContext.lookup(InitialContext.java:123)
        at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:214)
        at javax.naming.InitialContext.lookup(InitialContext.java:392) [rt.jar:1.6.0_30]
        at org.jboss.spring.support.SpringInjectionSupport.lookup(SpringInjectionSupport.java:173)
        ... 49 more

这个错误显然意味着 Hibernate jar 没有在 EJB jar 之前加载。

我尝试通过像这样在 jboss-deployment-structure.xml 中添加依赖项来“强制”部署

<sub-deployment name="CorbeilleGestionEJBImpl.jar">
    <dependencies>
        <module name="deployment.CorbeilleGestionEAR.ear.CorbeilleGestionHibernate.jar"  />
        <module name="org.jboss.as.webservices.server.integration" />
    </dependencies>
</sub-deployment>

但这似乎并没有改变任何东西。

我做了一些搜索,在我看来,Java 模块不尊重“按顺序初始化”属性。我错了吗 ?

如果我错了,我不明白为什么在我的 EJB jar 之前没有加载 Hibernate jar。

如果我没记错的话,在我的 EJB jar 之前加载我的 Hibernate jar 的最佳解决方案是什么。

为了完成这个问题,从 JNDI 加载的对象是在 Spring 上下文中定义的对象。这个上下文在这个 jboss-spring.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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <!-- Ceci permet de définir le nom JNDI -->
    <description>BeanFactory=(CorbeilleGestionHibernate)</description>

    <!-- Chargement des properties -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>file:${jboss.server.config.dir}/corbeille/corbeille.properties</value>
            </list>
        </property>
    </bean>

    <!-- Pour pouvoir récupérer l'application context facilement -->
    <bean id="hibernateApplicationContextHolder" class="com.sham.corbeille.util.SpringApplicationContextHolder" />

    <import resource="*-config.xml"/>
</beans>

您可以看到我们加载了所有“*-config.xml”,即两个文件:db-config.xml 和 dao-config.xml。db-config.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:tx="http://www.springframework.org/schema/tx"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/jee
        http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
        <property name="persistenceXmlLocation" value="classpath:META-INF/jpa-persistence.xml" />
    </bean>

    <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="transactionManagerName" value="java:jboss/TransactionManager" />
        <property name="userTransactionName" value="java:jboss/UserTransaction" />
        <property name="transactionSynchronizationRegistryName" value="java:jboss/TransactionSynchronizationRegistry" />
    </bean>

    <bean id="exceptionTranslator" class="org.springframework.orm.hibernate4.HibernateExceptionTranslator"/>

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

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

    <!-- Exception translation bean post processor -->
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

</beans>

dao-config.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:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- Configuration par annotations -->
    <context:annotation-config />
    <context:component-scan base-package="com.sham.corbeille.dao" />


    <!-- DAOs Codification -->
    <bean id="elementCodifDAO" class="com.sham.corbeille.dao.codification.ElementCodifDAOImpl" />
    <bean id="sinCodTacheDAO" class="com.sham.corbeille.dao.codification.SinCodTacheDAOImpl" />

    <!-- DAOs Client -->
    <bean id="cliClientDAO" class="com.sham.corbeille.dao.client.CliClientDAOImpl" />

    <!-- DAOs Contrat -->
    <bean id="conContratDAO" class="com.sham.corbeille.dao.contrat.ConContratDAOImpl" />

    <!-- DAOs Flux -->
    <bean id="vueMessagesCorbeilleFluxClientDAO" class="com.sham.corbeille.dao.flux.VueMessagesCorbeilleClientDAOImpl" />
    <bean id="vueMessagesCorbeilleFluxSinistreDAO" class="com.sham.corbeille.dao.flux.VueMessagesCorbeilleSinistreDAOImpl" />
    <bean id="vueMessagesCorbeilleFluxUtilisateurDAO" class="com.sham.corbeille.dao.flux.VueMessagesCorbeilleUtilisateurDAOImpl" />

    <!-- DAOs Habilitations -->
    <bean id="habUtilisateurDAO" class="com.sham.corbeille.dao.habilitations.HabUtilisateurDAOImpl" />
    <bean id="habGroupeDAO" class="com.sham.corbeille.dao.habilitations.HabGroupeDAOImpl" />
    <bean id="habFonctionDAO" class="com.sham.corbeille.dao.habilitations.HabFonctionDAOImpl" />

    <!-- DAOs Historisation -->
    <bean id="sinHistoriseEtatNotifDAO" class="com.sham.corbeille.dao.historisation.SinHistoriseEtatNotifDAOImpl" />
    <bean id="sinHistoriseEtatMessageDAO" class="com.sham.corbeille.dao.historisation.SinHistoriseEtatMessageDAOImpl" />

    <!-- DAOs Notifications -->
    <bean id="sinNotificationsDAO" class="com.sham.corbeille.dao.notifications.SinNotificationsDAOImpl" />
    <bean id="sinMessagesDAO" class="com.sham.corbeille.dao.notifications.SinMessagesDAOImpl" />
    <bean id="notificationDAO" class="com.sham.corbeille.dao.notifications.NotificationDAOImpl" />

    <!-- DAOs Personne -->
    <bean id="cliContactsDAO" class="com.sham.corbeille.dao.personne.CliContactsDAOImpl" />
    <bean id="sinActeurExterneDAO" class="com.sham.corbeille.dao.personne.SinActeurExterneDAOImpl" />

    <!-- DAOs Règlements -->
    <bean id="sinReglementDAO" class="com.sham.corbeille.dao.reglement.SinReglementDAOImpl" />

    <!-- DAOs Sinistres -->
    <bean id="sinSinistreDAO" class="com.sham.corbeille.dao.sinistres.SinSinistreDAOImpl" />
    <bean id="sinProcedureDAO" class="com.sham.corbeille.dao.sinistres.SinProcedureDAOImpl" />
    <bean id="sinVictimeDAO" class="com.sham.corbeille.dao.sinistres.SinVictimeDAOImpl" />

    <!-- DAOs Vues -->
    <bean id="vueMessagesCorbeilleDAO" class="com.sham.corbeille.dao.vues.VueMessagesCorbeilleDAOImpl" />
</beans>

这在 EJB 中用于注入对象。例如,要注入 CliClientDAO 类型的对象,我们使用以下注解

@Spring(bean = "cliClientDAO", jndiName = "CorbeilleGestionHibernate")

这些注入是导致 JNDI 查找失败的原因

如果您需要任何进一步的信息以完全理解问题,请询问我。

4

0 回答 0