3

I am new to spring framework and was trying some sample examples to understand the AOP and here is what I did till now, but its not working.

The problem is as soon as I add <aop:aspectj-autoproxy /> to spring.xml my build fails saying not able to create bean with null pointer exception. But If I run the app without <aop:aspectj-autoproxy /> tag then it runs fine but without AOP.

Here are few details of my project. enter image description here

Here I have AopMain which is the main class which runs, LoggingAspect Where I actually defined the before aspect Have two models Circle and Triangle ShapeService which actually uses the above two models

And Here am giving the code for them

AopMain:

package com.spring.aop;

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

import com.spring.service.ShapeService;

public class AopMain {

/**
 * @param args
 */
public static void main(String[] args) {

    ApplicationContext ctx = new ClassPathXmlApplicationContext("Spring.xml");
    ShapeService shapeService =  ctx.getBean("shapeService", ShapeService.class); 
    System.out.println(shapeService.getCircle().getName());
}

}

LoggingAspect:

package com.spring.aspect;
 import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.Before;

@Aspect
public class LoggingAspect {

@Before("execution(public String getName())")
public void loggingAdvice(){
    System.out.println("Advice run, Get method called");
}
}

Circle:

package com.spring.model;

public class Circle {

private String name;

public String getName() {
    return name;
}

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

}

Triangle: Same as circle with single attribute name

ShapeService:

package com.spring.service;

import com.spring.model.Circle;
import com.spring.model.Triangle;

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;
}


}

And Now here comes the important file 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.spring.model.Triangle">
<property name="name" value="Triagnle Name"/>
</bean>

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

<bean name="shapeService" class="com.spring.service.ShapeService" autowire="byName" />

<bean name="loggingAspect" class="com.spring.aspect.LoggingAspect"/>

</beans>

Am getting the error as follows:

Error creating bean with name 'triangle' defined in class path resource [Spring.xml]: Initialization of bean failed; nested exception is java.lang.NullPointerException

But if I try to comment the <aop:aspectj-autoproxy /> line in Spring.xml and run the code it is able to create the bean. Please guide me am I missing some thing? Some library or is it some library conflict?

Something to do with <aop:aspectj-autoproxy />

786543214
  • 855
  • 4
  • 14
  • 29
  • For starters you are mixing aspectj versions don't do that. Next you also need cglib as you don't have interfaces and jdk dynamic proxies won't be applied. Finally you are using a quite old milestone version of Spring I suggest upgrading to the 4.x release. – M. Deinum Oct 24 '14 at 07:55

2 Answers2

0

I think you are referring the code from https://javabrains.io/ your code has no problem as I have done the example with same code.

Initially, I also facing the same issue but later I come up with the solution.

1] You need to install the aspectj jar after installing you will get:

i)aspectjrt.jar ii)aspectjtools.jar iii)aspectjweaver.jar iv)org.aspectj.matcher.jar

2] Now we need to use aspectjrt.jar and aspectjweaver.jar

In your code, you have also included the aspectj.jar which is not required and one more thing aspectj.jar and aspectjrt.jar version are different don't include the aspectjrt.jar and aspectjweaver.jar directly from the internet.

Install the aspectj.jar and then include both jars which are in bin folder of aspectj

Akshay Vakharia
  • 79
  • 1
  • 12
-3

Please change

 <aop:aspectj-autoproxy/>

to

<aop:aspectj-autoproxy ></aop:aspectj-autoproxy>

in spring.xml file.Hope it should work fine.