java - AspectJ 在 Eclipse 中不能正常工作

标签 java spring aop aspectj spring-aop

我尝试用 java 创建一个简单的程序。

在我的程序中:

1) Person 类 - 带有带注释的方法

2) 方面类。

现在我想做的是在设置人名之前,将一些数据打印到日志文件和控制台。

这就是我所做的:

人员类别

package pack.bl;

import org.springframework.stereotype.Component;
import pack.aop.LogLevel;
import pack.aop.TestAnnotation;

@Component
public class Person {

private String name,dest;
public Person(String name,String dest)
{
    this.setName(name);
    this.setDest(dest);
}

public String getName() {
    return name;
}

@TestAnnotation(value=LogLevel.INFO)
public void setName(String name) {
    this.name = name;
    System.out.println("Im " + this.toString() + " My name was changed to " + name);
}

public String getDest() {
    return dest;
}

@TestAnnotation(value=LogLevel.INFO)
public void setDest(String dest) {
    this.dest = dest;
}

@Override
public String toString()
{
    return this.name + "\n";
}

public static void main(String[] args) {
    Person p = new Person("Person1","Kripton");
    p.setName("Person2");
}

}

方面类

package pack.aop;

import java.util.Arrays;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.test.context.ContextConfiguration;


@Aspect
@ContextConfiguration(locations = {
        "applicationContext.xml"})

public class BeforeAdvice {

private Log logger = LogFactory.getLog("Logger");

@Before("@annotation(testAnnotation)")
public void myBeforeLogger(JoinPoint joinPoint,TestAnnotation testAnnotation)
{
    System.out.println("Okay - we're in the before handler...");
    System.out.println("The test annotation value is: " + testAnnotation.value());
    Signature signature = joinPoint.getSignature();
    String methodName = signature.getName();
    String stuff = signature.toString();
    String arguments = Arrays.toString(joinPoint.getArgs());
    logger.info("Write something in the log... We are just about to call method: "
    + methodName + " with arguments " + arguments + "\nand the full toString: "
    + stuff);
}

}

ApplicationContext.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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context.xsd">

<aop:aspectj-autoproxy/>

<context:component-scan base-package="pack.bl.Person"/>

*注意: LogLevel 是一个枚举。所以当我运行时它需要这样工作 程序并设置人名,首先需要转到Aspect类中的'BeforeAdvice'(因为setName,是用@testAnnotation注释的)方法,并执行该方法..之后需要返回setName方法并设置人名。*

还有一件事,testAnnotation 是我创建的注释

最佳答案

您没有提及任何有关您如何提出问题的内容。据我所知,您似乎将这项工作委托(delegate)给了 Spring 容器。但是您使用的是 Spring AOP 而不是 AspectJ。尽管您也可以将 AspectJ 注释与 Spring AOP 一起使用。

我的建议是分成两种情况。首先确保 AspectJ 方面已编织并使用仅使用 System.out.println(..) 的简单建议方法,然后确保 Spring 配置与您的方面和 Log 枚举集成。

使用 AJDT Eclipse 插件进行编写

要启用 AspectJ 编织,最简单的替代方法是使用 AspectJ plugin并使用编译时编织。右键单击项目并启用 AspectJ nature 后,您将在代码中看到橙色箭头,其中包含建议。有关示例,请参阅下图。

我不确定你的切入点是否有效。如果您的建议没有编织在任何地方,您应该尝试这个切入点:

@Pointcut("execution(@pack.aop.TestAnnotation * *(..)) ")
public void logMethod() {}

并像这样建议:​​

@Before("logMethod()")
public void beforeLogMethod(JoinPoint joinPoint) {
    System.out.println("Logging..");
}

将切面和枚举与 Spring 容器集成

其次,由于切面是在 Spring 容器之前创建的,因此您必须从切面的工厂方法 Aspects.aspectOf(pack.aop.BeforeAdvice.class) 检索切面,或者使用工厂 - Spring 配置中的方法。

从 Spring XML 配置中,您可以像这样检索方面(对象):

<bean id="beforeAdvice" class="apack.aop.BeforeAdvice"
    factory-method="aspectOf" />

您还必须使用工厂方法来检索在 Spring 容器外部创建的记录器。

我写过相关的blog post它通过示例解释了您的大部分问题,并通过图片展示了 AJDT Eclipse 插件的工作原理。

enter image description here

该图像显示了两个箭头,说明了后建议,最后一个箭头说明了周围建议。

关于java - AspectJ 在 Eclipse 中不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12167397/

相关文章:

c# - PostSharp:使用 OnMethodInvocationAspect 时删除了自定义属性

java - 如何在Spring数据JPA中获取结果集中的列名

java - JFrame和JPanel之间的通信

MySQL 查询 LocalDateTime 字段上的 DATE() 函数

java - 由于 bean 命名冲突,Spring Boot 应用程序无法运行

asp.net-mvc - 使用 Asp.Net MVC 进行自动交易的最佳方式是什么?

java - 如何从非 Activity 和 finish() 获取应用程序上下文;?

java - 如何使用 Java 套接字将加密添加到我自己的基于 TCP 的协议(protocol)中?

eclipse - tc 服务器无法在 SpringsourceToolSuite 中启动

java - AOP + Jenkins + Maven 集成