使用 @AspectJ 元素进行自动代理
我按照 Spring in Action 示例创建一个图书馆应用程序,该应用程序使用 AOP 将任何添加的书籍注册到图书馆。我已经编写了 Aspect(如下所示)并为接口 ILibrary 中的方法 addBook 定义了切入点。 类型的参数。
方法 addBook() 采用 IBook LibraryRegisterAspect.java
@Aspect
public class LibraryRegisterAspect {
private ILibrary aspectLibrary;
private int count;
private Map<IAuthor, List<String>> authorBookMap;
public LibraryRegisterAspect() {
authorBookMap = new Hashtable<IAuthor, List<String>>();
}
@Pointcut("com.dell.spring.interfaces.ILibrary.addBook(..)")
public void adding() {
}
/**
* @return
*/
@Before("adding()")
public int takeStock() {
try {
System.out.println("THE COUNT IS "
+ aspectLibrary.getAllBooks().size());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return count;
}
/**
* @param book
* @return
*/
@Before("adding()")
public boolean isBookAlreadyAvailableInLibrary(IBook book) {
System.out.println("CHECKING IF BOOK IS ALREADY THERE");
try {
for (IBook bookInLib : aspectLibrary.getAllBooks()) {
if (bookInLib.equals(book))
return true;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("CHECKED IF BOOK IS ALREADY THERE");
return false;
}
/**
* @param library
*/
public void setAspectLibrary(ILibrary library) {
this.aspectLibrary = library;
try {
count = library.getAllBooks().size();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param book
*/
@AfterReturning("adding()")
public void updateRegistry(IBook book) {
try {
count = aspectLibrary.getAllBooks().size();
List<String> authorBooks = authorBookMap.get(book.getAuthor());
if (authorBooks == null)
authorBooks = new ArrayList<String>();
authorBooks.add(book.getTitle());
authorBookMap.put(book.getAuthor(), authorBooks);
System.out.println("REGISTRY Updated " + authorBooks.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param author
* @return
*/
public List<String> getTitlesForAuthor(IAuthor author) {
return authorBookMap.get(author);
}
Library App 如下所示。
public class LibraryApp {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
ApplicationContext factory = new FileSystemXmlApplicationContext(
"src/main/resources/spring-beans-aop.xml");
Library library = (Library) factory.getBean("library");
System.out.println(library.getAllBooks().toString());
Author author2 = new Author("R.K. Narayan");
Book book1 = new Book("Malgudi Days", 100, author2, "Malgudi ad");
Book book2 = new Book("Man Eater of Malgudi", 100, author2,
"Malgudi ad");
try {
library.addBook(book1);
library.addBook(book2);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("AFTER ADD");
System.out.println(library.getAllBooks().toString());
}
spring context xml如给定
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
xmlns:aop="http://www.springframework.org/schema/aop">
<aop:aspectj-autoproxy />
<bean id="library" class="com.dell.spring.impl.Library">
<property name="books">
<list>
<ref bean="solitude" />
<ref bean="cholera" />
<ref bean="book2" />
<ref bean="book3" />
<ref bean="book4" />
<ref bean="book5" />
</list>
</property>
</bean>
<bean id="libregister" class="com.dell.spring.impl.LibraryRegisterAspect">
<property name="aspectLibrary" ref="library" />
</bean>
<bean id="marquez" class="com.dell.spring.impl.Author">
<constructor-arg value="Gabriel Garcia Marquez" />
</bean>
<bean id="archer" class="com.dell.spring.impl.Author">
<constructor-arg value="Jeffrey Archer" />
</bean>
<bean id="saramago" class="com.dell.spring.impl.Author">
<constructor-arg value="Jose Saramago" />
</bean>
<bean id="toni" class="com.dell.spring.impl.Author">
<constructor-arg value="Toni Morrison" />
</bean>
<bean id="salinger" class="com.dell.spring.impl.Author">
<constructor-arg value="J. D. Salinger" />
</bean>
<bean id="garciaBooks" class="com.dell.spring.impl.Book" abstract="true">
<property name="author" ref="marquez" />
</bean>
<bean id="groovyBook" class="com.dell.spring.groovy.impl.GroovyBook">
<property name="title" value="Shall we Tell The President?" />
<property name="price" value="400" />
<property name="synopsis"
value="A plot to kill
the first woman president of USA" />
<property name="author" ref="archer" />
</bean>
<bean id="solitude" parent="garciaBooks">
<property name="title" value="On Hundred Years of Solitude" />
<property name="synopsis" value="Tale of Macondo" />
<property name="price" value="500" />
</bean>
<bean id="cholera" parent="garciaBooks">
<property name="title" value="Love in the Time of Cholera" />
<property name="synopsis" value="Garcia's parents' love story" />
<property name="price" value="500" />
</bean>
<bean id="book2" class="com.dell.spring.impl.Book">
<property name="title" value="The Double" />
<property name="price" value="300" />
<property name="synopsis" value="Tertuliano sees his double on the TV" />
<property name="author" ref="saramago" />
</bean>
<bean id="book5" class="com.dell.spring.impl.Book">
<property name="title" value="Baltasar and Blimunda" />
<property name="price" value="300" />
<property name="synopsis"
value="Love Story of 2 lovers in the period of inquisition" />
<property name="author" ref="saramago" />
<replaced-method name="readSynopsis" replacer="replacer" />
</bean>
<bean id="replacer" class="com.dell.spring.impl.BookSynopsisReplacer"></bean>
<bean id="book3" class="com.dell.spring.impl.Book">
<property name="title" value="Beloved" />
<property name="synopsis"
value="The story of the Negro Woman who killed her daughter" />
<property name="price" value="345" />
<property name="author" ref="toni" />
</bean>
<bean id="book4" class="com.dell.spring.impl.Book">
<property name="title" value="The Catcher In The Rye" />
<property name="synopsis" value="Rye" />
<property name="price" value="678" />
<property name="author" ref="salinger" />
</bean>
问题是每次我运行LibraryApp时,它都会抛出异常
Caused by: java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut addBook
at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:317)
当我像这样定义切入点时 @Pointcut("execution(public * *(..))")
根据 spring 文档,以便它匹配任何公共方法。抛出这样的异常。
Caused by: java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut
at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:317)
它用不同的消息抛出相同的异常。这与我传递的论点有关吗? “(..)”表达式不匹配我们传递的任何参数吗?
达努什
I am following Spring in Action example to create a Library App which uses AOP to register any addition of Book to the Library. I have written the Aspect (given below) and defined the point cut for a method addBook in the interface ILibrary. The method addBook() takes a parameter of type IBook
LibraryRegisterAspect.java
@Aspect
public class LibraryRegisterAspect {
private ILibrary aspectLibrary;
private int count;
private Map<IAuthor, List<String>> authorBookMap;
public LibraryRegisterAspect() {
authorBookMap = new Hashtable<IAuthor, List<String>>();
}
@Pointcut("com.dell.spring.interfaces.ILibrary.addBook(..)")
public void adding() {
}
/**
* @return
*/
@Before("adding()")
public int takeStock() {
try {
System.out.println("THE COUNT IS "
+ aspectLibrary.getAllBooks().size());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return count;
}
/**
* @param book
* @return
*/
@Before("adding()")
public boolean isBookAlreadyAvailableInLibrary(IBook book) {
System.out.println("CHECKING IF BOOK IS ALREADY THERE");
try {
for (IBook bookInLib : aspectLibrary.getAllBooks()) {
if (bookInLib.equals(book))
return true;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("CHECKED IF BOOK IS ALREADY THERE");
return false;
}
/**
* @param library
*/
public void setAspectLibrary(ILibrary library) {
this.aspectLibrary = library;
try {
count = library.getAllBooks().size();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param book
*/
@AfterReturning("adding()")
public void updateRegistry(IBook book) {
try {
count = aspectLibrary.getAllBooks().size();
List<String> authorBooks = authorBookMap.get(book.getAuthor());
if (authorBooks == null)
authorBooks = new ArrayList<String>();
authorBooks.add(book.getTitle());
authorBookMap.put(book.getAuthor(), authorBooks);
System.out.println("REGISTRY Updated " + authorBooks.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param author
* @return
*/
public List<String> getTitlesForAuthor(IAuthor author) {
return authorBookMap.get(author);
}
The Library App is as given below.
public class LibraryApp {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
ApplicationContext factory = new FileSystemXmlApplicationContext(
"src/main/resources/spring-beans-aop.xml");
Library library = (Library) factory.getBean("library");
System.out.println(library.getAllBooks().toString());
Author author2 = new Author("R.K. Narayan");
Book book1 = new Book("Malgudi Days", 100, author2, "Malgudi ad");
Book book2 = new Book("Man Eater of Malgudi", 100, author2,
"Malgudi ad");
try {
library.addBook(book1);
library.addBook(book2);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("AFTER ADD");
System.out.println(library.getAllBooks().toString());
}
The spring context xml is as given
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
xmlns:aop="http://www.springframework.org/schema/aop">
<aop:aspectj-autoproxy />
<bean id="library" class="com.dell.spring.impl.Library">
<property name="books">
<list>
<ref bean="solitude" />
<ref bean="cholera" />
<ref bean="book2" />
<ref bean="book3" />
<ref bean="book4" />
<ref bean="book5" />
</list>
</property>
</bean>
<bean id="libregister" class="com.dell.spring.impl.LibraryRegisterAspect">
<property name="aspectLibrary" ref="library" />
</bean>
<bean id="marquez" class="com.dell.spring.impl.Author">
<constructor-arg value="Gabriel Garcia Marquez" />
</bean>
<bean id="archer" class="com.dell.spring.impl.Author">
<constructor-arg value="Jeffrey Archer" />
</bean>
<bean id="saramago" class="com.dell.spring.impl.Author">
<constructor-arg value="Jose Saramago" />
</bean>
<bean id="toni" class="com.dell.spring.impl.Author">
<constructor-arg value="Toni Morrison" />
</bean>
<bean id="salinger" class="com.dell.spring.impl.Author">
<constructor-arg value="J. D. Salinger" />
</bean>
<bean id="garciaBooks" class="com.dell.spring.impl.Book" abstract="true">
<property name="author" ref="marquez" />
</bean>
<bean id="groovyBook" class="com.dell.spring.groovy.impl.GroovyBook">
<property name="title" value="Shall we Tell The President?" />
<property name="price" value="400" />
<property name="synopsis"
value="A plot to kill
the first woman president of USA" />
<property name="author" ref="archer" />
</bean>
<bean id="solitude" parent="garciaBooks">
<property name="title" value="On Hundred Years of Solitude" />
<property name="synopsis" value="Tale of Macondo" />
<property name="price" value="500" />
</bean>
<bean id="cholera" parent="garciaBooks">
<property name="title" value="Love in the Time of Cholera" />
<property name="synopsis" value="Garcia's parents' love story" />
<property name="price" value="500" />
</bean>
<bean id="book2" class="com.dell.spring.impl.Book">
<property name="title" value="The Double" />
<property name="price" value="300" />
<property name="synopsis" value="Tertuliano sees his double on the TV" />
<property name="author" ref="saramago" />
</bean>
<bean id="book5" class="com.dell.spring.impl.Book">
<property name="title" value="Baltasar and Blimunda" />
<property name="price" value="300" />
<property name="synopsis"
value="Love Story of 2 lovers in the period of inquisition" />
<property name="author" ref="saramago" />
<replaced-method name="readSynopsis" replacer="replacer" />
</bean>
<bean id="replacer" class="com.dell.spring.impl.BookSynopsisReplacer"></bean>
<bean id="book3" class="com.dell.spring.impl.Book">
<property name="title" value="Beloved" />
<property name="synopsis"
value="The story of the Negro Woman who killed her daughter" />
<property name="price" value="345" />
<property name="author" ref="toni" />
</bean>
<bean id="book4" class="com.dell.spring.impl.Book">
<property name="title" value="The Catcher In The Rye" />
<property name="synopsis" value="Rye" />
<property name="price" value="678" />
<property name="author" ref="salinger" />
</bean>
The problem is every time I run the LibraryApp, it throws out an exception
Caused by: java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut addBook
at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:317)
When I define the point cut like this
@Pointcut("execution(public * *(..))")
as per the spring documentation so that it matches any public method. An exception like this is thrown.
Caused by: java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut
at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:317)
It throws the same exception with different messages. Has it something to do with the arguments that I am passing? Isn't "(..)" expression to match any argument that we pass?
Dhanush
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
意识到需要在接受参数的方法的 @Before 或 @After 注释中添加参数
还将切入点 def 更改为
所以现在可以工作了
谢谢
Realised the need of adding the parameters in the @Before or @After annotations for the methods that takes in a parameter
Also changed the pointcut def to
So now got it working
Thank You