Springs - SPLessons

Spring AOP

Chapter 13

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Spring AOP

Spring AOP

shape Description

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.

shape Description

Spring AOP use

shape Description

AOP modules

shape Description

Spring AOP concepts have their own functionalities such as below.

Join point

It is about where the advice is to be applied.


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.


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


It adds methods and attributes to previous class.


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


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.


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

shape Description

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.

shape Example

Following example show you how Spring AOP advice works.

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.

<beans xmlns="http://www.springframework.org/schema/beans"

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



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");

		try {
		} 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.

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


shape Key Points

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