Java 未执行方面
我试图测试一个简单的方面。 该应用程序编译和运行良好,但我没有执行方面。或者至少,我没有得到方面应该产生的输出。 (我的目标是为应用程序中发生的任何ex编写一个异常记录器。但首先应该运行此测试方面…) 也许有人在这方面更有经验,看看我做错了什么Java 未执行方面,java,spring,jakarta-ee,spring-aop,Java,Spring,Jakarta Ee,Spring Aop,我试图测试一个简单的方面。 该应用程序编译和运行良好,但我没有执行方面。或者至少,我没有得到方面应该产生的输出。 (我的目标是为应用程序中发生的任何ex编写一个异常记录器。但首先应该运行此测试方面…) 也许有人在这方面更有经验,看看我做错了什么 package business; public interface Customer { void addCustomer(); } import org.springframework.stereotype.Component; @Comp
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!!");
}
}
春天:
<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>
缺少Aspect语句。您正在使用其构造函数创建组件,而不是从Spring容器中获取它!这就是问题所在,否则必须使用AspectJ的加载时编织器
只需将组件(CustomerImpl)注入到服务中,然后使用注入的实例。我记得曾经有过类似的问题;Spring没有实际加载代理,因为它没有将@Aspect注释识别为注释可扫描bean。我在@Aspect符号中添加了@Component注释,Spring开始扫描它 我从未调查过发生这种情况的原因,以及为什么我需要这样做,所以我无法确认这是“正确”的做事方式。我的直觉告诉我,我的配置文件中缺少了一些东西;我无法想象Spring为什么不扫描@Aspect bean 您可以做的另一件事是在XML配置文件中显式声明方面bean,看看这是否与您遇到的问题类型相同
您还可以在Spring框架中启用调试日志记录,并查看Spring是否正在加载bean。如果没有,那么它会给你一个开始寻找的想法。你的Spring配置是什么样子的?上面更新了一些非常重要的信息。我在SpringConfig中为aspects更改的唯一配置是*.xsd和。您正在使用其构造函数创建组件,而不是从SpringContainer获取它!这就是问题所在。但不要按类型(CustomerImpl)注入,而是按上面修改的接口(Customer)注入:
@injectcustomer-cust代码>但是行为仍然是一样的:没有执行方面。
Service is running
addCustomer() is running