【问题标题】:Aspectj advice not getting executedAspectj 建议未执行
【发布时间】:2015-07-18 09:01:03
【问题描述】:

我正在尝试编写一个简单的 AspectJ 实现,但 Advice 没有得到执行。

LoggingAspect 类正在启动,因为在控制台中我可以看到构造函数的 s.o.p

This is logging aspect 

在加载 ApplicationContext 并初始化 controllerAspect 时打印。当我运行 main 方法时,输出是

Name: John Age :12

我所期望的是应该首先打印 @Before 建议的 s.o.p,然后应该打印 getter 方法 @AfterReturning s.o.p。

没有编译错误,程序正在运行但建议没有被执行。

根据切入点,建议应该在 Customer 类的所有方法上实现。

我遇到了这里发布的一些类似问题,但无法弄清楚我的实现有什么问题。

谁能指出我所犯的错误并提供解决方案?

这里是 sn -p servlet-context.xml

<context:component-scan base-package="main.com.controller"/>                
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven></annotation-driven>      

<aop:aspectj-autoproxy/>
<!-- AOP support -->      

<bean id='controllerAspect' class='main.com.logging.LoggingAspect' />
<beans:bean id="cust" class="main.com.dtos.Customer"</beans:bean> 

<resources mapping="/resources/**" location="/resources/" />

<beans:bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">       
<beans:property name="prefix" value="/WEB-INF/views/"/>
<beans:property name="suffix" value=".jsp"/>
</beans:bean>

LoggingAspect 类是:

@Aspect
public class LoggingAspect {
    public LoggingAspect() {
        System.out.println("This is logging aspect");
    }

    @Pointcut("execution(* com.dtos.*.*(..))")
    public void getMethodPointcut() {}

    @AfterReturning(pointcut = "getMethodPointcut()", returning="retVal")
    public void afterReturningAdvice(Object retVal) {
        System.out.println("Returning:" + retVal.toString() );
    }

    @Before("getMethodPointcut()")
    public void printBefore(JoinPoint jp ) {
        System.out.println("Before calling method:"+jp.getSignature());
}

客户类别是:

@Size(max=30, min=3)
@NotNull
private String name;

@Max(150)
@NotNull
private String age; 

public Customer() {}

public Customer(String sName, String sAge) {
    name = sName;
    age = sAge;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAge() {
    return age;
}

public void setAge(String age) {
    this.age = age;
}

@Override
public String toString() {
    return "Customer [name=" + name + ", age=" + age + "]";
}

主要方法是:

public static void main(String[] args) {
    ApplicationContext context = 
        new ClassPathXmlApplicationContext("classpath:servlet- context.xml");

    Customer cust = (Customer)context.getBean("cust");
    cust.setAge("12");
    cust.setName("John");

    String name = cust.getName();
    String age = cust.getAge();

    System.out.println("Name: "+name+" Age :"+age);
}

Aspectj POM 依赖项

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.7.4</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjtools</artifactId>
    <version>1.7.4</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.7.4</version>
</dependency>

【问题讨论】:

  • 您的代码中缺少最重要的部分 sn -p:Customer 的包名称是什么?您的 Spring 配置暗示它是 main.com.dtos.Customer,但您的切面切入点正在寻找 com.dtos.*(没有 main. 前缀)。
  • 包是main.com.dtos. 谢谢指出,我把切入点改成execution(* main.com.dtos.Customer.get*()) 并且成功了。
  • 抱歉缩进,块格式丢失了。

标签: spring-mvc aspectj


【解决方案1】:

为了让您能够通过接受答案来结束问题,我在评论中重复我的回答:

您的 Spring 配置暗示它是 main.com.dtos.Customer,但您的切面切入点正在寻找 com.dtos.*(没有 main. 前缀)。

【讨论】:

    猜你喜欢
    • 2014-10-01
    • 1970-01-01
    • 2020-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 1970-01-01
    相关资源
    最近更新 更多