我已经编写了一个带有注释(无xml文件)的示例AOP应用程序,该应用程序应与JUnit测试用例一起运行。 类如下所示,AOPTest类是我的junit测试用例,它调用方法showProducts(),但在调用showProducts()之前 我需要调用方面logBeforeV1(..)在下面的代码中没有被调用。任何输入将不胜感激。
package com.aop.bl;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan(basePackages="com.aop.bl")
@EnableAspectJAutoProxy
public class MyBusinessLogicImpl {
public void showProducts() {
//business logic
System.out.println("---show products called from business layer----");
}
}
package com.aop.bl;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class MyAspect {
@Before("execution(* com.aop.bl.MyBusinessLogicImpl.showProducts(..))") // point-cut expression
public void logBeforeV1(JoinPoint joinPoint) {
System.out.println("------------calling showProducts() from MyAspect---------------: ");
}
}
package com.aop.test;
import org.junit.Test;
import com.aop.bl.*;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = { MyBusinessLogicImpl.class })
public class AOPTest {
@Test
public void test() {
MyBusinessLogicImpl myObj = new MyBusinessLogicImpl();
myObj.showProducts();
}
}
我的输出如下:
---show products called from business layer----
预期输出:
------------calling showProducts() from MyAspect---------------:
---show products called from business layer----
注意:我已使用 @EnableAspectJAutoProxy
启用了方面
任何建议都会有帮助。
-编辑-
package com.aop.test;
import org.junit.Test;
import com.aop.bl.*;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = { MyBusinessLogicImpl.class })
public class AOPTest {
@Autowired MyBusinessLogicImpl myObj;
@Test
public void test() {
myObj.showProducts();
}
}
我编写了如下所示的修改测试类的方法,但是输出仍然与以前相同..它没有调用方面@Before。任何输入都会有所帮助。