bhavin
bhavin

Reputation: 347

Spring AOP (Aspect) Not executing

I ams using Spring 2.5.6, asm 1.5.3, aspectjrt/aspectjweaver 1.6.1, cglib 2.1_3 In my Web based Spring application I have following class:

package uk.co.txttools.aspects;

@Aspect
public class LoggingAspect {
    @Before("execution(* uk.co.txttools.web.controller.compose.PreviewMessageController.set*(..))")
    public void setLoggingAdvice(){
        System.out.println("********************************* Advice run..... set mothod called....");
    }

    @AfterThrowing("execution(* uk.co.txttools.web.controller.compose.PreviewMessageController.onSubmit(..) throws java.lang.Exception)")
    public void hadleException(){
       System.out.println("================= PreviewMessageController =========== ON SUBMIT Exception Throwen ==================");
    }

    @Before("execution(* uk.co.txttools.web.controller.compose.PreviewMessageController.onSubmit(..) throws java.lang.Exception)")
    public void OnSubmitAspect(){
        System.out.println("================= PreviewMessageController =========== ON SUBMIT CALLED ==================");
    }
}

I have one Controller:uk.co.txttools.web.controller.compose.PreviewMessageController which hasonSubmit()method, which get called from web page. I have separateapplicationContext.xml` file.

My springapp-servlet.xml(which is used in web.xml file with org.springframework.web.servlet.DispatcherServlet) file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">

<aop:aspectj-autoproxy proxy-target-class="true" />

<bean id="loggingAspect" class="uk.co.txttools.aspects.LoggingAspect" />
.
.

Below in same xml file PreviewMessageController get initialize which means my Controller and Aspect live is same container.

I don't get any exception while running application but my aspect class LoggingAspect never get called. I'm not sure what is that missing or I'm doing wrong. Please help me..

Thanks

Upvotes: 24

Views: 46464

Answers (7)

Payam Soudachi
Payam Soudachi

Reputation: 489

I had this issue and as I didn't see the reason for that I'm mentioning it. My problem was related to the component scan. There was a config class like this:

@ComponentScan(basePackages = {
    "com.example.web.controller",
    "com.example.service",
    "com.example.repository"})

And the aspect class was located in the log package and needed to add it to the @ComponentScan.

Upvotes: 0

PowerStat
PowerStat

Reputation: 3839

Only to make the list of possible answers complete:

To me it looks like you are missing the following dependency in your maven pom.xml:

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

Upvotes: 1

bhavin
bhavin

Reputation: 347

Finally SOLVED it.

I think I was missing aspectj-maven-plugin. It required for spring to weaving of aspects. None tutorial provide this information though. Added following to my pom.xml.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.0</version>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>1.6.1</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
                <goal>test-compile</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <outxml>true</outxml>
        <verbose>true</verbose>
        <showWeaveInfo>true</showWeaveInfo>
        <aspectLibraries>
            <aspectLibrary>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
            </aspectLibrary>
        </aspectLibraries>
        <source>1.6</source>
        <target>1.6</target>
    </configuration>
</plugin>

Thanks guys

Upvotes: 5

Zakaria
Zakaria

Reputation: 15070

For those who opted for JavaConfig, you can declare your Aspect as a bean and add the @EnableAspectJAutoProxy annotation to turn on auto-proxying :

@Configuration
@EnableAspectJAutoProxy
@ComponentScan
public class MyConfig {
    @Bean
    public LoggingAspect loggingAspect(){
        return new LoggingAspect();
    }
}

Upvotes: 12

baraka
baraka

Reputation: 806

I'm not sure if I did it properly but for me what solved it was adding @Component to the "Aspect'ed" class -

@Aspect
@Component
public class PerformanceLogger {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Around("within(com.something.rest.service..*)")
    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
        long start = System.currentTimeMillis();
        Object retVal = pjp.proceed();
        long end = System.currentTimeMillis();
        logger.debug(pjp.getSignature().toShortString() + " Finish: " + (end - start) + "ms");
        return retVal;
    }
}

(And just to close the loop - if you are using annotation based, don't forget adding @EnableAspectJAutoProxy to your Config class.

@EnableAspectJAutoProxy

Upvotes: 44

Vikram
Vikram

Reputation: 4192

If you havent tried already...try the xml based spring-aop configuration as follows:

    <aop:config>
    <aop:aspect ref="loggingAspect">
        <aop:pointcut expression="execution(* uk.co.txttools.web.controller.compose.PreviewMessageController.set*(..))" id="previewMessageControllerSetters"/>
        <aop:before method="setLoggingAdvice" pointcut-ref="previewMessageControllerSetters"/>

         // set other 2 pointcuts similarly....
        </aop:aspect>       
    </aop:config>
    <bean id="loggingAspect" class="uk.co.txttools.aspects.LoggingAspect" />

Upvotes: 1

Vikram
Vikram

Reputation: 4192

Try this in your config file:

<aop:aspectj-autoproxy proxy-target-class="true">
     <aop:include name="loggingAspect"/>
</aop:aspectj-autoproxy>

Upvotes: 0

Related Questions