java - 方面未执行

标签 java spring jakarta-ee spring-aop

我正在尝试测试一个简单的方面。 该应用程序编译并运行良好,但我没有执行方面。或者至少,我没有得到方面应该产生的输出。 (我的目标是为应用程序中发生的任何异常编写一个异常记录器。但首先应该运行此测试方面...)

也许在各方面有更多经验的人会看到我做错了什么?

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/

相关文章:

java - 使用 XPath 从 XML 文件获取特定值

java - Spring 返回 String 作为 JSON,行为改变

java - 当 cassandra 重新启动时,应用程序的准备好的语句会发生什么?

java - Spring MVC 专用登录拦截器

java - 如何在使用 JPA 和 Hibernate 获得 OptimisticLockException 时重试服务方法

java - 使用 EJB 注释的依赖注入(inject)

Java,使一个引用指向另一个引用

java - EnversSchemaGenerator 无法解析为类型

java - Java 的 ScheduledExecutorService.scheduleAtFixedRate() 行为

java - Spring可变实例数