1
在項目中,我們使用註釋來定義方面。eclipse中的建議方法的標記
不幸的是,我不能讓eclipse在建議的方法旁邊顯示標記。
在另一個項目中,我們使用XML來定義方面和eclipse顯示標記。
盡我張貼一些代碼來澄清:
首先一個Bean建議:
package aop.test;
import org.springframework.stereotype.Service;
@Service
public class Worker {
public void work() {}
}
然後方面:
package aop.test;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Service;
@Aspect
@Service
public class WorkerLogger {
@Before("execution(void aop.test.Worker.work())")
public void log() {
System.out.println("working...");
}
}
最後一個主要方法制備的ApplicationContext ,得到豆並運行建議的方法:
package aop.test;
import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context;
context = new AnnotationConfigApplicationContext();
context.register(AnnotationAwareAspectJAutoProxyCreator.class);
context.scan("aop.test");
context.refresh();
context.getBean(Worker.class).work();
}
}
我在使用STS插件和STS本身的eclipse中試過這個。我從來沒有在work()
旁邊得到一個紅色箭頭表示建議。
我錯過了什麼?
我改變了這個例子,使用一個定義工作者和記錄器的bean,aspect和` `的XML,並且仍然沒有看到標記建議的方法。有什麼選擇我必須啓用? –
CKuck
2011-12-15 10:05:03
當我啓用Spring Aspects Tooling時,它只是添加了AspectJ Nature,我可以看到標記,但AspectJ Nature不是我想要的。建議使用非託管實例或靜態方法,這意味着使用AspectJ編譯器。 – CKuck 2011-12-15 13:59:59