关于Spring的@Autowired和Wicket的@SpringBean

use*_*163 3 spring annotations wicket

我目前正在研究将Mockito和JUnit集成到我的Wicket/Spring/Hibernate项目中,并找到了如何使用注释来完成此操作的教程.

麻烦是我对@Autowired不熟悉,在浏览谷歌后我发现很难看出这个注释和@SpringBean注释之间的区别.

它们是同一个还是我应该注意的差异?

我的代码为这个问题提供了一些上下文:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
@TransactionConfiguration(transactionManager = "txManager", defaultRollback = false) 
public class TestHome
{
private WicketTester tester;

@Autowired
private ApplicationContext ctx;

@Autowired
private WebApplication webApplication;

@Before
public void setUp() {
    tester = new WicketTester(webApplication);
}

@Test
@Transactional
@Rollback(true)
public void testRenderHomePage() {
    tester.startPage(Home.class);
    tester.assertRenderedPage(Home.class);
    tester.assertComponent("home", Home.class);
}

}
Run Code Online (Sandbox Code Playgroud)

Mar*_*ejc 5

如果使用Wicket SpringComponentInjector,它会使用自己的注入.@Autowired注释是一个Springframework注释,但Wicket SpringComponentInjector忽略了这一点.因此,Wicket注释是@SpringBean,它标记了Spring bean或必须存在于Spring上下文中的组件自动装配(注入)的字段.

在您的代码片段中,您使用SpringJUnit4ClassRunner运行器,因此您的字段由Spring注入,因此它是正确的.

查看示例,如何使用SpringComponentInjector 如何在我的自定义Wicket模型类中注入Spring bean?