Spring AOP:在使用Collection执行方法之前

时间:2014-06-07 19:17:25

标签: java spring spring-aop

案例1(工作正常)

我正在尝试为以下方法实现before AOP实现:

public void setNames(List names) {
    this.names = names;
}

我试过了:

<aop:pointcut id="empl" expression="execution(* com.model.Employee.set*(java.util.List)) and args(names)"/>
    <aop:aspect ref="aspect">
        <aop:before pointcut-ref="empl" method="beforeExecutionEmpl" arg-names="names"/>
    </aop:aspect>

它有效。

案例2(不起作用)

但如果我将方法更改为此(添加了泛型):

public void setNames(List<String> names) {
    this.names = names;
}

和此:

<aop:pointcut id="empl" expression="execution(* com.model.Employee.set*(java.util.List<java.lang.String>)) and args(names)"/>
    <aop:aspect ref="aspect">
        <aop:before pointcut-ref="empl" method="beforeExecutionEmpl" arg-names="names"/>
    </aop:aspect>

然后我得到以下例外:

Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 44 in XML document from class path resource [spring-context.xml] is invalid; nested exception is org.xml.sax.SAXParseException: The value of attribute "expression" associated with an element type "null" must not contain the '<' character.
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:522)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:436)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.aop.App.main(App.java:19)
Caused by: org.xml.sax.SAXParseException: The value of attribute "expression" associated with an element type "null" must not contain the '<' character.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:174)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:388)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1414)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.scanAttributeValue(XMLScanner.java:932)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanAttribute(XMLNSDocumentScannerImpl.java:460)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:277)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2755)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:235)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:388)
... 14 more

请建议如何使Case 2工作,因为我想在代码中使用泛型。

1 个答案:

答案 0 :(得分:1)

我使用SpringFramework 3.2.4和aspectjrt和aspectjweaver 1.7.4进行了一些测试

我无法完全重现您的问题。案例2中的切入点表达式实际上是不正确的,应该写入(没有<...>

<aop:pointcut id="empl" expression="execution(* com.model.Employee.set*(java.util.List)) and args(names)"/>

界面(*)中提供

public void setNames(List<String> names);

并在你方面

public void beforeExecutionEmpl(List<String> names) {
//
}

这就是我在测试中所做的,我没有得到任何错误,并且有效地调用了建议。我必须承认,即使方面中的签名也是public void beforeExecutionEmpl(List<Integer> names)方面也被调用,但在使用列表元素时可能会出现错误,因为它们是Strings

(*)当你使用Spring AOP时,我认为切入点是在接口上......