0

I am new to springs and trying to run a simple java application with java advices....

xml file...

<?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: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-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 
    <aop:aspectj-autoproxy>
        <aop:include name="com.cts.two.Advices"/>
    </aop:aspectj-autoproxy>    
    <context:annotation-config/>
    <context:component-scan base-package="com.cts.two"></context:component-scan>
</beans>

Advice Class

package com.cts.two;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class Advices implements Adv{    

    @Pointcut("execution(* com.cts.two.*.*(..))")
    public void advice(){

    }
    @Before("advice()")
    public void before(JoinPoint name) throws Throwable{
        System.out.println("inside advices");
        /*System.out.println(name.getClass() + " this is get class");
        System.out.println(name.getSignature().getName() + " this is the get signatue and get name");*/
    }
}

class on which advice needs to be applied...I want the before method of Advice class to be executed before below mentioned test() method

package com.cts.two;

import org.springframework.stereotype.Component;
@Component

public class ClassA {
    private ClassB b= new ClassB();

    public void setB(ClassB b) {
        this.b = b;
    }
    public void test(){
        System.out.println("inside classA test");
        //b.test();
    }

}

caller of methods/test class/main class

package com.cts.two;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CallerAB {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "AllAnnotations.xml");
        ClassA calledA = (ClassA) context.getBean("classA");
        calledA.test();
    }

}

the problem is that when I run the code directly the test method of class A is executed but the advice is not... Kindly advice.. Am i missing something ??? AspectJ 1.6.12 jar is also added...

skaffman
  • 398,947
  • 96
  • 818
  • 769

2 Answers2

2

Aspects should be decalred as beans.

@Aspect doesn't do it automatically, <aop:include> doesn't do it as well (it sets additional restriction on beans that can be used as aspects).

So, you need

@Aspect
@Component
public class Advices implements Adv { ... }

and don't need <aop:include>.

axtavt
  • 239,438
  • 41
  • 511
  • 482
0

As mentioned in the answer from @axtavt you need to add the @Component annotation. But you also need to remove the <aop:include>. Your spring wiring xml should just be:

<aop:aspectj-autoproxy/>
<context:annotation-config/>
<context:component-scan base-package="com.cts.two"/>

As stated in the spring AOP documentation, the name attribute in the <aop:include> element is supposed to be a bean name, not a class name. Specifying a bean explicitly overrides Spring's auto-detection and specifying it incorrectly means that there is no aspect used at all.

John Watts
  • 8,717
  • 1
  • 31
  • 35