![](/img/trans.png)
[英]AspectJ & Maven warning: “Advice defined in … has not been applied?”
[英]Spring @AspectJ: Advice not applied
我試圖在調用start();
之前編寫代碼start();
這是我想建議的TestClass:
package com.test;
public class TestClass {
public static void main(String[] args) {
new TestClass().start();
}
private void start() {
System.out.println("Test started");
}
}
這是包括建議在內的方面:
package com.test;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LogAspect {
@Before("call(void com.test.TestClass.start())")
public void logBefore(JoinPoint joinPoint) {
System.out.println("logBefore() is running");
}
}
這是我的spring.xml:
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:aspectj-autoproxy/>
<context:annotation-config/>
<context:component-scan base-package="com.test"/>
我還嘗試在XML中顯式命名bean,如下所示:
<aop:aspectj-autoproxy/>
<bean id="test" class="com.test.TestClass" />
<bean id="aspect" class="com.test.LogAspect" />
這是我的項目設置(我使用的是Spring Tool Suite 3.1.0版):
結果是TestClass.start被調用就好了,但是沒有應用建議。
我需要更改什么以便應用建議?
謝謝。
編輯 :我終於得到了它的工作:
在根據你的建議編輯我的代碼后,我看了一下本教程 。 這讓我讓我的TestClass 實現了一個接口 。 這解決了問題。
這是我的最終設置:
TestClass包括其界面:
package com.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
interface TestClass {
void start();
}
public class TestClassImpl implements TestClass {
public static void main(String[] args) {
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
"Spring-Config.xml");
TestClass t1 = (TestClass) appContext.getBean("myTest");
t1.start();
}
@Override
public void start() {
System.out.println("test");
}
}
方面:
package com.test;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LogAspect {
@Before("execution(void com.test.TestClass.start(..))")
public void logBefore(JoinPoint joinPoint) {
System.out.println("logging before "
+ joinPoint.getSignature().getName());
}
}
和Spring-Config.xml
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:aspectj-autoproxy/>
<bean id="myTest" class="com.test.TestClassImpl" />
<bean id="logAspect" class="com.test.LogAspect" />
</beans>
謝謝你的幫助。
當從另一個類調用該方法時, call
AspectJ切入點應用。 當你自己直接調用它時,你應該使用執行切入點,所以改變:
@Before("call(void com.test.TestClass.start())")
至
@Before("execution(void com.test.TestClass.start())")
還讓Spring
創建bean而不是直接創建bean。
旁白:注意,您可以將Spring
XML文件與src/resources
源文件分開,它們將被ClassPathXmlApplicationContext
選中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.