我对 Spring AOP 真的很陌生。在我的应用程序中,我配置HiddenHttpMethodFilter
了将方法参数转换为 HTTP 方法,并使 Spring 能够处理其他 HTTP 方法,例如 等DELETE
,PUT
包括GET
和POST
.
有时需要禁用此功能,尤其是在处理多部分请求时。为了在特定请求时禁用它(关于 mulripart),我使用了以下代码。
package resolver;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartResolver;
/**
* Disables the spring multipart resolver for specific client requests and
* therefore keeps the request intact so that controllers can process it in
* whatever way they wish. This behaviour is triggered by a particular GET
* parameter in the client request so it is configurable.
* @see MultipartResolver
*/
@Aspect
public final class MultipartResolverDisablingAspect
{
/**
* GET parameter which, if present in request, enables advice.
*/
private static final String DISABLING_HTTP_REQUEST_PARAMETER_KEY = "_multipartResolverDisable";
private static boolean disablingParameterExists(final HttpServletRequest request)
{
Assert.notNull(request);
return request.getParameter(DISABLING_HTTP_REQUEST_PARAMETER_KEY) != null;
}
/**
* If above GET parameter exists in request then prompt the spring multipart
* resolver to always tell spring that request is not of type multipart.
* Spring then does not process the request any further.
* @param pjp
* @param request
* @return
* @throws Throwable
*/
@Around("isMultipartOperation() && args(request)")
public Object disableIsMultipartOperation(final ProceedingJoinPoint pjp, final HttpServletRequest request) throws Throwable
{
Assert.notNull(pjp);
Assert.notNull(request);
if (disablingParameterExists(request))
{
return Boolean.FALSE;
}
return pjp.proceed();
}
/**
* Applies to any implementation of {@linkplain MultipartResolver}
*/
@SuppressWarnings("unused")
@Pointcut("execution(public boolean " + "org.springframework.web.multipart.MultipartResolver." + "isMultipart(javax.servlet.http.HttpServletRequest))")
private void isMultipartOperation() {}
}
在application-context.xml
文件中,需要以下 xml。
<aop:aspectj-autoproxy proxy-target-class="false" />
<bean class="resolver.MultipartResolverDisablingAspect" /> <!--Registers the above bean (class)-->
此代码取自这篇文章的“多部分解析器禁用方面”部分。
它旨在通过HiddenHttpMethodFilter
将GET
参数multipartResolverDisable=1
用作上面代码指定的查询字符串来禁用多部分处理,以便可以像往常一样使用公共multipartResolverDisable=1
文件上传(当作为查询字符串提供时)
实际的问题仍然不在图片中。此方法之前在以下环境中正常工作(使用 NetBeans 6.9.1)。
- 春天 3.2.0
- 带有 Servlet API 2.5 的 Apache Tomcat 6.0.26.6。
最近我用具有 Servlet API 3.0 的 Apache Tomcat 7.0.35 升级了 NetBeans 7.2.1。Spring 版本与之前相同- Spring 3.2.0。
通过此更新,上述方法导致以下异常。
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name
'org.springframework.transaction.config.internalTransactionAdvisor':
Cannot resolve reference to bean
'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0'
while setting bean property 'transactionAttributeSource'; nested
exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name
'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0':
Initialization of bean failed; nested exception is
**java.lang.IllegalArgumentException: error at ::0 can't find referenced
pointcut isMultipartOperation**
其中isMultipartOperation()
是上述类中的最后一个方法。
代码可能会有一点小改动,但我对 AOP 知之甚少,无法在这段好看的代码中找出这个异常的原因。
这个异常的原因是什么?它必须与 Servlet API 做些什么吗?