简体   繁体   中英

Java EE servlet as spring bean proxy

I have servlet named com.sample.servlets.CreateReleaseServlet for which I am trying to add spring AOP advice in a helper method.

I have the following code in spring config file:

<bean id="customerService" class="com.sample.servlets.CreateReleaseServlet">
</bean>

<bean id="notificationAdvice" class="com.sample.advice.NotificationAdvice" />

<bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">

    <property name="target" ref="customerService" />

    <property name="interceptorNames">
        <list>
            <value>notificationAdvice</value>
        </list>
    </property>
</bean>

When I try to get this servlet as bean by using the following Java code:

ApplicationContext appContext = new ClassPathXmlApplicationContext(
        new String[] { "/WEB-INF/form-servlet.xml" });
CreateReleaseServlet servlet = (CreateReleaseServlet) appContext.getBean("customerServiceProxy");

String next = servlet.execute(req);

I get the following exception:

 com.ibm.ws.webcontainer.webapp.WebApp logServletError SRVE0293E: [Servlet Error]-[CreateReleaseServlet]: java.lang.ClassCastException: $Proxy103 incompatible with com.sample.servlets.CreateReleaseServlet
        at com.sample.servlets.CreateReleaseServlet.service(CreateReleaseServlet.java:1823)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
        at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1229)
        at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:774)
        at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:456)
        at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
        at com.ibm.ws.webcontainer.filter.WebAppFilterChain.invokeTarget(WebAppFilterChain.java:136)
        at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:97)
        at com.sample.servlets.SampleFilter.doFilter(Unknown Source)
        at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195)
        at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91)
        at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:928)
        at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1025)
        at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3751)
        at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:304)
        at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:962)
        at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1662)
        at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:195)
        at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:452)
        at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:511)
        at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:305)
        at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:83)
        at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
        at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
        at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
        at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
        at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
        at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
        at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
        at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1690)

Any suggestion will be of great help

Unless you have enabled proxying of classes (and not plain JdkProxy of interfaces), this will not work, as the proxy object will not act as a subclass of your servlet. You could probably cast it to an instance of Servlet instead. That said, your approach does seem a little strange. What do your interceptor do? The common way of doing AOP(ish) stuff with servlets is to create a servlet filter. And if that's not an option, I suggest you convert your servlet to a Spring MVC controller and use regular Spring MVC options for configuring interceptors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM