我正在尝试测试一个简单的方面。 该应用程序编译并运行良好,但我没有执行方面。或者至少,我没有得到方面应该产生的输出。 (我的目标是为应用程序中发生的任何异常编写一个异常记录器。但首先应该运行此测试方面...)
也许在各方面有更多经验的人会看到我做错了什么?
package business;
public interface Customer {
void addCustomer();
}
import org.springframework.stereotype.Component;
@Component
public class CustomerImpl implements Customer {
public void addCustomer() {
System.out.println("addCustomer() is running ");
}
}
@RequestScoped @Named
//this is backing bean for jsf page
public class Service {
@Inject
Customer cust;
add() {
System.out.println("Service is running ");
cust.addCustomer();
}
}
@Aspect
public class AspectComp {
@Before("within(business..*)")
public void out() {
System.out.println("system out works!!");
}
}
Spring :
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">
<context:annotation-config />
<context:component-scan base-package="business" />
<aop:aspectj-autoproxy />
</beans>
输出:
Service is running
addCustomer() is running
缺少 Aspect 语句。
最佳答案
您正在使用其构造函数创建组件,而不是从 Spring 容器获取它!这就是问题所在,否则您必须使用 AspectJ 的加载时编织器。
只需在服务中注入(inject)组件 (CustomerImpl),然后使用注入(inject)的实例即可。
关于java - 方面未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9604872/