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.
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 />