1
我遵循Spring in Action示例創建一個使用AOP將任何書籍添加到庫中的庫應用程序。我寫了Aspect(下面給出)並定義了界面ILibrary中addBook方法的切點。該方法addBook()採用類型的參數IBOOK使用@AspectJ元素自動代理
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);
}
圖書館應用程序是如以下給出。作爲給予
<?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>
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上下文XML的問題是我每次運行這個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 * *(..))」)
根據春季文檔,以便它匹配任何公共方法。像這樣的異常被拋出。
Caused by: java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut
at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:317)
它引發與不同消息相同的異常。這與我傳遞的論點有關嗎?不是「(..)」表達式來匹配我們傳遞的任何參數嗎?
丹努什