我正在尝试注入一个方面内的对象。但它总是无效。这个拦截器用于使用aspectj注入域对象,因此除了以下定义
之外,不受spring管理<context:load-time-weaver />
<context:component-scan base-package="framework.interceptor" />
@Aspect
public class LoggingInterceptor {
@Autowired
EventLogManager eventLogManager;
.....
}
我的单元测试是这样的。当调用asa.execute()时,它会被LoggingInterceptor拦截,但LoggingInterceptor.eventLogManager始终为null。但是下面的testInjection()工作正常。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext-dao.xml",
"classpath:applicationContext-service.xml",
"classpath:applicationContext-resources.xml",
"classpath:LoggingTest-context.xml"})
public class LoggingInterceptorTest {
@Autowired
EventLogManager eventLogManager;
@Test
public void testInjection(){
Assert.assertNotNull(eventLogManager);
}
@Test
public void testAccountSaveActionAdvice(){
AccountSaveAction asa = new AccountSaveAction();
asa.execute();
}
}
我的applicationContext-service.xml包含以下内容
<bean id="eventLogManager"
class="service.impl.EventLogDBManagerImpl">
<property name="eventLoggingDao" ref="eventLoggingDao" />
</bean>
我在META-INF中的aop.xml看起来像这样
<aspectj>
<weaver>
<!-- only weave classes in this package -->
<include within="action..*" />
</weaver>
<aspects>
<!-- use only this aspect for weaving -->
<aspect name="interceptor.LoggingInterceptor" />
</aspects>
</aspectj>
答案 0 :(得分:3)