Spring JNDI Lookup - 无法将com.sun.proxy。$ Proxy转换为EJB类

时间:2014-09-25 10:53:52

标签: spring-mvc jboss ejb jndi wildfly

当我尝试从另一个模块查找EJB时,jboss将返回此异常:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.ClassCastException: com.sun.proxy.$Proxy125 cannot be cast to com.foo.spring.logging.UserCategoryLogService
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:927)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:822)
javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:130)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:139)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter.doFilter(DefaultLoginPageGeneratingFilter.java:91)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:125)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60)
io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132)
io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.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:25)
io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:113)
io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45)
io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:61)
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:25)
org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:240)
io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:227)
io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:73)
io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:146)
io.undertow.server.Connectors.executeRootHandler(Connectors.java:177)
io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:727)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
java.lang.Thread.run(Thread.java:722)

到目前为止看起来查找工作正常,但我根本无法强制转换Object。 我的项目是一个Spring-MVC项目。后端,我连接以接收UserCategoryLogService,是一个简单的Java-EE项目。

我尝试了不同的查找和转换方法。通常我会这样:

try {

        UserCategoryLogService logService = (UserCategoryLogService) new InitialContext.lookup("java:jboss/exported/backend/UserCategoryLogServiceImpl!logging.UserCategoryLogService");
        logService.logLogin(username);
    } catch (NamingException e) {
        e.printStackTrace();
    }

当我在后端项目中进行本地ejb查找时,通常这是有效的。 我还尝试创建一个Property并在创建新Context时使用它。它将返回相同的错误。

我在我的spring-config.xml中添加了UserCategoryLogService接口,但它没有帮助。

我在春天没有关于JNDI的额外配置。 我试图查找@Local和@Remote,两者都会返回相同的castingException。

我使用的是Wildfly 8.1.0

您有任何想法如何处理代理对象?为什么ejb-lookup会与我的java-ee后端一起工作,而不是我的spring-mvc项目呢?

春季项目的界面:

import javax.ejb.Remote;

@Remote
public interface UserCategoryLogService {
    void logUserCatCall(String userId, Integer categoryId);
    void logLogin(String userId);

}

干杯

编辑27.09.2014:

我发现了这个问题: Injecting EJB 3 into Spring Bean

所以我尝试自动装配bean,我得到了同样的错误:

Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'java:jboss/exported/backend/UserCategoryLogServiceImpl!logging.UserCategoryLogService' must be of type [com.frontend.spring.logging.UserCategoryLogService], but was actually of type [com.sun.proxy.$Proxy105]

EDIT2:

 @Bean
    UserCategoryLogService getUserCatLogService() {
        UserCategoryLogService ucLog = null;
        JndiTemplate template = new JndiTemplate();

        try {
            ucLog = (UserCategoryLogService) template.lookup("java:jboss/exported/backend/UserCategoryLogServiceImpl!logging.UserCategoryLogService");
        } catch (NamingException e) {
            e.printStackTrace();
        }


        return ucLog;
    }

也不会工作:

 threw exception; nested exception is java.lang.ClassCastException: com.sun.proxy.$Proxy125 cannot be cast to com.recc.frontend.logging.UserCategoryLogService

1 个答案:

答案 0 :(得分:2)

我刚想通了,如何解决这个问题,但我不确定这是不是最好的方式。

您可以使用 SimpleRemoteStatelessSessionProxyFactoryBean 在Spring中查找远程EJBS。

  SimpleRemoteStatelessSessionProxyFactoryBean fb = new SimpleRemoteStatelessSessionProxyFactoryBean();
            fb.setJndiName(UserCategoryLogService.JNDI_NAME);
            fb.setResourceRef(true);
            fb.setBusinessInterface(UserCategoryLogService.class);
            // Need lifecycle methods
            fb.afterPropertiesSet();
            fb.setJndiTemplate(new JndiTemplate());
            UserCategoryLogService logservice = (UserCategoryLogService) fb.getObject();

这将返回正确的bean,也不会需要任何额外的注释或xml配置。

赏金仍然有效。如果您发布更好的工作方式,您将获得+50声望。

编辑:我也在寻找有状态EJB的实现,根据这个名称,这只适用于无状态ejbs。