潜水生活 2013-06-14 09:45
浏览 256
已采纳

Spring 3.2.2 AOP引入方式集成测试的问题

有如下一个配置

 

    <context:component-scan base-package="com.myapp.aop.introduce" />
    <bean id="forumService" class="org.springframework.aop.framework.ProxyFactoryBean"
        p:interfaces="com.myapp.aop.introduce.Monitorable" p:target-ref="forumServiceTarget"
        p:interceptorNames="pmonitor" p:proxyTargetClass="true" />

 

在第一个测试中

 

@Test
    public void test() {
        String configPath = "/aop/introduce/applicationContext.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath);
        ForumService forumService = (ForumService) ctx.getBean("forumService");
        forumService.removeForum(10);
        forumService.removeTopic(1022);
        Monitorable moniterable = (Monitorable) forumService;
        moniterable.setMonitorActive(true);
        forumService.removeForum(10);
        forumService.removeTopic(1022);
    }

可以正常运行

 

通过@ContextConfiguration加载配置文件,方法如下

 

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/aop/introduce/applicationContext.xml" })
public class ForumServiceTest{

    @Autowired
    @Qualifier(value = "forumService")
    ForumService forumService;

    @Test
    public void test() {
        forumService.removeForum(10);
        forumService.removeTopic(1022);
        Monitorable moniterable = (Monitorable)forumService;
        moniterable.setMonitorActive(true);
        forumService.removeForum(10);
        forumService.removeTopic(1022); 
    }
}

抛出org.springframework.beans.factory.NoSuchBeanDefinitionException的异常,指向forumService不存在

 

其他主要代码:

public interface Monitorable{
    void setMonitorActive(boolean active);
}
@Component(value = "pmonitor")
public class ControllablePerformaceMonitor extends DelegatingIntroductionInterceptor implements Monitorable{
    private static final long serialVersionUID = 7120067058565963790L;
    private ThreadLocal<Boolean> MonitorStatusMap = new ThreadLocal<Boolean>();

    public void setMonitorActive(boolean active) {
        MonitorStatusMap.set(active);
    }

    public Object invoke(MethodInvocation mi) throws Throwable {
        Object obj = null;
        if (MonitorStatusMap.get() != null && MonitorStatusMap.get()) {
            PerformanceMonitor.begin(mi.getClass().getName() + "." + mi.getMethod().getName());
            obj = super.invoke(mi);
            PerformanceMonitor.end();
        } else {
            obj = super.invoke(mi);
        }
        return obj;
    }
}
@Component(value = "forumServiceTarget")
public class ForumService{

    public void removeTopic(int topicId) {
        System.out.println("模拟删除Topic记录:" + topicId);
        try {
            Thread.currentThread().sleep(20);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    public void removeForum(int forumId) {
        System.out.println("模拟删除Forum记录:" + forumId);
        try {
            Thread.currentThread().sleep(40);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

 

注:通过第二种方式测试AOP的前置、后置、环绕、异常后置增强均通过

@ContextConfiguration(locations = { "classpath:/aop/introduce/applicationContext.xml" }) 这么写也不行

 

 

 

  • 写回答

2条回答 默认 最新

  • jinnianshilongnian 2013-06-14 12:18
    关注

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "classpath:aop/introduce/applicationContext.xml" })
    public class ForumServiceTest{

    @Autowired
    private ApplicationContext ctx;
    
    @Test
    public void test() {
        ForumService forumService = ctx.getBean("forumService", ForumService.class);
        forumService.removeForum(10);
        forumService.removeTopic(1022);
        Monitorable moniterable = (Monitorable)forumService;
        moniterable.setMonitorActive(true);
        forumService.removeForum(10);
        forumService.removeTopic(1022);
    }
    

    }

    你暂时先通过如上代码实现吧 具体原因 和 spring-data-jpa 与shiro集成时的原因类似,,后边我会专门写篇文章介绍 为什么

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的