Results 1 to 10 of 12

Thread: Autowiring in Aspects

Hybrid View

  1. #1

    Default Autowiring in Aspects

    I am using Spring MVC for web applications and for Audit logging I am using AOP and it works when I log the changes using log4j. I want to dump these changes to database and for this I am using Autowired DAO inside my Aspect bean as below.

    Can somebody let me know why it fails? I am really not sure about the sequence of things when aspects come in to play at runtime.


    Code:
    @Aspect
    public class LoggingInterceptor
    {
        Log log = LogFactory.getLog(LoggingInterceptor.class);
    
        @Autowired
        private AuditData auditData;
    
        @Around("@annotation(com.xx.yy.util.audit.Auditable)")
        public Object profile(ProceedingJoinPoint pjp) throws Throwable
        {
            final String currentUser = SecurityContextHolder.getContext().getAuthentication().getName();
            try
            {
    
                Object resultValue = pjp.proceed();
                if (log.isErrorEnabled())
                {
                    logInfo(currentUser, pjp);
                    System.out.println("dao: " + auditData); // am getting this as null
                    // logAudit(currentUser, pjp); //dump changes to db 
                }
                return resultValue;
            }
            finally
            {
                // sw.stop();
    
            }
        }
        
    }
    Last edited by ketankhairnar; Dec 31st, 2009 at 12:50 PM.

  2. #2
    Join Date
    Jun 2006
    Location
    SF Bay Area, California
    Posts
    524

    Default

    The aspect itself looks fine. It is probably your configuration that is causing the problem. Do you have other non-aspect beans with the DAO as an autowired dependency? Does that work?

    -Ramnivas
    Ramnivas Laddad (Follow me on Twitter)
    AspectJ in Action: Enterprise AOP with Spring Applications (2nd edition). Now available!

  3. #3

    Default

    I do have a Service bean/Controller bean with with this dao as Autowired dependency and that works fine. I tried these combination just for testing.
    Configuration is as below:

    Code:
    	<!-- Turn on AspectJ @Configurable support -->
    	<aop:aspectj-autoproxy proxy-target-class="false" />
    	<bean id="logInterceptor" class="com.xx.yy.util.audit.LoggingInterceptor" >
    		<property name="dao">
    			<ref bean="auditData"/>
    		</property>
    	</bean>
    Also myDAO don't have interface. is that a problem
    Last edited by ketankhairnar; Jan 1st, 2010 at 10:17 PM.

  4. #4
    Join Date
    Jun 2006
    Location
    SF Bay Area, California
    Posts
    524

    Default

    First, the comment is incorrect :-) <aop:aspectj-autoproxy> does NOT turn on @Configurable support.

    You don't have property named "dao" in your aspect (it is named "auditData"). Did you not get an error for this?

    -Ramnivas
    Ramnivas Laddad (Follow me on Twitter)
    AspectJ in Action: Enterprise AOP with Spring Applications (2nd edition). Now available!

  5. #5

    Default

    my bad, sorry for the confusion
    I tried both approaches :

    1) putting "dao" in Aspect and configuration is done as mentioned above. So I had getter and setter for dao

    2) using @Autowired annotation as in the very first snippet.

  6. #6
    Join Date
    Jun 2006
    Location
    SF Bay Area, California
    Posts
    524

    Default

    It may help if you put together exact code that doesn't work. What you describe is a common scenario, so it must be a combination of how you put things together that causes it not to work.

    -Ramnivas
    Ramnivas Laddad (Follow me on Twitter)
    AspectJ in Action: Enterprise AOP with Spring Applications (2nd edition). Now available!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •