Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 需要在没有xml配置的情况下调用方面_Java_Spring Mvc_Aop - Fatal编程技术网

Java 需要在没有xml配置的情况下调用方面

Java 需要在没有xml配置的情况下调用方面,java,spring-mvc,aop,Java,Spring Mvc,Aop,我想在spring中运行一个方面,而不使用xml文件。 我已经编写了如下的类,AOPTest类是我的junit测试用例,它调用方法showProducts(),但在调用showProducts()之前 我需要调用方面logBeforeV1(..),这在下面的代码中没有被调用。如有任何意见,将不胜感激 package com.aop.bl; 导入org.springframework.context.annotation.ComponentScan; 导入org.springframework.c

我想在spring中运行一个方面,而不使用xml文件。 我已经编写了如下的类,AOPTest类是我的junit测试用例,它调用方法showProducts(),但在调用showProducts()之前 我需要调用方面logBeforeV1(..),这在下面的代码中没有被调用。如有任何意见,将不胜感激

package com.aop.bl;
导入org.springframework.context.annotation.ComponentScan;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.context.annotation.enableAspectProxy;
@配置
@组件扫描(basePackages=“com.aop.bl”)
@促性腺激素
公共类MyBusinessLogicImpl{
公共产品(){
//业务逻辑
System.out.println(“----显示从业务层调用的产品----”;
}
}
package com.aop.bl;
导入org.aspectj.lang.JoinPoint;
导入org.aspectj.lang.annotation.After;
导入org.aspectj.lang.annotation.Aspect;
导入org.aspectj.lang.annotation.Before;
导入org.springframework.stereotype.Component;
@面貌
@组成部分
公共类MyAspect{
@在(“执行(*com.aop.bl.MyBusinessLogicImpl.showProducts(..)”)之前//切点表达式
1(连接点连接点)之前的公共无效日志{
System.out.println(“--------------从MyAspect--------------------:”调用showProducts()”;
}
}
package com.aop.test;
导入org.junit.Test;
导入com.aop.bl.*;
公共类AOPTest{
@试验
公开无效测试(){
MyBusinessLogicImpl myObj=新的MyBusinessLogicImpl();
myObj.showProducts();
}
}
我的输出如下:

---show products called from business layer----
预期产出:

------------calling showProducts() from MyAspect---------------:
---show products called from business layer----

注意:我已经使用
@enableSpectJautoproxy
启用了aspect。您的单元测试是在spring上下文之外启动的,因此您需要导入配置

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = { MyBusinessLogicImpl.class })
public class AOPTest {
    ...
}

单元测试是在spring上下文之外启动的,因此需要导入配置

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = { MyBusinessLogicImpl.class })
public class AOPTest {
    ...
}

感谢您的更新,什么是SpringRunner.class@NikolayShevchenko@user3684675Spring对JUnit的Runnerok的实现..我将尝试从main()方法调用并测试,而不使用@RunWith..Shevchenko-正如您前面提到的,我已经添加了..但它仍然没有在MyAspect类中调用之前的方法谢谢您的更新,什么是SpringRunner.class@NikolayShevchenko@user3684675Spring对JUnit的Runnerok的实现..我将尝试从main()方法调用并测试,而不使用@RunWith..Shevchenko-正如您前面提到的,我已经添加了..但它仍然没有在MyAspect类中调用Before