如果我在EJB

时间:2016-03-21 12:08:16

标签: java spring ejb-3.0 autowired spring-3

我在使用SpringBeanAutowiringInterceptor时遇到异常调用EJB(我使用的是JDK 1.7.0_51和Weblogic12)。

像往常一样,当我得到这样的异常时,我尝试创建一个示例应用程序,以查看我的应用程序中的任何现有代码是否可能存在冲突的库。只是为了将它与我的主应用程序隔离开来

所以我走了:

我将EJB定义为(不是我评论@Interceptors(SpringBeanAutowiringInterceptor.class)

package my.company.impl;

import my.company.client.remote.CalculatorRemote;
import org.springframework.stereotype.Component;

import javax.ejb.Remote;
import javax.ejb.Stateless;


@Stateless(mappedName = "ejb/MyCalcuatorEJB")
@Remote(CalculatorRemote.class)
//@Interceptors(SpringBeanAutowiringInterceptor.class)
@Component
public class CalculatorEJB implements CalculatorRemote {

    private static final long serialVersionUID = 1L;

    @Override
    public int add(final int a, final int b) {
        return a+b;
    }
}

和一个maven POM

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>my.company</groupId>
    <artifactId>my-test-ejb-service</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>ejb</packaging>
    <properties>
        <cobertura-maven-plugin-ver>2.6</cobertura-maven-plugin-ver>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ejb-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <ejbVersion>3.2</ejbVersion>
                    <generateClient>true</generateClient>
                    <!-- Ensure the a thin EJB-client. -->
                    <clientIncludes>
                        <clientInclude>my/company/client/remote/**</clientInclude>

                    </clientIncludes>

                </configuration>
                <executions>
                    <execution>
                        <id>make-an-ejb</id>
                        <phase>package</phase>
                        <goals>
                            <goal>ejb</goal>
                        </goals>
                        <configuration>
                            <classifier>ejb</classifier>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- This needs to be removed in real life scenario-->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>${cobertura-maven-plugin-ver}</version>
                <configuration>
                    <check>
                        <haltOnFailure>true</haltOnFailure>
                        <totalLineRate>0</totalLineRate>
                    </check>
                </configuration>
            </plugin>

        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>javax.ejb</groupId>
            <artifactId>ejb</artifactId>
            <version>3.0</version>
            <scope>provided</scope>
        </dependency>
        <!--No interceptor annotation add yet this will be added later-->
       <!-- <dependency>
            <groupId>javax.interceptor</groupId>
            <artifactId>javax.interceptor-api</artifactId>
            <scope>provided</scope>
            <version>1.2</version>
        </dependency>-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
    </dependencies>

</project>

我将耳朵部署到weblogic,从IntelliJ我有一个测试类,如下所示

import my.company.client.remote.CalculatorRemote;

import javax.naming.Context;
import javax.naming.NamingException;
import java.util.Hashtable;

public class TestClient {

    public static void main(String[] args) {
        try {
            testUsingJavaSe();
        } catch (final Exception ex) {
            ex.printStackTrace();
        }
    }

    private static void testUsingJavaSe() throws NamingException {
        Hashtable<String, String> env = new Hashtable<String, String>();
        env.put("java.naming.provider.url", "t3://127.0.0.1:7002");
        System.out.println("creating context");
        Context ic = new weblogic.jndi.WLInitialContextFactory().getInitialContext(env);
        System.out.println("looking up object");
        Object object = ic.lookup("ejb/MyCalcuatorEJB#my.company.client.remote.CalculatorRemote");

        CalculatorRemote remote = (CalculatorRemote) object;
        System.out.println(remote);

        System.out.println("invoking ejb");
        int result = remote.add(1, 2);

        System.out.println("result: " + result);
    }

}

现在所有这些都有效,当我运行测试类时,我可以看到3被打印到控制台窗口。

在我的应用程序中,我需要在EJB中使用额外的bean。由于我使用的是春天,我想使用@Autowired。根据我的理解,我需要删除@Component注释并将其替换为@Interceptors(SpringBeanAutowiringInterceptor.class)

但是为了使用它我在我的POM中取消注释以下依赖

 <dependency>
            <groupId>javax.interceptor</groupId>
            <artifactId>javax.interceptor-api</artifactId>
            <scope>provided</scope>
            <version>1.2</version>
        </dependency>

这允许我导入@Interceptors(SpringBeanAutowiringInterceptor.class)

这将允许我启动可以扫描类和注入bean的Spring Context。

我还有一个beanRefContext.xml,用于启动我的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: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/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="calculatorService" class="org.springframework.context.support.ClassPathXmlApplicationContext">
        <constructor-arg value="classpath*:applicationContext.xml"/>
    </bean>
</beans>

指向空的applicaitonContext.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:sws="http://www.springframework.org/schema/web-services"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                            http://www.springframework.org/schema/web-services
                            http://www.springframework.org/schema/web-services/web-services-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
                            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">


</beans>

我还没有任何自动装配...只是想测试接线和依赖性是否有效。

但如果我再次运行它,我会收到以下错误

> "C:\Program Files\Java\jdk1.7.0_51\bin\java" -Didea.launcher.port=7539
> "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ
> IDEA 14.0.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program
> Files\Java\jdk1.7.0_51\jre\lib\charsets.jar;C:\Program
> Files\Java\jdk1.7.0_51\jre\lib\deploy.jar;C:\Program
> Files\Java\jdk1.7.0_51\jre\lib\javaws.jar;C:\Program
> Files\Java\jdk1.7.0_51\jre\lib\jce.jar;C:\Program
> Files\Java\jdk1.7.0_51\jre\lib\jfr.jar;C:\Program
> Files\Java\jdk1.7.0_51\jre\lib\jfxrt.jar;C:\Program
> Files\Java\jdk1.7.0_51\jre\lib\jsse.jar;C:\Program
> Files\Java\jdk1.7.0_51\jre\lib\management-agent.jar;C:\Program
> Files\Java\jdk1.7.0_51\jre\lib\plugin.jar;C:\Program
> Files\Java\jdk1.7.0_51\jre\lib\resources.jar;C:\Program
> Files\Java\jdk1.7.0_51\jre\lib\rt.jar;C:\Program
> Files\Java\jdk1.7.0_51\jre\lib\ext\access-bridge-64.jar;C:\Program
> Files\Java\jdk1.7.0_51\jre\lib\ext\dnsns.jar;C:\Program
> Files\Java\jdk1.7.0_51\jre\lib\ext\jaccess.jar;C:\Program
> Files\Java\jdk1.7.0_51\jre\lib\ext\localedata.jar;C:\Program
> Files\Java\jdk1.7.0_51\jre\lib\ext\sunec.jar;C:\Program
> Files\Java\jdk1.7.0_51\jre\lib\ext\sunjce_provider.jar;C:\Program
> Files\Java\jdk1.7.0_51\jre\lib\ext\sunmscapi.jar;C:\Program
> Files\Java\jdk1.7.0_51\jre\lib\ext\zipfs.jar;D:\git\devrepos\dcs\dcs-commons\my-test-ejb-service\testclient\target\classes;D:\git\devrepos\dcs\dcs-commons\my-test-ejb-service\core\target\classes;D:\Development\Maven\Maven_Repo_Local_DCS\org\springframework\spring-core\4.0.5.RELEASE\spring-core-4.0.5.RELEASE.jar;D:\Development\Maven\Maven_Repo_Local_DCS\commons-logging\commons-logging\1.1.3\commons-logging-1.1.3.jar;D:\Development\Maven\Maven_Repo_Local_DCS\org\springframework\spring-context\4.0.5.RELEASE\spring-context-4.0.5.RELEASE.jar;D:\Development\Maven\Maven_Repo_Local_DCS\org\springframework\spring-aop\4.0.5.RELEASE\spring-aop-4.0.5.RELEASE.jar;D:\Development\Maven\Maven_Repo_Local_DCS\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;D:\Development\Maven\Maven_Repo_Local_DCS\org\springframework\spring-beans\4.0.5.RELEASE\spring-beans-4.0.5.RELEASE.jar;D:\Development\Maven\Maven_Repo_Local_DCS\org\springframework\spring-expression\4.0.5.RELEASE\spring-expression-4.0.5.RELEASE.jar;D:\Development\Maven\Maven_Repo_Local_DCS\org\springframework\spring-context-support\4.0.5.RELEASE\spring-context-support-4.0.5.RELEASE.jar;D:\Development\Maven\Maven_Repo_Local_DCS\javax\ejb\ejb\3.0\ejb-3.0.jar;D:\Development\Maven\Maven_Repo_Local_DCS\com\oracle\middleware\wlthint3client\12.1.2.0.0\wlthint3client-12.1.2.0.0.jar;C:\Program
> Files (x86)\JetBrains\IntelliJ IDEA 14.0.1\lib\idea_rt.jar"
> com.intellij.rt.execution.application.AppMain TestClient creating
> context looking up object
> ClusterableRemoteRef(-4386785499045922938S:127.0.0.1:[7002,7002,-1,-1,-1,-1,-1]:jDomain:Server7002
> [-4386785499045922938S:127.0.0.1:[7002,7002,-1,-1,-1,-1,-1]:jDomain:Server7002/307])/307
> invoking ejb javax.ejb.EJBException: Problem finding error class;
> nested exception is:      java.lang.ClassNotFoundException: Failed to
> load class com.oracle.pitchfork.interfaces.LifecycleCallbackException;
> nested exception is: java.lang.ClassNotFoundException: Failed to load
> class com.oracle.pitchfork.interfaces.LifecycleCallbackException
> java.lang.ClassNotFoundException: Failed to load class
> com.oracle.pitchfork.interfaces.LifecycleCallbackException    at
> weblogic.rmi.utils.WLRMIClassLoaderDelegate.loadClass(WLRMIClassLoaderDelegate.java:208)
>   at
> weblogic.rmi.utils.WLRMIClassLoaderDelegate.loadClass(WLRMIClassLoaderDelegate.java:135)
>   at weblogic.rmi.utils.Utilities.loadClass(Utilities.java:306)   at
> weblogic.rjvm.MsgAbbrevInputStream.resolveClass(MsgAbbrevInputStream.java:439)
>   at
> weblogic.utils.io.ChunkedObjectInputStream$NestedObjectInputStream.resolveClass(ChunkedObjectInputStream.java:268)
>   at
> java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1612)
>   at
> java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1517)
>   at
> java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
>   at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
>   at
> java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1990)
>   at
> java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1915)
>   at
> java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1798)
>   at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
>   at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
>   at
> weblogic.utils.io.ChunkedObjectInputStream.readObject(ChunkedObjectInputStream.java:208)
>   at
> weblogic.rjvm.MsgAbbrevInputStream.readObject(MsgAbbrevInputStream.java:602)
>   at weblogic.rjvm.ResponseImpl.getThrowable(ResponseImpl.java:200)   at
> weblogic.rjvm.ResponseImpl.unmarshalReturn(ResponseImpl.java:248)     at
> weblogic.rmi.cluster.ClusterableRemoteRef.invoke(ClusterableRemoteRef.java:466)
>   at
> weblogic.rmi.cluster.ClusterableRemoteRef.invoke(ClusterableRemoteRef.java:274)
>   at
> my.company.impl.CalculatorEJB_wo0f1c_CalculatorRemoteImpl_12120_WLStub.add(Unknown
> Source)   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native
> Method)   at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
>   at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>   at java.lang.reflect.Method.invoke(Method.java:606)     at
> weblogic.ejb.container.internal.RemoteBusinessIntfProxy.invoke(RemoteBusinessIntfProxy.java:84)
>   at com.sun.proxy.$Proxy0.add(Unknown Source)    at
> TestClient.testUsingJavaSe(TestClient.java:29)    at
> TestClient.main(TestClient.java:11)   at
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)   at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
>   at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>   at java.lang.reflect.Method.invoke(Method.java:606)     at
> com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
> javax.ejb.EJBException: Problem finding error class; nested exception
> is:   java.lang.ClassNotFoundException: Failed to load class
> com.oracle.pitchfork.interfaces.LifecycleCallbackException; nested
> exception is: java.lang.ClassNotFoundException: Failed to load class
> com.oracle.pitchfork.interfaces.LifecycleCallbackException    at
> weblogic.ejb.container.internal.RemoteBusinessIntfProxy.unwrapRemoteException(RemoteBusinessIntfProxy.java:117)
>   at
> weblogic.ejb.container.internal.RemoteBusinessIntfProxy.invoke(RemoteBusinessIntfProxy.java:92)
>   at com.sun.proxy.$Proxy0.add(Unknown Source)    at
> TestClient.testUsingJavaSe(TestClient.java:29)    at
> TestClient.main(TestClient.java:11)   at
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)   at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
>   at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>   at java.lang.reflect.Method.invoke(Method.java:606)     at
> com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
> 
> Process finished with exit code 0

我不确定我在这里做错了什么。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

不确定正确的解释是什么,但我最终通过改变两件事来实现它:

  1. 我需要将@Interceptors(SpringBeanAutowiringInterceptor.class)添加到我的班级,而不是使用@Component将其填满,我需要两者。所以我的EJB类最终看起来像:

    package my.company.impl;
    
    import my.company.client.remote.CalculatorRemote;
    import org.springframework.stereotype.Component;
    
    import javax.ejb.Remote;
    import javax.ejb.Stateless;
    
    
    @Stateless(mappedName = "ejb/MyCalcuatorEJB")
    @Remote(CalculatorRemote.class)
    @Interceptors(SpringBeanAutowiringInterceptor.class)
    @Component
    public class CalculatorEJB implements CalculatorRemote {
    
    private static final long serialVersionUID = 1L;
    
    @Override
    public int add(final int a, final int b) {
        return a+b;
    }
    

    }

  2. 我的beanRefContext.xml位于src/main/resources/my/company包中,我移至src/main/resources,现在它位于资源文件夹的根目录中。猜猜SpringBeanAutowiringInterceptor只扫描根。

  3. 如果任何人能够解释为什么这样取悦它可能会帮助另一个人。