是否可以排除@Component
带注释的类?
我想从 JUnit 测试中排除一个特殊的类。
我的项目有一个类xEventHandler
注释为@Component
我不希望 spring 在 junit 测试时使用这个类。
我的应用程序 TestContext.xml 如下所示:
<context:component-scan base-package="com.company">
...
<context:exclude-filter expression="com\.company\.xyz\.xEventHandler.java " type="regex"/>
...
</context:component-scan>
然而,进入该班级是因为@Component
注释(例如,当我删除@Component
来自该班级)。
我该如何修复它?
恕我直言,最好使用配置文件(在 3.1 中引入)来实现这一点:
- 在里面定义这个bean
test
profile
- 启用
test
在 JUnit 测试中添加配置文件@ActiveProfiles("test")
注解
有关配置文件的更多信息:
- http://blog.springsource.com/2011/02/11/spring-framework-3-1-m1-released/ http://blog.springsource.com/2011/02/11/spring-framework-3-1-m1-released/
- http://www.captaindebug.com/2012/08/using-spring-profiles-in-xml-config.html http://www.captaindebug.com/2012/08/using-spring-profiles-in-xml-config.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)