如何使用SpringAOP建议JUnit方法

如何使用SpringAOP建议JUnit方法,spring,junit,spring-aop,Spring,Junit,Spring Aop,我有一个扩展AbstractTransactionalJUnit4SpringContextTests的类。我想为每个子类提供@Before方法的建议。 然而,在我的方面类中,我不能注入这些实例中的任何一个。我已经查看了在应用程序上下文中创建的bean,但没有看到任何测试bean。那么我怎样才能得到它们,或者spring如何创建这些豆子呢 @ContextConfiguration(locations = {"/test-context.xml"}) public abstract cl

我有一个扩展AbstractTransactionalJUnit4SpringContextTests的类。我想为每个子类提供@Before方法的建议。 然而,在我的方面类中,我不能注入这些实例中的任何一个。我已经查看了在应用程序上下文中创建的bean,但没有看到任何测试bean。那么我怎样才能得到它们,或者spring如何创建这些豆子呢

@ContextConfiguration(locations = {"/test-context.xml"})
    public abstract class Ab extends AbstractTransactionalJUnit4SpringContextTests {
}

public class MyTests extends Ab {
    @Before //I want to advice this
    public void before() {}
}

@Aspect
public class BeforeTestsAspect {
    @Autowired //it won't find any bean of type Ab
    private Ab ab;
}

您应该看看如何使用aspectj而不是SpringAOP,有一个很好的例子说明了如何设置它


aspectj允许您切入应用程序或测试中的任何类,而不仅仅是spring创建的类。

spring不会创建这些bean,JUnit会。你应该告诉我们你真正想要达到的目标。可能还有另一个解决方案。我基本上想实现这个问题中所描述的。但是这个解决方案是不可接受的,因为它会为每个类运行脚本。