春天无法注入方面

时间:2010-12-27 21:27:45

标签: spring

我正在尝试注入一个方面内的对象。但它总是无效。这个拦截器用于使用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>

1 个答案:

答案 0 :(得分:3)

那不起作用。

我google了一下,在春季论坛上找到了解决方案。这是链接

http://forum.springsource.org/showthread.php?t=79674