Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring unable to inject in aspect

Tags:

spring

I am trying to inject an object which is inside an aspect. But it always turns up null. this interceptor was used to inject domain objects using aspectj hence not managed by spring except the following definition

<context:load-time-weaver />
<context:component-scan base-package="framework.interceptor" />

@Aspect
public class LoggingInterceptor {
     @Autowired
     EventLogManager eventLogManager;
 .....
}

my unit test is something like this. when asa.execute() is called it gets intercepted by LoggingInterceptor, but LoggingInterceptor.eventLogManager is always null. But testInjection() below works fine.

 @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();
}
}

my applicationContext-service.xml has the following

<bean id="eventLogManager"
    class="service.impl.EventLogDBManagerImpl">
    <property name="eventLoggingDao" ref="eventLoggingDao" />
</bean>

my aop.xml in META-INF looks like this

<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>
like image 937
user373201 Avatar asked Dec 27 '10 21:12

user373201


1 Answers

That did not work.

I googled a little bit and found the solution in spring forum board. Here is the link

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

like image 178
user373201 Avatar answered Oct 30 '22 09:10

user373201