我对 Spring AOP 真的很陌生。在我的应用程序中,我配置了 HiddenHttpMethodFilter
将方法参数转换为 HTTP 方法,并使 Spring 能够处理其他 HTTP 方法,如 DELETE
、PUT
等,包括 GET
和 POST
.
有时需要禁用此功能,尤其是在处理多部分请求时。为了根据特定请求(关于多部分)禁用它,我使用了以下代码。
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)-->
此代码取自 this该部分下的文章 - 多部分解析器禁用方面。
这是为了通过 HiddenHttpMethodFilter
禁用多部分处理当 GET
参数 multipartResolverDisable=1
用作查询字符串时由上面的代码指定 以便可以使用 commons fileupload像往常一样(当 multipartResolverDisable=1
作为查询字符串提供时)
真正的问题还没有出现在图片中。此方法早些时候在以下环境(使用 NetBeans 6.9.1)中正常工作。
- Spring 3.2.0
- 带有 Servlet API 2.5 的 Apache Tomcat 6.0.26.6。
最近,我用 Apache Tomcat 7.0.35 升级了 NetBeans 7.2.1,后者具有 Servlet API 3.0。 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 做些什么吗?
最佳答案
我认为你应该将你的周围建议更改为
@Around("isMultipartOperation(..) && args(request)")
你的切入点注释
@Pointcut("execution(* org.springframework.web.multipart.MultipartResolver.isMultipart(..)) && args(request)")
和切入点注释方法
private void isMultipartOperation(HttpServletRequest request) {}
根据内存,我遇到过这个问题,我试图用一些建议来包装切入点,而建议对切入点有不同数量的参数。在您的代码中,您似乎正在使用 @Around 来定位带有 args(request) 的切入点,但是您的切入点方法没有这样的参数。
关于spring - java.lang.IllegalArgumentException: error at::0 找不到引用的切入点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14521832/