gpt4 book ai didi

java - 如果使用 @Local 注释 (EJB),则会出错

转载 作者:太空宇宙 更新时间:2023-11-04 06:09:45 24 4
gpt4 key购买 nike

GIT:https://github.com/AndreaCatania/libreria

我有一个包含两个模块的ear文件,EJB和WAR。

我有这个 EJB 接口(interface)

@Remote
public interface Library2IFace{
public String getName();
public void setName(String name);
public String getText();
public void setText(String text);
}

一切正常。 WAR 模块正确锁定了 EJB。

当我设置注释 @Local 而不是 @Remote 时,出现此错误:

java.lang.ClassCastException: com.ac.ejbsclient.ejb.Library2IFace$$$view3 cannot be cast to com.ac.ejbsclient.ejb.Library2IFace
com.ac.test.servlet.MyObj.<init>(MyObj.java:15)
com.ac.test.servlet.DeServlet.doGet(DeServlet.java:17)
javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61)
io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45)
io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:63)
io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)
io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70)
io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:261)
io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:247)
io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:76)
io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:166)
io.undertow.server.Connectors.executeRootHandler(Connectors.java:197)
io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:759)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:745)

更新MyObj的代码是:

public class MyObj{

public MyObj(){
try{
lb1 = (LibraryIFace) new InitialContext().lookup("java:global/"+NAME_VERSION_EAR+"/"+NAME_VERSION_EJBMODULE+"/LibraryBean");
lb2 = (Library2IFace) new InitialContext().lookup("java:global/"+NAME_VERSION_EAR+"/"+NAME_VERSION_EJBMODULE+"/Library2Bean");
}catch( NamingException e ){
e.printStackTrace();
}
}

public void setName(String name){
lb1.setName(name);
}

public String getName(){
return lb1.getName();
}

public void setText(String text){
lb1.setText(text);
}

public String getText(){
return lb1.getText();
}

public void setName2(String name){
lb2.setName(name);
}

public String getName2(){
return lb2.getName();
}

public void setText2(String text){
lb2.setText(text);
}

public String getText2(){
return lb2.getText();
}

public void createBook(int bookId, String name){
lb1.createBook(bookId,name);
}

public Book getBook(int bookId){
return lb1.getBook(bookId);
}

private static LibraryIFace lb1;
private static Library2IFace lb2;
private static final String NAME_VERSION_EAR = "libreriaEar";
private static final String NAME_VERSION_EJBMODULE = "libreria-core-1.0-SNAPSHOT";
}

最佳答案

堆栈跟踪中的错误消息表明调用者可以访问 Library2IFace 类的不同副本,很可能位于 war 文件的/WEB-INF/lib 目录中。

您需要确保整个 EAR 中只有该类的一个副本,并且它应该位于 EAR/lib 目录中的 jar 中或所属的 ejb-jar 中。

@Ajan 的建议是管理此问题的常用方法。

关于java - 如果使用 @Local 注释 (EJB),则会出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28885121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com