Spring AOP: Before method execution with Collection

冷暖自知 提交于 2019-12-13 05:03:02

问题


Case 1 (Works fine)

I am trying to implement a before AOP implementation for the below method:

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

I tried this:

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

And it worked.

Case 2 (Does not works)

But if I change the method to this (Added the Generics):

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

and this:

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

Then I an getting the below exception:

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

Please suggest how to make the Case 2 work since I want to use generics in my code.


回答1:


I did some tests with SpringFramework 3.2.4, and aspectjrt and aspectjweaver 1.7.4

I could not exactly reproduce your problem. Your pointcut expression in case 2 is effectively incorrect and should be written (without <...>)

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

provided in your interface(*) you have

public void setNames(List<String> names);

and in you aspect

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

That's what I did in my tests and I got no error and the advice was effectively called. I must admit that even if signature in aspect is public void beforeExecutionEmpl(List<Integer> names) aspect is called too, but there can be errors at execution when using elements of the list because they are Strings.

(*) as you are using Spring AOP, I assume the pointcut is on an interface ...



来源:https://stackoverflow.com/questions/24100740/spring-aop-before-method-execution-with-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!