Timothy Kanters
Timothy Kanters

Reputation: 316

Aspect not excecuting at runtime (Spring 3, AspectJ)

I have created an Aspect but it doesn't seem to be working, I see nothing in the log, also no error messages or anything in the log that help me.

The repository gets Autowired by Spring into another bean, that works so the component-scanning works. The Aspect is inside the package being scanned. I have aspectj on the classpath.

The Aspect:

@Aspect
@Component
public class LoggingAspect {

    private static Logger logger = Logger.getLogger(LoggingAspect.class);

    @Before("execution(* nl.bar.repository.*.*(..))")
    public void logIt(JoinPoint joinPoint) {
        logger.debug("WE'RE LOGGING IT!!!!");      
    }
}

The Spring bean:

package nl.bar.repository

@Component
public class BarRepository {

    public List<Bar> findBar() {
        ....
    }
}

ApplicationContext:

<context:annotation-config />
<context:component-scan base-package="nl.bar" />
<aop:aspectj-autoproxy/>

Maven:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>2.5.6</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.6.11</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.6.11</version>
</dependency>

Upvotes: 1

Views: 4793

Answers (1)

Dave Newton
Dave Newton

Reputation: 160170

You must be leaving out some of your Maven dependencies, because without spring-context the code shouldn't compile. Adding spring-context, the code works as is.

Here's my guess as to what's wrong: the logging level for your aspect package/class isn't set to debug.

Here's my version, and some successful output, with essentially no changes:

<dependencies>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>2.5.6</version>
  </dependency>

  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>2.5.6</version>
  </dependency>

  <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.6.11</version>
  </dependency>

  <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.6.11</version>
  </dependency>

  <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.16</version>
  </dependency>
</dependencies>

The applicationContext.xml file remains unchanged, as does the Java, except my repository class is simply:

@Component("repo")
public class BarRepository implements Repo {
    public String findBar() {
        return "Hello!";
    }
}

A sanity-check main as follows:

public static void main(String[] args) {
    LOG.debug("Initializing context...");
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

    LOG.debug("Retrieving bean...");
    Repo repo = (Repo) context.getBean("repo");

    LOG.debug("Calling bean...");
    System.out.println(repo.findBar());
}

I get the following output with org.springframework.aop logging at DEBUG, and my aspect class at INFO:

DEBUG nl.main.Main - Initializing context...
INFO  org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1125127: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1125127]; startup date [Sat Nov 12 16:39:45 EST 2011]; root of context hierarchy
INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [applicationContext.xml]
INFO  org.springframework.context.support.ClassPathXmlApplicationContext - Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1125127]: org.springframework.beans.factory.support.DefaultListableBeanFactory@89cc5e
INFO  org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@89cc5e: defining beans [org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,repo,loggingAspect,org.springframework.aop.config.internalAutoProxyCreator]; root of factory hierarchy
DEBUG org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory - Found AspectJ method: public void nl.bar.aspects.LoggingAspect.logIt(org.aspectj.lang.JoinPoint)
DEBUG org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator - Creating implicit proxy for bean 'repo' with 0 common interceptors and 2 specific interceptors
DEBUG org.springframework.aop.framework.JdkDynamicAopProxy - Creating JDK dynamic proxy: target source is SingletonTargetSource for target object [nl.bar.repository.BarRepository@1dec1dd]
DEBUG nl.main.Main - Retrieving bean...
DEBUG nl.main.Main - Calling bean...
INFO  nl.bar.aspects.LoggingAspect - WE'RE LOGGING IT!!!!
Hello!

I'm betting it's either a log level issue, or it's not compiling like you think it is, because it should be working as written.

Upvotes: 1

Related Questions