kamal
kamal

Reputation: 101

@AspectJ Based AOP with Spring 3.1

I am trying to run a @AspectJ Based AOP with Spring 3.1 & not able to configure pointcut properly Pointcut and advice methods are :

Pointcuts:

@Pointcut("execution(* point.*.*(..))")
public void selectAll() {}

after advice:

@After("selectAll()")
public void afterAdvice() {
    System.out.println("profile has been setup.");
}

before advice:

@Before("selectAll()")
public void beforeAdvice() {
    System.out.println("Going to setup profile.");
}

& run main program i got Exception:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'student' defined in class path resource [spring.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut selectAll
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:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)

although when i give pointcut expression in beforeAdvice() and afterAdvice() methods , remove PointCuts method every thing work fine

@Before("execution(* point.*.*(..))")
public void beforeAdvice() {
    System.out.println("Going to setup profile.");
}

 @After("execution(* point.*.*(..))")
public void afterAdvice() {
    System.out.println("profile has been setup.");
}

i am trying to apply pointcut to method of Student class:

package point;

 public class Student{
private Integer age;
private String name;

public void setAge(Integer age) {
    this.age = age;
}

public Integer getAge() {
    System.out.println("Age : " + age);
    return age;
}

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

public String getName() {
    System.out.println("Name : " + name);
    return name;
}

public void printThrowException() {
    System.out.println("Exception raised");
    throw new IllegalArgumentException();
}
 }

spring configuration 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

<aop:aspectj-autoproxy />
 <bean id="student" class="point.Student">
    <property name="name" value="Zara" />
    <property name="age" value="11" /> 
</bean>
<bean id="logging" class="point.Logging" />
</beans>

Jar file used :

aopalliance-1.0.jar
asm-3.3.1.jar
aspectj-1.7.1.jar
aspectjrt-1.6.8.jar
aspectjtools-1.5.4.jar
aspectjweaver-1.6.2.jar
cglib-2.2.2.jar

with spring 3.1 jar's

Upvotes: 2

Views: 1940

Answers (1)

kamal
kamal

Reputation: 101

solved the problem : The issue while creating bean was because of old jars

Replaced jars:

 aopalliance-1.0.jar
 asm-3.3.1.jar
 aspectj-1.7.1.jar
 aspectjrt-1.6.8.jar
 aspectjtools-1.5.4.jar
 aspectjweaver-1.6.2.jar
 cglib-2.2.2.jar

with

aopalliance-1.0.jar
asm-3.3.1.jar
aspectj-1.7.1.jar
aspectjrt-1.7.0.jar
aspectjweaver-1.7.0.jar
cglib-2.2.2.jar

Upvotes: 4

Related Questions