为什么我的Aspect不做任何事情? [英] Why my Aspect doesn't do anything?

查看:157
本文介绍了为什么我的Aspect不做任何事情?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个很简单的类,是一个方面:

This is a simple class which is an aspect:

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;
        }


}

这是我的主班:

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);

    }
}

这是我的文件夹lib:

this is my the folder lib:

这是我的aop.xml:

this is my aop.xml:

<aspectj>
    <aspects>

        <aspect name="aspectTest.aspect"/>

</aspects>

</aspectj>

这是我的运行配置:

我的问题是,当我运行主类时,它只会写hello而不是hello hello,这应该是由于方面.有人知道为什么我的方面没有任何影响吗?

my problem is that when I run my main class, it just writes hello but not hello hello, which should be because of aspect. Anyone knows why my aspect doesn't make any affect?

推荐答案

这对我有用.

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文件进行比较

Perhaps you need to redefine your project type to aspect project. Compare with your .projects file

<?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>

这篇关于为什么我的Aspect不做任何事情?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆