简体   繁体   English

Java EE Servlet作为Spring Bean代理

[英]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. 我有一个名为com.sample.servlets.CreateReleaseServlet的servlet,我正在尝试为它的辅助方法中添加spring AOP建议。

I have the following code in spring config file: 我在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>

When I try to get this servlet as bean by using the following Java code: 当我尝试通过使用以下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);

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. 除非您启用了类的代理(而不是接口的纯JdkProxy),否则这将不起作用,因为代理对象将不充当Servlet的子类。 You could probably cast it to an instance of Servlet instead. 您可能可以将其强制转换为Servlet实例。 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. 用servlet处理AOP(ish)的常用方法是创建一个servlet过滤器。 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. 如果不是这样,我建议您将servlet转换为Spring MVC控制器,并使用常规的Spring MVC选项配置拦截器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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