No Spring Bean found in EJB with SpringBeanAutowiringInterceptor

时间:2016-06-06 14:18:17

标签: spring ejb autowired interceptor

I need some help: I have one EAR-File, containing one WAR-File, one EJB-Jar-File and some "shared" libs:

vmax

The War File has a Context initializer which find the spring config and loads everything well. I now want to use another Spring Context for the EJB Jar. My EJB is defined as

aopalliance-1.0.jar commons-logging-1.1.1.jar log4j-1.2.16.jar spring-aop-4.0.5.RELEASE.jar spring-beans-4.0.5.RELEASE.jar spring-context-4.0.5.RELEASE.jar spring-context-support-4.0.5.RELEASE.jar spring-core-4.0.5.RELEASE.jar spring-expression-4.0.5.RELEASE.jar

Inside the EJB-JAR, there is also a beanRefContext.xml:

@Stateless(mappedName = "ejb/SpringRocks")
@RemoteHome(com.ibm.websphere.ola.ExecuteHome.class)
@Interceptors(SpringBeanAutowiringInterceptor.class)
public class WolaUseCaseOne {
  @Autowired
  private DummyService dummyService;
  /* ...More stuff here */

The simpleEjb.xml is is also inside the EJB-Jar and is defining a very simple Bean:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd">
  <bean id="myEjb" name="myEjb" class="org.springframework.context.support.ClassPathXmlApplicationContext">
   <constructor-arg value="classpath*:META-INF/spring/simpleEjb.xml" />
  </bean>
</beans>

As described, the WEB Part works perfect, but when the EJB is called, the SpringBeanAutowiringInterceptor is called, but seems to do nothing. What do I have to do, to get a Spring Context created?! My hope was that it is initialized when the EJB is created. I created a Subclass of SpringBeanAutowiringInterceptor with some loggers, but the class is only created, no method is called ! What else do I have to do? Or does anybody have a valid EAR File example? I think the Problem is that inside the EJB Module no context is initialized...

Greets Timo

1 个答案:

答案 0 :(得分:1)

我的EJB遇到了类似的问题(没有WAR)。这是固定我的,

  1. 我错过了班级路径上的弹簧罐。我知道你在那里做得很好。
  2. 在我的ejb-jar.xml文件中,我将元数据标志设置为true,因此在部署完成时没有提示我。

  3. 我为一个部署设置为“false”,以查看IBM为我生成的内容。在ejb-jar.xml中,它添加了以下内容(我的MDB名为TaskMDB),

    <assembly-descriptor>
            <interceptor-binding>
                <ejb-name>TaskMDB</ejb-name>
                <interceptor-class>org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor</interceptor-class>
            </interceptor-binding>
        </assembly-descriptor>
    
        <interceptors>
            <interceptor>
                <interceptor-class>org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor</interceptor-class>
                <post-activate>
                    <lifecycle-callback-class>org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor</lifecycle-callback-class>
                    <lifecycle-callback-method>autowireBean</lifecycle-callback-method>
                </post-activate>
                <pre-passivate>
                    <lifecycle-callback-class>org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor</lifecycle-callback-class>
                    <lifecycle-callback-method>releaseBean</lifecycle-callback-method>
                </pre-passivate>
                <post-construct>
                    <lifecycle-callback-class>org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor</lifecycle-callback-class>
                    <lifecycle-callback-method>autowireBean</lifecycle-callback-method>
                </post-construct>
                <pre-destroy>
                    <lifecycle-callback-class>org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor</lifecycle-callback-class>
                    <lifecycle-callback-method>releaseBean</lifecycle-callback-method>
                </pre-destroy>
            </interceptor>
        </interceptors>
    

    然后我将IBM生成的内容(程序集描述符和拦截器节)添加回我的ejb-jar.xml并将metadata-complete设置回true。

    然后它奏效了。希望这会有所帮助。

    这是完整的ejb-jar.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <ejb-jar version="3.1" 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/ejb-jar_3_1.xsd">
        <display-name>ares-api-uow-ejb</display-name>
        <enterprise-beans>
            <message-driven id="TaskMDB">
                <ejb-name>TaskMDB</ejb-name>
                <ejb-class>something.api.uow.ejb.mdb.TaskMDB</ejb-class>
                <messaging-type>javax.jms.MessageListener</messaging-type>
                <transaction-type>Bean</transaction-type>
            </message-driven>
        </enterprise-beans>
    </ejb-jar>