Search code examples
javaspringhibernatepointcut

Exception while using PointCut in spring


Aspect Class:

 import org.aspectj.lang.annotation.AdviceName;
 import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.Before;
 import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class LoggingAspect {


@Before("allCircleMethods()")
public void LoggingAdvice() {
    System.out.println("Advice Run: Get Method Called:");
}

@Before("args(String)")
public void StringArgumentMethod(String name) {
    System.out.println("Method That takes String Argument is calling  value of that string is "+name);
}

@Pointcut("execution(* get*())")
public void allGetters(){}

@Pointcut("within(com.cisco.kapil.model.Circle)")
public void allCircleMethods() {}    }

Main Class:

 public class AopMain {
public static void main(String args[]) {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
    ShapeService shapeservice = ctx.getBean("ShapeService", ShapeService.class);
    shapeservice.getCircle().setName("My Own Circle");
    //System.out.println(shapeservice.getCircle().getName());
}   }

Shape Class:

public class ShapeService {
private Circle circle;
private Triangle  triangle;

public Circle getCircle() {
    return circle;
}
public void setCircle(Circle circle) {
    this.circle = circle;
}
public Triangle getTriangle() {
    return triangle;
}
public void setTriangle(Triangle triangle) {
    this.triangle = triangle;
} }

Spring.xml:

<?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.0.xsd
            http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

 <aop:aspectj-autoproxy />
    <bean name="Triangle" class="com.cisco.kapil.model.Triangle">
        <property name="name" value="Triangle name" />
    </bean>


   <bean name="circle" class="com.cisco.kapil.model.Circle">
        <property name="name" value="Circle Name" />
   </bean>

    <bean name="ShapeService" class="com.cisco.kapil.service.ShapeService" autowire="byName" />

    <bean name="LoggingAspect" class="com.cisco.kapil.aspect.LoggingAspect" />
    </beans>

Circle:

public class Circle {
private String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
    System.out.println("Circle Setter is called");
}  }

Triangle:

public class Triangle {
private String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}  }

Exception:

INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@536d00: defining beans [org.springframework.aop.config.internalAutoProxyCreator,Triangle,circle,ShapeService,LoggingAspect]; root of factory hierarchy
Jul 04, 2015 2:33:16 AM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@536d00: defining beans [org.springframework.aop.config.internalAutoProxyCreator,Triangle,circle,ShapeService,LoggingAspect]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'Triangle' defined in class path resource [spring.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)

Solution

  • in your LoggingAspect.java class should @After

     @After("args(name)")
    public void StringArgumentMethod(String name) {
        System.out.println("Method take string argument is calling "+name);
    }