gpt4 book ai didi

java - 使用 @AspectJ 元素进行自动代理

转载 作者:行者123 更新时间:2023-11-30 05:04:23 25 4
gpt4 key购买 nike

我正在按照 Spring in Action 示例创建一个图书馆应用程序,该应用程序使用 AOP 将任何添加的书籍注册到图书馆。我已经编写了 Aspect(如下所示)并为接口(interface) 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);
}

图书馆应用程序如下所示。

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("执行(public * *(..))")

按照 spring 文档,使其匹配任何公共(public)方法。抛出这样的异常。

Caused by: java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut 
at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:317)

它用不同的消息抛出相同的异常。这与我传递的论点有关吗? “(..)”表达式不匹配我们传递的任何参数吗?

丹努什

最佳答案

意识到需要在接受参数的方法的@Before或@After注解中添加参数

@Before("adding() && args(book)") 
public boolean isBookAlreadyAvailableInLibrary(IBook book) { //..... }

还将切入点 def 更改为

@Pointcut("execution(* *.addBook(..))") 

现在开始工作了

谢谢

关于java - 使用 @AspectJ 元素进行自动代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5617693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com