Springs - SPLessons

Chapter 12

Spring AOP

Spring AOP

Spring AOP language is the powerful tool. Spring AOP allows developers to add enterprise functionality to the application such as transaction, security. AOP allows to write less code. AOP separates the code logic as various parts sales as concerns. Spring AOP uses crosscutting concerns. Crosscutting concerns figure as follows. A Cross cutting concern is a service of a program, that can affect on other concerns. Examples are logging, transaction.

Spring AOP use

AOP modules

Spring AOP concepts have their own functionalities such as below.

Join point

It is about where the advice is to be applied.

Advice

Advice is a service provider,types of advice are as follows.

Advice Functionality
Before Advice Execute advicer code before method is invoked.
After Advice Run advice after the method execution regardless of its outcome.
Around Advice Before and after method execution.
After-Returning Advice Executes when method executs successfully.
After-Throwing Advice When method throws an exception during the execution.

Pointcut

Point cut is a point or condition to execute aspect of business.

Introduction

It adds methods and attributes to previous class.

Aspect

Aspect is a service,  AOP will help to add this service to business logic.

Target object

It is called as proxied object because AOP will be implemented at dynamic proxies and it is  advised (point cut+advice) by one or more services (aspects).

Interceptor

An interceptor is a service (aspect), it consists  one service provider (advice).

AOP proxy

The proxy is also called weaver, proxy combines both the business logic and its services, finally it delivers a proxy object.

Weaving

It is a procedure of jointing aspects with another object to create an advised object at run time, compile time, load time.

Custom Aspects Implementation

To implement the custom aspects spring supports two types of approaches are as follows.

AspectJ Annotation Style Approach

AspectJ is an extension of Aspect oriented programming created for the Java programming language. The aspect will be directly implemented in Java virtual machine.

XML Schema Based Approach

Here aspects will be implemented by classes with XML configuration.


Following example show you how Spring AOP advice works.
CustomerService.java

package splesson;
public class CustomerService {
	private String name;
	private String url;

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

	public void setUrl(String url) {
		this.url = url;
	}

	public void printName() {
		System.out.println("Customer name : " + this.name);
	}

	public void printURL() {
		System.out.println("Customer website : " + this.url);
	}

	public void printThrowException() {
		throw new IllegalArgumentException();
	}

}

Here created data members name, url and also performed SET and GET methods.
Spring-Customer.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean id="customerService" class="splesson.CustomerService">
		<property name="name" value="Andre Samule" />
		<property name="url" value="http://www.splesson.com" />
	</bean>

</beans>

App.java

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

public class App {
	public static void main(String[] args) {
		ApplicationContext appContext = new ClassPathXmlApplicationContext(
				new String[] { "Spring-Customer.xml" });

		CustomerService cust = (CustomerService) appContext.getBean("customerService");

		System.out.println("*************************");
		cust.printName();
		System.out.println("*************************");
		cust.printURL();
		System.out.println("*************************");
		try {
			cust.printThrowException();
		} catch (Exception e) {

		}

	}
}

The Application Context is spring’s more best in class holder. Like BeanFactory it can stack bean definitions, wire beans together and administer beans upon solicitation. Also it includes more enterprise-specific usefulness, for example, the capacity to determine literary messages from a properties document and the capacity to distribute application events to interested event listeners. This container is characterized by the org.springframework.context.ApplicationContext interface.
Output

*************************
Customer name : Andre Samule
*************************
Customer website : http://www.splesson.com
*************************


Summary

  • AOP permits the developer to add enterprise features to the application.
  • AOP separates code into random parts called concerns.
  • AOP permits the developer to use custom aspects.
  • Point cut is an expression language of Aspect oriented programming.
  • An Aspect is a class that consists of Join points and advices.
  • An interceptor is a service that consists only one advice.
  • An AOP proxy is a JDK dynamic proxy or CGLIB(byte code generation library) proxy in spring framework.
  • AspectJ is an extension of Aspect oriented programming created for the Java programming language.