0

我有一个名为 com.sample.servlets.CreateReleaseServlet 的 servlet,我正在尝试在辅助方法中添加 spring AOP 建议。

我在 spring 配置文件中有以下代码:

<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>

当我尝试使用以下 Java 代码将此 servlet 作为 bean 获取时:

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

String next = servlet.execute(req);

我得到以下异常:

 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)

任何建议都会有很大帮助

4

1 回答 1

1

除非您启用了类的代理(而不是接口的普通 JdkProxy),否则这将不起作用,因为代理对象不会充当您的 servlet 的子类。您可能可以将其转换为一个实例Servlet。也就是说,你的方法确实有点奇怪。你的拦截器是做什么的?用 servlet 做 AOP(ish) 的常用方法是创建一个 servlet 过滤器。如果这不是一个选项,我建议您将 servlet 转换为 Spring MVC 控制器并使用常规 Spring MVC 选项来配置拦截器。

于 2013-04-04T07:18:17.100 回答