Spring AOP XML Based Configuration

In this article, we will explore Spring AOP XML configuration. How to use pure XML configuration without any annotations. I have used aop namespaces.

We’ll learn how to configure aspects, advice and pointcuts using the traditional XML way.

package com.dineshonjava.Spring.Aop;

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

import com.venkat.Spring.Aop.service.ShapeService;

public class AopTest {
public static void main(String args[])
{
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
ShapeService service=context.getBean("shapeService",ShapeService.class);
service.getCircle();
}
}
package com.dineshonjava.Spring.Aop;

public @interface Loggable {

}
package com.dineshonjava.Spring.Aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class LoggingAspect {

// @Pointcut("execution(*  get*())")
// public void allGetters(){}
// @Around("@annotation(com.dineshonjava.Spring.Aop.Aspect.Loggable)")
public Object myAroundAdvice(ProceedingJoinPoint proceedingJoinPoint) 
{
Object returnValue=null;
try {
System.out.println("Before method Executed");
returnValue=proceedingJoinPoint.proceed();
System.out.println("After Method Executed....");
} catch (Throwable e) {
System.out.println("Around Advice throws exception");
}
System.out.println("After Finally executed........");
return returnValue;
}
}
package com.dineshonjava.Spring.Aop;

 public class Circle {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
System.out.println("Setter method is called.....");
throw(new RuntimeException());
}
public String setNameAndReturning(String name) {
this.name = name;
System.out.println("Setter Method and return the value method executed....");
return name;
}
}
package com.dineshonjava.Spring.Aop;

 public class Triangle {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
package com.dineshonjava.Spring.Aop;

import com.dineshonjava.Spring.Aop.Aspect.Loggable;
import com.dineshonjava.Spring.Aop.model.Circle;
import com.dineshonjava.Spring.Aop.model.Triangle;

public class ShapeService {

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

 

<beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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 class="com.dineshonjava.Spring.Aop.model.Triangle" name="triangle">
<property name="name" value="triangle name">
</property></bean>
<bean class="com.dineshonjava.Spring.Aop.model.Circle" name="circle">
<!-- <property name="name" value="circle name"/>  -->
</bean>
<bean class="com.dineshonjava.Spring.Aop.service.ShapeService" name="shapeService">
<property name="triangle" ref="triangle">
<property name="circle" ref="circle">
</property></property></bean>
<bean class="com.dineshonjava.Spring.Aop.Aspect.LoggingAspect" name="loggingAdviceBean">
<aop:config>
<aop:aspect id="loggingAdvice" ref="loggingAdviceBean">
<aop:pointcut expression="execution(*  get*())" id="allGetters">
<aop:around method="myAroundAdvice" pointcut-ref="allGetters">
</aop:around></aop:pointcut></aop:aspect>
</aop:config>
</bean></aop:aspectj-autoproxy></beans>

 

Spring AOP Related Posts

  1. Spring AOP Interview Questions and Answers
  2. Spring AOP-Introduction to Aspect Oriented Programming
  3. @Aspect Annotation in Spring
  4. Advices in Spring AOP
  5. Spring AOP JoinPoints and Advice Arguments
  6. Spring AOP-Declaring pointcut Expressions with Examples
  7. Spring AOP XML configuration
  8. Spring AOP XML Schema based Example
  9. Spring AOP AspectJ @Before Annotation Advice Example
  10. Spring AOP Before Advice Example using XML Config
  11. Spring AOP AspectJ @After Annotation Advice Example
  12. Spring AOP After Advice Example using XML Config
  13. Spring AOP AspectJ @AfterReturning Annotation Advice Example
  14. Spring AOP After-Returning Advice Example using XML Config
  15. Spring AOP AspectJ @AfterThrowing Annotation Advice Example
  16. Spring AOP After Throwing Advice Example using XML Config
  17. Spring AOP AspectJ @Around Annotation Advice Example
  18. Spring AOP Around Advice Example using XML Config
  19. Spring AOP Writing First AspectJ Program in Spring
  20. Spring AOP Proxies in Spring
  21. Spring AOP Transaction Management in Hibernate
  22. Spring Transaction Management
  23. Spring Declarative Transaction Management Example
  24. Spring AOP-Ordering of Aspects with Example

 

Previous
Next