Announcement Announcement Module
Collapse
No announcement yet.
Injecting Dependencies Into Aspects Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Injecting Dependencies Into Aspects

    I have an Aspect like

    Code:
    public aspect ActionLoggingAspect {
    
        private IAppUserContext appUserContext;
        private static final Logger log = Logger.getLogger(ActionLoggingAspect.class);
    
        private pointcut doLogActionExecution(PageBean pageBean, ActionForm form, HttpServletRequest request,
                HttpServletResponse response) : 
            execution(String BeanMultiAction+.doExecute(PageBean, ActionForm, HttpServletRequest, HttpServletResponse)) && args(pageBean, form, request, response);
    
        before(PageBean pageBean, ActionForm form, HttpServletRequest request, HttpServletResponse response) : doLogActionExecution( pageBean, form, request, response) {
    ...    
    }
    
        public void setAppUserContext(final IAppUserContext appUserContext) {
            this.appUserContext = appUserContext;
        }    
    }
    and I am trying to inject appUserContext into this aspect. My config

    Code:
    <bean
    class="packagexxxx.ActionLoggingAspect"
    lazy-init="true"
    p:appUserContext-ref="appUserContext"/>
    
    <bean 
    id="appUserContext"
    class="packagexxxx.AppUserContext" />
    When the application starts up, I am getting

    Code:
    Caused by: 
    java.lang.IllegalStateException: Expecting to find 5 arguments to bind by name in advice, but actually found 4 arguments.
    	at org.springframework.aop.aspectj.AbstractAspectJAdvice.bindExplicitArguments(AbstractAspectJAdvice.java:412)
    	at org.springframework.aop.aspectj.AbstractAspectJAdvice.bindArgumentsByName(AbstractAspectJAdvice.java:377)
    	at org.springframework.aop.aspectj.AbstractAspectJAdvice.calculateArgumentBindings(AbstractAspectJAdvice.java:344)
    	at org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.getAdvice(ReflectiveAspectJAdvisorFactory.java:219)
    	at org.springframework.aop.aspectj.annotation.InstantiationModelAwarePointcutAdvisorImpl.instantiateAdvice(InstantiationModelAwarePointcutAdvisorImpl.java:145)
    	at org.springframework.aop.aspectj.annotation.InstantiationModelAwarePointcutAdvisorImpl.<init>(InstantiationModelAwarePointcutAdvisorImpl.java:94)
    	at org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.getAdvisor(ReflectiveAspectJAdvisorFactory.java:135)
    	at org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory$1.doWith(ReflectiveAspectJAdvisorFactory.java:75)
    	at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:193)
    	at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:170)
    	at org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.getAdvisors(ReflectiveAspectJAdvisorFactory.java:71)
    	at org.springframework.aop.aspectj.annotation.BeanFactoryAspectJAdvisorsBuilder.buildAspectJAdvisors(BeanFactoryAspectJAdvisorsBuilder.java:111)
    	at org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator.findCandidateAdvisors(AnnotationAwareAspectJAutoProxyCreator.java:85)
    	at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:83)
    	at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:66)
    	at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:296)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:315)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1181)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:428)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:156)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:284)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
    	at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:244)
    	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:187)
    	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:49)
    	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3729)
    	at org.apache.catalina.core.StandardContext.start(StandardContext.java:4187)
    	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:759)
    	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:739)
    	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:524)
    	at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:608)
    	at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:535)
    	at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:470)
    	at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1122)
    	at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:310)
    	at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1021)
    	at org.apache.catalina.core.StandardHost.start(StandardHost.java:718)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1013)
    	at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:442)
    	at org.apache.catalina.core.StandardService.start(StandardService.java:450)
    	at org.apache.catalina.core.StandardServer.start(StandardServer.java:709)
    	at org.apache.catalina.startup.Catalina.start(Catalina.java:551)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:585)
    	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:294)
    	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:432)
    2008-01-12 14:27:16,359 ERROR [main] (StandardContext.java:3733) - Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
    Any input would be appreciated. I am generally not having any luck injecting dependencies into AspectJ aspects.

  • #2
    This has nothing to do with DI, its the AspectJ code in your class that's blowing up, I think.

    Code:
     execution(String BeanMultiAction+.doExecute(PageBean, ActionForm, HttpServletRequest, HttpServletResponse)) && args(pageBean, form, request, response);
    Are the brackets put up correctly, should the red one be moved to the end to complete the pointcut expression?
    Last edited by nikhil78; Jan 14th, 2008, 01:31 AM.

    Comment


    • #3
      Thanks for the reply. Aspect itself works great, just when I try to inject dependencies I am getting that weird exception!

      Comment


      • #4
        this is what I have which does similar thing

        Code:
            <bean id="setPasswordInterceptor" 
            	factory-method="aspectOf"
            	class="org.gng.jbc.advice.EncodePassword" lazy-init="false">
                <property name="authenticationProvider" ref="daoAuthenticationProvider"/>
            </bean>
        EncodePassword is an aspect(class).

        I am using 2.5.1 + aspectJ 1.5.3, for what it is worth.

        Comment


        • #5
          Thanks Gary! I am on Spring 2.0.5 still for this application, I am may test on Spring 2.5, perhaps there is an issue in Spring 2.0.x related to this so I may test it out just to be sure

          Comment

          Working...
          X