为什么我的 Aspect 什么都不做?

Why my Aspect doesn't do anything?

这是一个简单的class这是一个方面:

package aspectTest;

import java.awt.Color;

import javax.swing.JLabel;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;


    @Aspect
    public class aspect {
        @Pointcut("execution(static String createMultiLabel(..))")
        public void multilabelCreation() {}

        @Around("multilabelCreation()")
        public String changeLabelColours(ProceedingJoinPoint thisJoinPoint) throws Throwable {

    String st = (String) thisJoinPoint.proceed();
System.out.println("fdfs");
    st = "st"+st;
    return st;
        }


}

这是我的主要 class:

package aspectTest;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class messagemethod {
    public static String createMultiLabel(final String msg) {

        return msg;
    }
    public static void main(String[] args) {
        String st1 = createMultiLabel("hello");
        System.out.println(st1);

    }
}

这是我的文件夹库:

这是我的 aop.xml:

<aspectj>
    <aspects>

        <aspect name="aspectTest.aspect"/>

</aspects>

</aspectj>

这是我的 运行 配置:

我的问题是当我运行我的主class时,它只写你好而不是你好,这应该是因为aspect。谁知道为什么我的方面没有任何影响?

这对我有用。

package test;

@Aspect
public class TestAspect {
    public static String createMultiLabel(final String msg) {
        return msg;
    }
    public static void main(String[] args) {
        String st1 = createMultiLabel("hello");
        System.out.println(st1);
    }

    @Around("execution(java.lang.String test.TestAspect.createMultiLabel(java.lang.String))")
    public String aroundCreateMultiLabel(ProceedingJoinPoint joinPoint) throws Throwable {
        System.err.println("in around before " + joinPoint);
        String string = (String) joinPoint.proceed();
        System.err.println("in around after " + joinPoint);
        return string;
    }
}

也许您需要将您的项目类型重新定义为方面项目。与您的 .projects 文件比较

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <name>Aspects</name>
    <buildSpec>
        <buildCommand>
            <name>org.eclipse.ajdt.core.ajbuilder</name>
        </buildCommand>
    </buildSpec>
    <natures>
        <nature>org.eclipse.ajdt.ui.ajnature</nature>
        <nature>org.eclipse.jdt.core.javanature</nature>
    </natures>
</projectDescription>